【问题标题】:Using Pandas Value_Counts and matplotlib使用 Pandas Value_Counts 和 matplotlib
【发布时间】:2016-08-14 05:13:43
【问题描述】:

我使用 Pandas 的 value_counts 函数来提供唯一值的计数:

CountStatus = pd.value_counts(df['scstatus'].values, sort=True)

Output:
200    133809
304      7217
404      2176
302       740
500       159
403         4
301         1
dtype: int64

我现在想使用 matplotlib 绘制这些值,即“plt.barh(CountStatus)”,但是我不断收到错误消息:ValueError:不兼容的大小:参数“宽度”必须是长度 7 或标量。

我猜这可能与左侧列是索引列有关。有没有办法获得水平条形图?我需要转换它还是在函数中指定其他内容?

谢谢

【问题讨论】:

    标签: python pandas matplotlib plot bar-chart


    【解决方案1】:

    更新

    import seaborn as sns
    
    # test data, loads a pandas dataframe
    df = sns.load_dataset('planets')
    
    # display(df.head(3))
                method  number  orbital_period  mass  distance  year
    0  Radial Velocity       1         269.300  7.10     77.40  2006
    1  Radial Velocity       1         874.774  2.21     56.95  2008
    2  Radial Velocity       1         763.000  2.60     19.84  2011
    
    # plot value_counts of Series
    ax = df.method.value_counts().plot(kind='barh')
    ax.set_xscale('log')
    

    原答案

    我觉得你可以用barh:

    CountStatus.plot.barh()
    

    示例:

    CountStatus = pd.value_counts(df['scstatus'].values, sort=True)
    print CountStatus
    AAC    8
    AA     7
    ABB    4
    dtype: int64
    
    CountStatus.plot.barh()
    

    【讨论】:

      猜你喜欢
      • 2020-07-05
      • 2022-12-02
      • 2021-03-15
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多