【发布时间】:2016-04-20 23:13:09
【问题描述】:
我正在尝试生成一个堆积条形图,以在多天内每小时跟踪三个参数。样本图的图片是SamplePlot。 但是,我在 python 中绘制它没有成功。我是 python 的初学者,这让事情变得更糟。
之前为回答此问题所做的两次尝试是:Horizontal stacked bar chart in Matplotlib 和 stack bar plot in matplotlib and add label to each section (and suggestions)。但是,按照上述任何解决方案,我都无法达到预期的结果。
任何人都可以就如何生成情节或为我指明方向提供指导吗?
编辑 1: 我写的代码如下:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
status_day1 = [[0.2,0.3,0.5], [0.1,0.3,0.6], [0.4,0.4,0.2], [0.6,0.1,0.4]]
status_day2 = [[0.1,0.2,0.7], [0.3,0.2,0.5], [0.1,0.5,0.4], [0.2,0.5,0.3]]
day = ('Day1', 'Day2')
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
for x in range(0,4): #Looping through every hour
for y in range(0,3): #Looping through every parameter
if y==0:
ax.bar(1, status_day1[x][y],color='b',align='center')
elif y==1:
ax.bar(1, status_day1[x][y],color='r',align='center')
else:
ax.bar(1, status_day1[x][y],color='g',align='center')
# I am assuming that the three parameters for every hour are getting stacked on top of one another
for x in range(0,4):
for y in range(0,3):
if y==0:
ax.bar(1, status_day2[x][y],color='b',align='center')
elif y==1:
ax.bar(1, status_day2[x][y],color='r',align='center')
else:
ax.bar(1, status_day2[x][y],color='g',align='center')
ax.set_xticklabels(day)
ax.set_xlabel('Day')
ax.set_ylabel('Hours')
plt.show()
【问题讨论】:
-
您实际尝试过什么,为什么没有成功?
-
@wflynny,感谢您查看我的问题。我做了最近的编辑,将展示我所做的尝试。
标签: python python-3.x matplotlib plot