gpt4 book ai didi

python - 极坐标图 thetagrid 标签

转载 作者:行者123 更新时间:2023-12-05 07:52:18 26 4
gpt4 key购买 nike

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

我已经根据tom的回答修改了代码。图左半部分的标签似乎颠倒了。

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

最佳答案

您可以在设置thetagrids 后设置标签的角度。您需要将其设置为 90 角。如果您保留从 thetagrids 返回的 labels,您可以遍历标签并使用 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 is the fraction of the polar axes radius at which to place the label (1 is the edge). e.g., 1.05 is outside the axes and 0.95 is inside the axes.

因此,要增加轴边缘和标签之间的填充,只需制作 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

关于python - 极坐标图 thetagrid 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33899855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com