【问题标题】:Switching axis while generating histogram using data frames使用数据框生成直方图时切换轴
【发布时间】:2017-10-09 06:35:31
【问题描述】:
index_size= 10
column_size = 1
df = pd.DataFrame(np.zeros((index_size, column_size)))
df.columns = ['Value']
df.iat[0,0] = 5
df.iat[1,0] = 6
df.iat[5,0] = 8
df.hist(column='Value')

我得到下图,X 轴上带有“值”,Y 轴上带有索引 (0-9),但我想要 X 轴上的索引和 Y 轴上的“值”。

我期待如下图:

【问题讨论】:

    标签: python pandas histogram


    【解决方案1】:

    使用参数orientation,不幸的是它不在DataFrame.plot.hist文档中,因为它包含最后一个kwds参数。

    参数可以在matplotlib.pyplot.hist中找到。

    df.hist(column='Value', orientation="horizontal")
    

    编辑:

    直方图计数值:

    print (df['Value'].value_counts())
    0.0    7
    8.0    1
    6.0    1
    5.0    1
    Name: Value, dtype: int64
    

    如果需要绘图barbarh

    df.plot.bar()
    


    df.plot.barh()
    

    【讨论】:

    • 谢谢,但我怎样才能得到像我画的那样的图表?
    • 我加了回答,请查收。
    • 非常感谢。这正是我想要的。
    • 另外,如果有多个列,我只需要绘制 1 列。我该怎么做?设置(y=列名)无法正常工作。
    • 使用df['colname'].plot.bar()df['colname'].plot.barh()
    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 2012-08-14
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    相关资源
    最近更新 更多