简体   繁体   English

Matplotlib:不同的堆叠条形图?

[英]Matplotlib: different stacked bars?

I want to create a stacked bar plot with different amount of stacks for each bar. 我想创建一个堆叠的条形图,每个条形具有不同数量的堆叠。 The general example for stacked bars works fine if my data are all homogenous, but I want something that rather looks like the shown example. 如果我的数据都是同质的,则堆叠条形的一般示例可以很好地工作,但是我想要的东西看起来像所示示例。

示例:不同的堆叠条

This turned out to be whole other level in Matplotlib (while still easy with some Excel-like tool, as you can see). 事实证明,这是Matplotlib中的另一个层次(尽管您可以看到,使用某些类似于Excel的工具仍然很容易)。 Is there a convenient way of creating this kind of plot in Matplotlib? 是否有在Matplotlib中创建此类图的便捷方法? Thanks. 谢谢。

I guess you are working directly in matplotlib, but these days plotting data, especially for quick a view can be easily done with pandas, following your example we get: 我想您是直接在matplotlib中工作的,但是这些天来绘制数据(尤其是快速绘制视图)可以轻松地通过pandas完成,按照您的示例,我们得到:

import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use("ggplot")
import pandas as pd
import numpy as np

df = pd.DataFrame([pd.Series([10,20,40,10,np.nan]), pd.Series([20,10,30,10,10]), pd.Series([30,40, np.nan, np.nan, np.nan])], index=["Bar1", "Bar2", "Bar3"])

df.plot.bar(stacked=True)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM