【发布时间】:2018-12-21 15:00:40
【问题描述】:
我一直在尝试正确显示一个简单的直方图,其中日期为 x 轴,整数为 y 轴。下面的示例恰好是一个子图(2 个 y 轴,1 个共享 x 轴),但问题不存在,而是 hist 本身。
import datetime
import matplotlib
matplotlib.use('agg') # server no need to display graphics
import matplotlib.pyplot as plt
# x-axis is 3 consecutive dates (days)
now = datetime.datetime.now().date()
x = [now, now + datetime.timedelta(days=1), now + datetime.timedelta(days=2)]
# y1-axis is 3 numbers
y1 = [10, 0, 3]
y2 = [8, 0, 3]
fig, axarr = plt.subplots(2, sharex=True)
bins = range(1, len(x) + 1)
axarr[1].hist(y1, bins=len(x), edgecolor="k")
axarr[1].set_xticks(bins)
axarr[1].set_xticklabels(x)
axarr[1].set_yticks(range(0, max(y1) + 1))
# axarr[0] ommitted for simplicity
plt.savefig('a.png', bbox_inches='tight')
但是我得到的图像是......
【问题讨论】:
-
你检查过这里吗:stackoverflow.com/questions/27083051/… ?
-
@Georgy 谢谢,但最后这是我的一个错误,我需要一个
bar图表来代替。
标签: python matplotlib