简体   繁体   中英

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). Is there a convenient way of creating this kind of plot in 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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