Python: Plot a bar graph for a pandas data frame with x axis using a given column

Edamame picture Edamame · Sep 1, 2016 · Viewed 9.5k times · Source

I want to plot a bar chart for the following pandas data frame on Jupyter Notebook.

      | Month | number
-------------------------
    0 | Apr   |  6.5
    1 | May   |  7.3
    2 | Jun   |  3.9
    3 | Jul   |  5.1
    4 | Aug   |  4.1

I did:

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
trend_df.plot(kind='bar')

How do I make sure x-axis is actually showing month here?

Answer

lanery picture lanery · Sep 1, 2016

You can simply specify x and y in your call to plot to get the bar plot you want.

trend_df.plot(x='Month', y='number', kind='bar')

enter image description here

Given trend_df as

In [20]: trend_df
Out[20]: 
  Month  number
0   Apr     6.5
1   May     7.3
2   Jun     3.9
3   Jul     5.1
4   Aug     4.1