使用 Pandas Value_Counts 和 matplotlib [英] Using Pandas Value_Counts and matplotlib
问题描述
我使用了 Pandas 的 value_counts 函数来提供唯一值的计数:
CountStatus = pd.value_counts(df['scstatus'].values, sort=True)输出:200 133809304 7217404 2176302 740500 159403 4301 1数据类型:int64
我现在想使用 matplotlib 即plt.barh(CountStatus)"绘制这些值,但是我不断收到错误:ValueError:不兼容的大小:参数宽度"必须是长度 7 或标量.
我猜这可能与作为索引列的左侧列有关.有没有办法解决这个问题来获得水平条形图?我是否需要转换它或在函数中指定其他内容?
谢谢
更新
原答案
我认为你可以使用
I have used Pandas's value_counts function to provide counts of unique values:
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.
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
解决方案Update
pandas.Series.value_counts
is aSeries
method- Plot with
pandas.Series.plot
withkind='bar'
orkind='barh'
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')
Original Answer
I think you can use
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()
这篇关于使用 Pandas Value_Counts 和 matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!