How to plot a bar graph from a pandas series?
Asked Answered
C

4

29

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

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

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 ?

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

Chatter answered 29/5, 2016 at 20:32 Comment(1)
Questions like these really leave me wondering if people try googling or reading docs before asking here. pandas.pydata.org/pandas-docs/stable/visualization.htmlApollus
C
49

IIUC you need 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

Clemons answered 29/5, 2016 at 20:35 Comment(4)
Thank you. Any suggestions for increasing the size of the plot ? I have 16980 different values in the plot and it looks a bit condense. I tried using plt.figure(figsize=(20,10)). PS I am using inline plot.Chatter
plt.figure(figsize=(20,10)) before s.plot.bar() works for me very well.Clemons
Worked. I was putting it after s.plot.bar()Chatter
@NishantKumar - If need sorting values s = s.sort_values().plot.bar() and if index for axis-x then s = s.sort_index().plot.bar()Clemons
E
6

The new pandas API suggests the following way:

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 matplotlib library.

Eula answered 30/1, 2019 at 13:57 Comment(0)
P
2

Just use 'bar' in kind parameter of plot

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)

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
Peyter answered 28/2, 2019 at 7:7 Comment(0)
A
0

Sometimes, you might not want to use the df.plot.bar() convenience functions, for example, if you have multiple subplots and you would prefer to plot them by axis using Axis.bar() class methods instead.

In this case, you need to get the index and the values from the pd.Series() instance and pass them to plt.bar():

# create an example Series for testing
n_seq = pd.Series({1: 0, 2: 3, 3: 1200, 4: 250, 5: 28, 6: 10})

# multiple subplots allows side-by-side comparison of data
fig, axs=plt.subplots(1, 2, sharey=False)

# 1st subplot, linear scales
axs[0].bar(x=n_seq.index, height=n_seq.values, color='blue')

# 2nd subplot, log scales
axs[1].bar(x=n_seq.index, height=n_seq.values, color='grey')
axs[1].set_yscale('log')

This example allows you to plot the same data side by side with different scales

Aubin answered 22/6 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.