【问题标题】:Pandas Pivot Table and Matplotlib barPandas 数据透视表和 Matplotlib 栏
【发布时间】:2017-06-27 14:06:20
【问题描述】:

我正在尝试使用 Matplotlib 制作不同颜色的条形图。 到目前为止,我有一个看起来像这样的 Pandas 数据透视表:

              productType
physicalType             
Chassis                54
Fan                   295
Module                154
Power Supply           91

我可以获得条形图,但每个系列的颜色都相同。 如何为不同的系列设置不同的颜色?

谢谢,

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    一种方法是使用T 和pandas plot:

    df.T.plot.bar()
    

    另一种方法是使用itertuples

    import matplotlib.pyplot as plt
    df1 = df.reset_index()
    fig, ax = plt.subplots()
    for r in df1.itertuples():
        ax.bar(r[0],r[2],label=r[1])
    
    _ = plt.xticks(df1.index,df1.physicalType)
    

    【讨论】:

    • 非常感谢,非常有用的例子!
    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 2021-01-17
    • 1970-01-01
    • 2023-03-23
    • 2020-08-30
    • 2019-05-21
    • 2022-07-26
    • 2012-05-22
    相关资源
    最近更新 更多