Ratnesh
Ratnesh

Reputation: 1700

Plotting a graph by month wise in pandas

I have a dataset: df.head(4)

               Dewptm   Fog  Humidity   Pressurem     Tempm      Wspdm  Rainfall
datetime_utc                            
1996-11-01    11.666667 0.0  52.916667  -2659.666667  22.333333  2.466667   0
1996-11-02    10.458333 0.0  48.625000  1009.833333   22.916667  8.028571   0
1996-11-03    12.041667 0.0  55.958333  1010.500000   21.791667  4.804545   0
1996-11-04    10.222222 0.0  48.055556  1011.333333   22.722222  1.964706   0

How could I plot the histogram of Rainfall for each month. I dont know the exact approach however, I have tried something like this, but not succeed.

df.groupby([df['datetime_utc'].dt.month_name()], sort=False).sum().plot(kind="bar",figsize=(18,5),stacked=True,align='center',width=0.9)
plt.ylabel('Rainfall (mm)')
plt.xlabel('Year Wise')

But I am getting error: KeyError: 'datetime_utc'

Please suggest me how ccould I plot the graph for each month for a column Rainfall.

Upvotes: 0

Views: 1022

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

As @DamianoC. says, datetime_utc is your index. So either do:

df = df.reset_index()

df.groupby([df['datetime_utc'].dt.month_name()], sort=False).sum().plot(kind="bar",figsize=(18,5),stacked=True,align='center',width=0.9)
plt.ylabel('Rainfall (mm)')
plt.xlabel('Year Wise')

Or

df.groupby([df.index.month_name()], sort=False).sum().plot(kind="bar",figsize=(18,5),stacked=True,align='center',width=0.9)
plt.ylabel('Rainfall (mm)')
plt.xlabel('Year Wise')

However, it's recommend not to use string as index for plot. Instead, do df.index.month and modify the label with ax.set_xticklabels(). For example, if your data starts with 2018-06-04, you would get some graph like this, noting how the x-axis starts with June.

enter image description here

While, you can use df.index.month and get:

enter image description here

Upvotes: 1

Related Questions