pandas 如何从熊猫系列绘制条形图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37514686/
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-08 15:42:58  来源:igfitidea点击:

How to plot a bar graph from a pandas series?

pandasmatplotlibplotipythonseries

提问by Aashil

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

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

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

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

logs 是这里的数据框,而 article_id 是其中的列之一。

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 ?

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

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

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

回答by jezrael

IIUC you need Series.plot.bar:

IIUC你需要Series.plot.bar

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

Sample:

样本:

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

graph

图形

回答by Jinhua Wang

The new pandas APIsuggests the following way:

新的Pandas API建议采用以下方式:

import pandas as pd

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

s.plot(kind="bar", figsize=(20,10))

If you are working on Jupyter, you don't need the matplotliblibrary.

如果您正在使用 Jupyter,则不需要该matplotlib库。

回答by Archana

Just use 'bar' in kind parameter of plot

只需在情节的实物参数中使用'bar'

Example

例子

series = read_csv('BwsCount.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
series.plot(kind='bar')

Default value of kind is 'line' (ie. series.plot() --> will automatically plot line graph)

kind 的默认值为“line”(即 series.plot() --> 将自动绘制折线图)

For your reference:

供你参考:

kind : str
        ‘line' : line plot (default)
        ‘bar' : vertical bar plot
        ‘barh' : horizontal bar plot
        ‘hist' : histogram
        ‘box' : boxplot
        ‘kde' : Kernel Density Estimation plot
        ‘density' : same as ‘kde'
        ‘area' : area plot
        ‘pie' : pie plot