Skip to main content
 首页 » 编程设计

python之使用 matplotlib 的极坐标图 x 轴标签位置

2024年11月01日9txw1958

使用极坐标图来表示一些数据,我想让标签(当前位于 x 轴正上方)位于数据段上方。所以就在轴之间。

我找到了多篇关于如何旋转所有内容的文章,但它始终使标签保持在轴的正上方。

我为轴准备的代码的和平(如果您需要整个定义/代码,请这样说):

# Set axis names and orientation 
ax.set_theta_zero_location("N") 
ax.set_xticklabels(['Seg 1', 'Seg 2', 'Seg 3', 'Seg 4', 'Seg 5', 'Seg 6', 'Seg 7', 'Seg 8']) 
ax.set_ylim((0, 10.0)) 
ax.set_rgrids([5,10], angle=22) 

它生成的当前图像:

现在我想要标签 'Seg 1' , 'Seg 2'等从轴向轴之间的右侧移动。

请您参考如下方法:

您可以通过在小刻度上设置标签来做到这一点,然后将小刻度宽度设置为零,这样您就看不到它们了:

import matplotlib.ticker as ticker 
 
# Set the major and minor tick locations 
ax.xaxis.set_major_locator(ticker.MultipleLocator(np.pi/4)) 
ax.xaxis.set_minor_locator(ticker.MultipleLocator(np.pi/8)) 
 
# Turn off major tick labels 
ax.xaxis.set_major_formatter(ticker.NullFormatter()) 
 
# Set the minor tick width to 0 so you don't see them 
for tick in ax.xaxis.get_minor_ticks(): 
    tick.tick1line.set_markersize(0) 
    tick.tick2line.set_markersize(0) 
    tick.label1.set_horizontalalignment('center') 
 
# Set the names of your ticks, with blank spaces for the major ticks 
ax.set_xticklabels(['','','Seg 1','','Seg 2','','Seg 3','','Seg 4','','Seg 5','','Seg 6','','Seg 7','','Seg 8'],minor=True)