【问题标题】:Stacking multiple columns in a stacked bar plot using matplotlib in python 3在 python 3 中使用 matplotlib 在堆积条形图中堆积多列
【发布时间】:2016-04-20 23:13:09
【问题描述】:

我正在尝试生成一个堆积条形图,以在多天内每小时跟踪三个参数。样本图的图片是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()

我得到的不想要的结果是:

【问题讨论】:

  • 您实际尝试过什么,为什么没有成功?
  • @wflynny,感谢您查看我的问题。我做了最近的编辑,将展示我所做的尝试。

标签: python python-3.x matplotlib plot


【解决方案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()

【讨论】:

  • MaxNoe,感谢您提供答案并向我介绍了一种更优雅的代码编写方式,不胜感激。我还能够修改我的原始代码以达到预期的效果,并将其发布给不懂 zip 和枚举的人。
  • 抱歉,zipenumerate 是绝对的 Python 基础。如果你不明白他们在做什么,你应该阅读它。这是一个很好的谈话:youtube.com/watch?v=OSGv2VnC0go
【解决方案2】:

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

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()  

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 2019-11-28
    • 2017-11-02
    • 2021-02-27
    • 2023-03-11
    • 2018-03-10
    • 2017-09-19
    相关资源
    最近更新 更多