4

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?

Edamame
  • 23,718
  • 73
  • 186
  • 320

2 Answers2

6

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
lanery
  • 5,222
  • 3
  • 29
  • 43
0
store the data in a csv file.

example i named my file plot.csv

    save data in following format in plot.csv

Month,number

Apr,6.5

May,7.3

Jun,3.9

Jul,5.1

Aug,4.1


import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

import csv

#first read the data
data = pd.read_csv('plot.csv',sep=',')

print(data)
#create  a data frame
df = data.ix[-5:,['Month','number']]
#plot
df.plot(kind = 'bar')

plt.show()
#for ggplot
plt.style.use('ggplot')

df.plot()

plt.show()
SUNITHA K
  • 150
  • 1
  • 3