极地情节标签

时间:2015-11-24 17:14:36

标签: python matplotlib

我正在尝试使用以下脚本绘制极坐标图。我想让标签标签的角度与网格线对齐。 (如图所示)

我根据汤姆的回答修改了代码。图的左半部分的标签似乎是颠倒的。

from pylab import *
fig = figure()
rc('grid', color='r', linewidth=0.5, linestyle='-')
data1 = loadtxt("1.dat")
ax = plt.subplot(111,polar=True)
x= data1[:,0]
y= data1[:,1]
plt.scatter(x,y, s=0.1,color='r' )
gl, gt = ax.set_rgrids(range(10,81,10))
for l in gl: l.set_ls('') 
(lines,labels)=thetagrids( range(0,360,18), ("AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "KK", "LL","MM","NN","OO","PP","QQ","RR","SS","TT","UU","VV"))
for label,angle in zip(labels,range(0,360,18)):
    label.set_rotation(90-angle)
    ax.set_theta_offset(np.pi/2.0)
    ax.set_theta_direction(-1)
    ax.set_yticklabels([])
    show()

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在设置thetagrids后设置标签的角度。您需要将其设置为90-angle。如果您保留从labels返回的thetagrids,则可以循环标签并使用label.set_rotation

(lines,labels) = thetagrids( range(0,360,18), ("XXXXX","YYYYY","ZZZZ","PPPP"))

for label,angle in zip(labels,range(0,360,18)):
    label.set_rotation(90-angle)

enter image description here

修改

至于你编辑过的问题。您可以设置一个条件,指出角度是否大于180,然后使用270-angle。类似的东西:

for label,angle in zip(labels,range(0,360,18)):
    if angle <= 180:
         label.set_rotation(90-angle)
    else:
         label.set_rotation(270-angle)

enter image description here

编辑2

并且,对于您在下面的评论中的问题,您可以使用frac参数更改刻度标签偏移量thetagrids。来自docs

  

frac是放置标签的极轴半径的一部分(1是边缘)。例如,1.05在轴外,0.95在轴内。

因此,要增加轴边缘和标签之间的填充,只需制作frac>1,例如

(lines,labels)=thetagrids(range(0,360,18), 
                          ("AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "KK", "LL","MM","NN","OO","PP","QQ","RR","SS","TT","UU","VV"),
                          frac=1.2)

enter image description here