【问题标题】:Bring radial axes labels in front of lines of polar plot matplotlib将径向轴标签放在极坐标图 matplotlib 线的前面
【发布时间】:2018-12-13 17:16:20
【问题描述】:

我试图让极坐标图上的径向(或 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,但这不起作用。

大多数解决方案都不适用于极轴。有什么建议么?

【问题讨论】:

    标签: python matplotlib axis-labels polar-coordinates


    【解决方案1】:

    不幸的是,网格和实验室的 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)
    

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 2019-08-25
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 2016-08-17
      相关资源
      最近更新 更多