【问题标题】:Bar plot - plot many rows on one column pandas条形图 - 在一列 pandas 上绘制多行
【发布时间】:2020-04-13 14:37:15
【问题描述】:

我的数据框如下所示:

Index | A | B | C | D | 
USA   | 1 | 2 | 1 | 2 |
GER   | 2 | 3 | 2 | 3 |
RUS   | 3 | 4 | 3 | 4 |
GBR   | 4 | 5 | 4 | 5 |

(这些值不是真实的) 我只想为前 3 行制作条形图,其中每个“条”是一个变量(A、B、C、D),并且行显示如下:

例如,USA 是蓝色,GER 是红色,RUS 是绿色。 我尝试使用matplotlibseaborn,所以我愿意接受建议。

【问题讨论】:

    标签: python pandas matplotlib bar-chart seaborn


    【解决方案1】:

    我相信您需要通过 DataFrame.head 过滤前 3 行,然后通过 DataFrame.plot.bar 绘制所有 4 列 A,B,C,D

    df.head(3).plot.bar(stacked=True, colors=['blue','red','green', 'pink'])
    

    或者你可能想要转置,所以:

    df.head(3).T.plot.bar(stacked=True, colors=['blue','red','green'])
    

    【讨论】:

    • 如果不是前 3 行怎么办?如果我想要的 3 行不相邻怎么办?
    • @user12195705 - 然后通过df.loc[['USA','RUS','GER']].plot.bar(stacked=True, colors=['blue','red','green','pink'])等列表选择它们
    猜你喜欢
    • 2019-03-02
    • 2016-03-18
    • 2021-12-27
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2016-05-24
    相关资源
    最近更新 更多