将径向轴标签放在极坐标图 matplotlib 行的前面
Bring radial axes labels in front of lines of polar plot matplotlib
我试图让极坐标图上的径向(或 y 轴)标签位于图表上绘制的线条之上。现在他们在线下并被掩盖了。
这里是一个城市和一条线路的简化版代码:
fig, ax = plt.subplots(figsize=(10,6) , nrows=1, ncols=1,subplot_kw=dict(projection='polar'))
rmax = 15
rticks = np.arange(9,rmax,1.5)
rticklabel = np.arange(18,rmax*2,3).astype(int)
theta = np.arange(0,6.3, 0.17) #plots a circle
r = np.ones(len(theta))*(21/2)
ax.plot(theta, r,c='r', linestyle='-',linewidth = 4,zorder=1)
ax.set_rmax(rmax)
ax.set_rticks(rticks) # less radial ticks
ax.set_xticklabels([])
ax.set_rlabel_position(285) # get radial labels away from plotted line
ax.grid(True)
ax.set_facecolor('white')
ax.yaxis.grid(color='silver', linestyle=':',linewidth = 1.5,zorder=10)
ax.set_yticklabels(rticklabel,fontsize=12,zorder=10) #this zorder does nothing
我已经试过了:
plt.rcParams["axes.axisbelow"] = False
如我所愿,这会将文本置于最前面,但是,它也会带来网格线。我希望那些留在彩色线条后面。
我也尝试过更改 yaxis 网格的 zorder,但这不起作用。
大多数解决方案都不适用于极轴。有什么建议么?
不幸的是,网格和标签的 zorder 似乎与轴的 zorder 相关联:https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.grid.html
一种可能的解决方案,即使不优雅,也是自己绘制网格线
fig, ax = plt.subplots(figsize=(10,6) , nrows=1, ncols=1,subplot_kw=dict(projection='polar'))
rmax = 15
rticks = np.arange(9,rmax,1.5)
rticklabel = np.arange(18,rmax*2,3).astype(int)
theta = np.arange(0,6.3, 0.17) #plots a circle
r = np.ones(len(theta))*(21/2)
ax.plot(theta, r,c='r', linestyle='-',linewidth = 4,zorder=2)
ax.set_rticks(rticks) # less radial ticks
ax.set_xticklabels([])
ax.set_rlabel_position(285) # get radial labels away from plotted line
ax.xaxis.grid(True)
ax.yaxis.grid(False)
ax.set_facecolor('white')
ax.set_yticklabels(rticklabel,fontsize=12,zorder=10) #this zorder does nothing
ax.yaxis.set_zorder(10)
#ax.yaxis.grid(color='silver', linestyle=':',linewidth = 1.5,zorder=10)
x = np.arange(0,2*np.pi,0.05)
y = np.outer( np.ones(x.shape), rticks)
ax.plot( x,y, zorder=1, color='silver', linestyle=':')
ax.set_ylim(0,rmax)
我试图让极坐标图上的径向(或 y 轴)标签位于图表上绘制的线条之上。现在他们在线下并被掩盖了。
这里是一个城市和一条线路的简化版代码:
fig, ax = plt.subplots(figsize=(10,6) , nrows=1, ncols=1,subplot_kw=dict(projection='polar'))
rmax = 15
rticks = np.arange(9,rmax,1.5)
rticklabel = np.arange(18,rmax*2,3).astype(int)
theta = np.arange(0,6.3, 0.17) #plots a circle
r = np.ones(len(theta))*(21/2)
ax.plot(theta, r,c='r', linestyle='-',linewidth = 4,zorder=1)
ax.set_rmax(rmax)
ax.set_rticks(rticks) # less radial ticks
ax.set_xticklabels([])
ax.set_rlabel_position(285) # get radial labels away from plotted line
ax.grid(True)
ax.set_facecolor('white')
ax.yaxis.grid(color='silver', linestyle=':',linewidth = 1.5,zorder=10)
ax.set_yticklabels(rticklabel,fontsize=12,zorder=10) #this zorder does nothing
我已经试过了:
plt.rcParams["axes.axisbelow"] = False
如我所愿,这会将文本置于最前面,但是,它也会带来网格线。我希望那些留在彩色线条后面。
我也尝试过更改 yaxis 网格的 zorder,但这不起作用。
大多数解决方案都不适用于极轴。有什么建议么?
不幸的是,网格和标签的 zorder 似乎与轴的 zorder 相关联:https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.grid.html
一种可能的解决方案,即使不优雅,也是自己绘制网格线
fig, ax = plt.subplots(figsize=(10,6) , nrows=1, ncols=1,subplot_kw=dict(projection='polar'))
rmax = 15
rticks = np.arange(9,rmax,1.5)
rticklabel = np.arange(18,rmax*2,3).astype(int)
theta = np.arange(0,6.3, 0.17) #plots a circle
r = np.ones(len(theta))*(21/2)
ax.plot(theta, r,c='r', linestyle='-',linewidth = 4,zorder=2)
ax.set_rticks(rticks) # less radial ticks
ax.set_xticklabels([])
ax.set_rlabel_position(285) # get radial labels away from plotted line
ax.xaxis.grid(True)
ax.yaxis.grid(False)
ax.set_facecolor('white')
ax.set_yticklabels(rticklabel,fontsize=12,zorder=10) #this zorder does nothing
ax.yaxis.set_zorder(10)
#ax.yaxis.grid(color='silver', linestyle=':',linewidth = 1.5,zorder=10)
x = np.arange(0,2*np.pi,0.05)
y = np.outer( np.ones(x.shape), rticks)
ax.plot( x,y, zorder=1, color='silver', linestyle=':')
ax.set_ylim(0,rmax)