zorder' 在风玫瑰图中无法正常工作
我给柱状图设置了一个比网格线更高的zorder值,但网格线还是在柱子上面可见。我也试过用 'ax.set_axisbelow(True)',但没效果。有人能告诉我该怎么解决这个问题吗?
from windrose import WindroseAxes
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'WD (Deg)': [45, 90, 135, 180, 225, 270, 315, 0, 45],
'WS (m/s)': [2, 3, 4, 5, 6, 7, 8, 9, 10]}
# Create a DataFrame
df = pd.DataFrame(data)
# Create a WindroseAxes object
ax = WindroseAxes.from_ax()
# Customize the grid
ax.grid(True, linestyle='--', linewidth=2.0, alpha=0.5,
color='grey', zorder = 0)
# Set the axis below the wind rose bars
ax.set_axisbelow(True) ## Not working
# Plot the wind rose bars
ax.bar(df['WD (Deg)'], df['WS (m/s)'], normed=True, opening=0.5, edgecolor='black',
cmap=plt.cm.jet, zorder = 3)
plt.show()
我不明白为什么会这样。我想把网格线画在柱状图下面。谢谢大家!
1 个回答
0
你可以手动设置图形中各个部分的层级顺序,比如:
from windrose import WindroseAxes
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'WD (Deg)': [45, 90, 135, 180, 225, 270, 315, 0, 45],
'WS (m/s)': [2, 3, 4, 5, 6, 7, 8, 9, 10]}
# Create a DataFrame
df = pd.DataFrame(data)
# Create a WindroseAxes object
ax = WindroseAxes.from_ax()
# Customize the grid
ax.grid(True, linestyle='--', linewidth=2.0, alpha=0.5,
color='grey', zorder=0)
# Set the axis below the wind rose bars
ax.set_axisbelow(True) ## Not working
# Plot the wind rose bars
ax.bar(df['WD (Deg)'], df['WS (m/s)'], normed=True, opening=0.5, edgecolor='black',
cmap=plt.cm.jet)
from matplotlib.patches import Rectangle
# get the minimum zorder of the patches
minzorder = min(
[
c.get_zorder() for c in ax.get_children() if isinstance(c, Rectangle)
]
)
# loop through rectangle patches and set zorder manually
# (keeping relative order of each rectangle and adding 3)
for child in ax.get_children():
if isinstance(child, Rectangle):
zorder = child.get_zorder()
child.set_zorder(3 + (zorder - minzorder))
# or just subtract ZBASE, e.g.
# from windrose.windrose import ZBASE
# child.set_zorder(3 + (zorder - ZBASE))
plt.show()