在Matplotlib极坐标图上旋转theta=0

38

我有以下示例代码:

import numpy as np
import matplotlib.pyplot as plt
import random

data_theta = range(10,171,10)

data_theta_rad = []
for i in data_theta:
    data_theta_rad.append(float(i)*np.pi/180.0)

data_r = random.sample(range(70, 90), 17)

print data_theta
print data_r

ax = plt.subplot(111, polar=True)
ax.plot(data_theta_rad, data_r, color='r', linewidth=3)
ax.set_rmax(95)
# ax.set_rmin(70.0)
ax.grid(True)

ax.set_title("Example", va='bottom')
plt.show()

...它会生成类似于这样的东西: enter image description here

...但我想将 theta=0 调整到“西”方。所以需要类似于这样的结果:

enter image description here

有什么办法用 matplotlib 实现这个效果吗?(我是在 PowerPoint 中制作了下面的图片。)


4
一个小提示:numpy 提供了方便的角度转换函数:data_theta_rad = np.radians(data_theta)。意思是使用 numpy 库中的 radians 函数可以将角度转换为弧度,并将结果赋值给 data_theta_rad 变量。 - hitzg
2个回答

52

只需使用:

ax.set_theta_zero_location("W")

matplotlib的文档中有更多信息。


我得到了一个 AttributeError 错误:'PolarAxesSubplot' 对象没有 'set_theta_zero_location' 属性...也许我的模块不是最新的??? - HotDogCannon
你使用的matplotlib版本是什么?你可能需要更新,我正在使用1.3.1版本,它运行良好。 - hitzg
我有点尴尬地说,但我不知道如何找到这个答案。 - HotDogCannon

17
更加灵活的方法:

ax.set_theta_offset(pi)

您可以任意旋转坐标轴,只需使用您想要的角度替换pi即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接