2

我想要一些示例,说明如何在聚类水平条形图中绘制每个日期的平均值、最大值、最小值。

dates = [datetime.datetime(2011, 1, 2, 0, 0, 25, 135000),
datetime.datetime(2011, 2, 2, 0, 0, 57, 386000), 
datetime.datetime(2011, 3, 2, 0, 0, 59, 579000), 
datetime.datetime(2011, 4, 2, 0, 0, 27, 676000), 
datetime.datetime(2011, 5, 2, 0, 0, 25, 135000), 
datetime.datetime(2011, 6, 2, 0, 0, 26, 414000), 
datetime.datetime(2011, 7, 2, 0, 0, 28, 145000), 
datetime.datetime(2011, 8, 2, 0, 0, 26, 432000), 
datetime.datetime(2011, 9, 2, 0, 0, 27, 301000), 
datetime.datetime(2011, 10, 2, 0, 0, 27, 643000), 
datetime.datetime(2011, 11, 2, 0, 0, 27, 673000), 
datetime.datetime(2011, 12, 2, 0, 0, 28, 294000)]


mean = [-10.246537396121893, 1.3039215686274515, 1.007462686567164,
-0.12771084337349403, -0.1932367149758454, -0.3196125907990315, 
-0.04128440366972498, -0.19950738916256117, -1.6319018404907975, 
-1.1532258064516134, -1.666666666666668, -2.3958333333333326]


max = [-8.0, 19.0, 11.0, 6.0, 6.0, 6.0, 6.0, 6.0, 2.0, 2.0, 2.0, 2.0]


min = [-12.0, -7.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -7.0, -7.0]

我尝试使用 barh 方法但没有成功让日期显示 y 轴侧。

dates = [datetime.datetime(2011, 1, 2, 0, 0, 25, 135000),
    datetime.datetime(2011, 2, 2, 0, 0, 57, 386000), 
    datetime.datetime(2011, 3, 2, 0, 0, 59, 579000), 
    datetime.datetime(2011, 4, 2, 0, 0, 27, 676000), 
    datetime.datetime(2011, 5, 2, 0, 0, 25, 135000), 
    datetime.datetime(2011, 6, 2, 0, 0, 26, 414000), 
    datetime.datetime(2011, 7, 2, 0, 0, 28, 145000), 
    datetime.datetime(2011, 8, 2, 0, 0, 26, 432000), 
    datetime.datetime(2011, 9, 2, 0, 0, 27, 301000), 
    datetime.datetime(2011, 10, 2, 0, 0, 27, 643000), 
    datetime.datetime(2011, 11, 2, 0, 0, 27, 673000), 
    datetime.datetime(2011, 12, 2, 0, 0, 28, 294000)]

data_values = [
[-10.246537396121893, 1.3039215686274515, 1.007462686567164,
-0.12771084337349403, -0.1932367149758454, -0.3196125907990315, 
-0.04128440366972498, -0.19950738916256117, -1.6319018404907975, 
-1.1532258064516134, -1.666666666666668, -2.3958333333333326], #mean
[-8.0, 19.0, 11.0, 6.0, 6.0, 6.0, 6.0, 6.0, 2.0, 2.0, 2.0, 2.0], #max
[-12.0, -7.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -7.0, -7.0]]#min

width = 0.35
ind = arange(len(dates))

for i in range(len(data_values)):
    plt.barh(ind + width * i, data_values[i], width)

如何在 y 轴侧显示与日期相对应的每个平均值、最大值、最小值条的日期?感谢帮助。

4

1 回答 1

4

这是你想要的效果吗?

单杠

关键是使用matplotlib.dates模块正确格式化日期:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
datenum = matplotlib.dates.date2num(dates)

for i in range(-1,len(data_values)-1):
    ax.barh(datenum + i*5., data_values[i], height=5.)
ax.yaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m/%d'))

plt.show()
于 2013-09-27T16:35:38.217 回答