我按照以下代码段使用 Pyasciigraph 模块
thresholds = {
int(mean): Gre, int(mean * 2): Yel, int(mean * 3): Red,
}
data = hcolor(chart, thresholds)
graph = Pyasciigraph(
separator_length=4,
multivalue=False,
human_readable='si',
)
for line in graph.graph(title, data):
print(line)
print("")
绘制水平条形图如下(在下面的屏幕截图中看不到y轴标签):
现在,我正在尝试使用 matplotlib 绘制相同的数据,我的示例代码为:
import collections
import matplotlib.pyplot as plt
D = [(u'00:00', 1), (u'01:00', 1), (u'02:00', 0), (u'03:00', 0), (u'04:00', 0), (u'05:00', 0), (u'06:00', 1), (u'07:00', 7), (u'08:00', 3), (u'09:00', 2), (u'10:00', 6), (u'11:00', 2), (u'12:00', 4), (u'13:00', 4), (u'14:00', 1), (u'15:00', 6), (u'16:00', 3), (u'17:00', 2), (u'18:00', 1), (u'19:00', 1), (u'20:00', 0), (u'21:00', 2), (u'22:00', 2), (u'23:00', 1)]
D2 = dict(D)
od2 = collections.OrderedDict(sorted(D2.items(), reverse=True))
plt.barh(range(len(od2)), od2.values(), align='center')
plt.yticks(range(len(od2)), od2.keys())
plt.gca().axes.get_xaxis().set_ticks([])
plt.show()
与使用Pyasciigraph绘制的图表相比,此图表中缺少的是:
与我相关的stackoverflow上最接近的答案是this,但它没有帮助。
任何建议,提示,帮助将不胜感激。
答案 0 :(得分:1)
为了在图表右侧显示条形值,您可以使用双轴并将其标签设置为字典中的值。< / p>
为了生成阈值条,您可以在每次将值截断到阈值时在彼此的顶部绘制几个条形图。
import collections
import matplotlib.pyplot as plt
D = [(u'00:00', 1), (u'01:00', 1), (u'02:00', 0), (u'03:00', 0), (u'04:00', 0),
(u'05:00', 0), (u'06:00', 1), (u'07:00', 7), (u'08:00', 3), (u'09:00', 2),
(u'10:00', 6), (u'11:00', 2), (u'12:00', 4), (u'13:00', 4), (u'14:00', 1),
(u'15:00', 6), (u'16:00', 3), (u'17:00', 2), (u'18:00', 1), (u'19:00', 1),
(u'20:00', 0), (u'21:00', 2), (u'22:00', 2), (u'23:00', 1)]
D2 = dict(D)
od = collections.OrderedDict(sorted(D2.items(), reverse=True))
fig, ax = plt.subplots()
ax.barh(range(len(od)), od.values(), align='center')
thr = lambda l, t: [v if (v <= t) else t for v in l ]
ax.barh(range(len(od)), thr(od.values(),4), align='center')
ax.barh(range(len(od)), thr(od.values(),2), align='center')
ax.set_yticks(range(len(od)))
ax.set_yticklabels(od.keys())
ax.set_xticks([])
ax2 = ax.twinx()
ax2.set_ylim(ax.get_ylim())
ax2.set_yticks(range(len(od)))
ax2.set_yticklabels(od.values())
plt.show()