python - 使用 Pandas value_counts 和 matplotlib

标签 python pandas matplotlib bar-chart

我使用 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或标量

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

最佳答案

更新

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')

enter image description here

原始答案

我想你可以使用 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()

graph

关于python - 使用 Pandas value_counts 和 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36762199/

相关文章:

python - 无法通过 Google App Engine 上的套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器

python - Pyspark - 计算每个数据框列中的空值数

python - 在多级列 Pandas 数据框中创建列的更好方法

python - 在什么情况下我可以使用 Dask 而不是 Apache Spark?

python - 如何使用 matplotlib 将 timedelta 绘制为值

python - 如何将 Matplotlib 图转换为 PIL 图像对象(不保存图像)

python - 将 matplotlib 3d 图导出到模型

python - 当下游任务定义依赖于上游结果时如何设置 DAG

python - 苹果电脑 : Error importing scikits while it has been installed

regex - 使用正则表达式识别字母/数字组合并存储在字典中