我正在尝试使用 Python 中的 matplotlib 以顺时针方式绘制数据,其风格为这个答案 https://stackoverflow.com/questions/25898523/how-to-plot-points-on-a-clock。我在绘制数据时注意到奇怪的行为;数据点具有正确的 y 值,但不会出现在正确的 x 值(即时间)处。我首先认为我的数据是错误的,但是在使用以下工作示例重新创建我的问题后,我得出的结论是错误一定在其他地方。
import numpy as np
import matplotlib.pyplot as plt
ax = plt.subplot(111, polar=True)
equals = np.linspace(0, 360, 24, endpoint=False) #np.arange(24)
ones = np.ones(24)
ax.scatter(equals, ones)
# Set the circumference labels
ax.set_xticks(np.linspace(0, 2*np.pi, 24, endpoint=False))
ax.set_xticklabels(range(24))
# Make the labels go clockwise
ax.set_theta_direction(-1)
# Place 0 at the top
ax.set_theta_offset(np.pi/2.0)
plt.show()
This results in the following plot:
考虑到以下的定义,我预计这些点的 x 值与小时数一致equals
。它目前被定义为角度,但我也尝试将其定义为小时。为什么情况并非如此?如何使我的数据与相应的时间一致?
Matplotlib 期望角度以弧度为单位而不是度数(请参阅打开错误报告 https://github.com/matplotlib/matplotlib/issues/4986)。您可以使用 numpy 函数np.deg2rad
转换为弧度:
import numpy as np
import matplotlib.pyplot as plt
ax = plt.subplot(111, polar=True)
equals = np.linspace(0, 360, 24, endpoint=False) #np.arange(24)
ones = np.ones(24)
ax.scatter(np.deg2rad(equals), ones)
# Set the circumference labels
ax.set_xticks(np.linspace(0, 2*np.pi, 24, endpoint=False))
ax.set_xticklabels(range(24))
# Make the labels go clockwise
ax.set_theta_direction(-1)
# Place 0 at the top
ax.set_theta_offset(np.pi/2.0)
plt.show()
这会产生以下图片:
或者,您可以更改 equals 的定义以产生以弧度表示的角度:equals = np.linspace(0, 2*np.pi, 24, endpoint=False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)