Multiple stacked bar plot with pandas

前端 未结 1 1844
生来不讨喜
生来不讨喜 2021-01-12 04:18

I am trying to make a multiple stacked bar plot with pandas but I\'m running into issues. Here is a sample code:

import pandas as pd

df = pd.DataFrame({\'a\         


        
相关标签:
1条回答
  • 2021-01-12 04:41

    You could do it by shifting the position parameter of a bar-plot so that they are adjacent to each other as shown:

    matplotlib.style.use('ggplot')
    
    fig, ax = plt.subplots()
    df[['a', 'c']].plot.bar(stacked=True, width=0.1, position=1.5, colormap="bwr", ax=ax, alpha=0.7)
    df[['b', 'd']].plot.bar(stacked=True, width=0.1, position=-0.5, colormap="RdGy", ax=ax, alpha=0.7)
    df[['a', 'd']].plot.bar(stacked=True, width=0.1, position=0.5, colormap="BrBG", ax=ax, alpha=0.7)
    plt.legend(loc="upper center")
    plt.show()
    

    0 讨论(0)
提交回复
热议问题