使用python 3中的matplotlib在堆积条形图中堆叠多个列

时间:2016-04-19 19:18:20

标签: python python-3.x matplotlib plot

我正在尝试生成堆积条形图,以便在多天内跟踪每小时的三个参数。样本图的图片是SamplePlot。 但是,我在python中绘制它没有成功。我是python初学者的事实让事情变得更糟。

以前回答过这两个问题的两次尝试是:Horizontal stacked bar chart in Matplotlibstack 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()

我得到的不良结果是:Incorrect plot

2 个答案:

答案 0 :(得分:1)

您需要跟踪条形图的底部,请参阅matplotlib文档中的堆积条形图示例: http://matplotlib.org/examples/pylab_examples/bar_stacked.html

此外,您可以使用python的zipenumerate函数以及

来摆脱大多数更丑陋的循环代码
for value in data:
     print(value)

而不是

for i in range(len(data)):
    print(data[i])

有了这个,我得到了预期的结果:

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],
]

days = ('Day1', 'Day2')

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)

for day, data in enumerate((status_day1, status_day2)):
    bottom = 0
    for hour in data:  # Looping through every hour
        for value, color in zip(hour, ('b', 'r', 'g')):
            ax.bar(
                day,
                value,
                bottom=bottom,
                color=color,
                align='center',
            )
            bottom += value


ax.set_xticks([0, 1])
ax.set_xticklabels(days)
ax.set_xlabel('Day')
ax.set_ylabel('Hours')
plt.show()

答案 1 :(得分:0)

@MaxNoe已经通过使用zip和枚举非常优雅地回答了这个问题。但是,对于不熟悉zip和枚举的人,以下代码可以达到预期的效果:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

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.3]]
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]]

xval = [0.,1.,2.] #The places where the ticks are going to be on the x-axis

bottom_append = 0 #Counter to keep track of the bar (this is quite important)
fig = plt.figure(figsize=(5,5))
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:
            if x==0:
                print(status_day1[x][y], bottom_append)
                ax.bar(0, status_day1[x][y], width = 0.3, color='blue',align='center')
                bottom_append = bottom_append+status_day1[x][y]
            else:
                print(status_day1[x][y], bottom_append)
                ax.bar(0, status_day1[x][y], width = 0.3, color='blue',align='center',bottom=bottom_append) 
                bottom_append = bottom_append+status_day1[x][y]
        elif y==1:
            print(status_day1[x][y], bottom_append)
            ax.bar(0, status_day1[x][y], width = 0.3, color='red',align='center', bottom=bottom_append)
            bottom_append = bottom_append+status_day1[x][y]
        else:
            print(status_day1[x][y], bottom_append)
            ax.bar(0, status_day1[x][y], width = 0.3, color='green',align='center', bottom=bottom_append)
            bottom_append = bottom_append+status_day1[x][y]

bottom_append = 0
# Code is exactly same as the above, only takes into account day2
for x in range(0,4): #Looping through every hour
    for y in range(0,3): #Looping through every parameter
        if y==0:
            if x==0:
                print(status_day2[x][y], bottom_append)
                ax.bar(1, status_day2[x][y], width = 0.3, color='blue',align='center')
                bottom_append = bottom_append+status_day2[x][y]
            else:
                print(status_day2[x][y], bottom_append)
                ax.bar(1, status_day2[x][y], width = 0.3, color='blue',align='center',bottom=bottom_append) 
                bottom_append = bottom_append+status_day2[x][y]
        elif y==1:
            print(status_day2[x][y], bottom_append)
            ax.bar(1, status_day2[x][y], width = 0.3, color='red',align='center', bottom=bottom_append)
            bottom_append = bottom_append+status_day2[x][y]
        else:
            print(status_day2[x][y], bottom_append)
            ax.bar(1, status_day2[x][y], width = 0.3, color='green',align='center', bottom=bottom_append)
            bottom_append = bottom_append+status_day2[x][y]          


# Setting the properties of the subplot in an attempt to beautify it
Label1 = mpatches.Patch(color='blue', label='Label1')
Label2 = mpatches.Patch(color='green', label='Label2')
Label3 = mpatches.Patch(color='red', label='Label3')
ax.legend(handles=[Label1, Label2, Label3], loc=1)            
ax.set_xticks(xval)         
ax.set_xticklabels(["Day1","Day2","Day3"])
ax.set_xlabel('Day')  
ax.set_ylabel('Hours') 
plt.show()