如何在时钟上标注点位

6

我有从Unix纪元开始的秒数。我想在24小时钟上绘制它们。到目前为止,我的尝试是:

from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
angles = 2*np.pi*np.random.randint(0,864000,100)/86400
ax = plt.subplot(111, polar=True)
ax.scatter(angles, np.ones(100)*1)
plt.show()

这会得到以下结果:

尝试在时钟上绘图

然而,这并不完全是我想要的。
  • 如何将点放在圆周上而不是内部(或者至少将它们移得离中心更远)?
  • 如何将标签从角度更改为时间?
  • 如何摆脱 0.2, 0.4, ...
  • 基本上,如何使它看起来更像钟表上标记的点?
1个回答

8
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from numpy import pi

angles = 2*pi*np.random.randint(0,864000,100)/86400
ax = plt.subplot(111, polar=True)
ax.scatter(angles, np.ones(100)*1)

# suppress the radial labels
plt.setp(ax.get_yticklabels(), visible=False)

# set the circumference labels
ax.set_xticks(np.linspace(0, 2*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(pi/2.0)    

# plt.grid('off')

# put the points on the circumference
plt.ylim(0,1)

plt.show()

在此输入图片描述

或者,为了更好地利用时钟的面,您可以将scatter图替换为bar图(灵感来自这个代码高尔夫答案):

# ax.scatter(angles, np.ones(100)*1, marker='_', s=20)
ax.bar(angles, np.full(100, 0.9), width=0.1, bottom=0.0, color='r', linewidth=0)

在此输入图片描述

或者,为了让条形图更像刻度线,您可以将bottom=0.89

ax.bar(angles, np.full(100, 0.9), width=0.05, bottom=0.89, color='r', linewidth=0)

enter image description here


1
如果你想要刻度,也许可以绘制小线段。 - Nick T

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