gpt4 book ai didi

python - y 轴为日期的水平条形图

转载 作者:行者123 更新时间:2023-11-28 22:53:46 25 4
gpt4 key购买 nike

我想举例说明如何在聚集水平条形图中绘制每个日期的平均值、最大值、最小值。

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 轴侧显示日期?感谢您的帮助。

最佳答案

这就是您想要的效果吗?

horizontal bar

关键是使用 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()

关于python - y 轴为日期的水平条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com