如何绘制 pandas 系列的条形图? [英] How to plot a bar graph from a pandas series?

查看:86
本文介绍了如何绘制 pandas 系列的条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我的系列如下:第一列是article_id,第二列是频率计数.

Consider my series as below: First column is article_id and the second column is frequency count.

article_id  
1         39 
2         49 
3        187 
4        159 
5        158 
        ...  
16947     14 
16948      7 
16976      2 
16977      1 
16978      1 
16980      1 

Name: article_id, dtype: int64

我使用以下命令从数据框中获得了该系列:

I got this series from a dataframe with the following command:

logs.loc[logs['article_id'] <= 17029].groupby('article_id')['article_id'].count()

日志是此处的数据框,article_id是其中的列之一.

logs is the dataframe here and article_id is one of the columns in it.

如何绘制条形图(使用Matlplotlib),使article_id在X轴上,而频率计数在Y轴上?

How do I plot a bar chart(using Matlplotlib) such that the article_id is on the X-axis and the frequency count on the Y-axis ?

我的自然本能是使用.tolist()将其转换为列表,但这并不保留article_id.

My natural instinct was to convert it into a list using .tolist() but that doesn't preserve the article_id.

推荐答案

IIUC,您需要

IIUC you need Series.plot.bar:

#pandas 0.17.0 and above
s.plot.bar()
#pandas below 0.17.0
s.plot('bar')

示例:

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series({16976: 2, 1: 39, 2: 49, 3: 187, 4: 159, 
               5: 158, 16947: 14, 16977: 1, 16948: 7, 16978: 1, 16980: 1},
               name='article_id')
print (s)
1         39
2         49
3        187
4        159
5        158
16947     14
16948      7
16976      2
16977      1
16978      1
16980      1
Name: article_id, dtype: int64


s.plot.bar()

plt.show()

这篇关于如何绘制 pandas 系列的条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆