使用 Pandas Value_Counts 和 matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36762199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:05:32  来源:igfitidea点击:

Using Pandas Value_Counts and matplotlib

pythonpandasmatplotlibplot

提问by W4K1NG

I have used Pandas's value_counts function to provide counts of unique values:

我使用 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

I now want to plot these values using matplotlib i.e "plt.barh(CountStatus)", however I keep getting the error: ValueError: incompatible sizes: argument 'width' must be length 7 or scalar.

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

I'm guessing this may have something to do with the left hand column being an index column. Is there a way around this to obtain a horizontal bar chart? Do I need to convert it or specify something else in the function?

我猜这可能与作为索引列的左侧列有关。有没有办法解决这个问题来获得水平条形图?我是否需要转换它或在函数中指定其他内容?

Thanks

谢谢

回答by jezrael

I think you can use barh:

我认为你可以使用barh

CountStatus.plot.barh()

Sample:

样本:

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

CountStatus.plot.barh()

graph

图形