Data visualisations made by Janhavi Pimplikar
A student at Pimpri Chinchwad College of Engineering
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly as py
import cufflinks as cf
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
from plotly.subplots import make_subplots
py.offline.init_notebook_mode(connected=True)
cf.go_offline()
from plotly.offline import iplot
rainfall=pd.read_csv('IndiaRainfall.csv')
rainfall
states=rainfall[['STATE_UT_NAME','DISTRICT']]
statedata=rainfall['STATE_UT_NAME'].value_counts()
#above plot using plotly module
statedata.iplot(kind='bar',color='blue',legend=True,xTitle='Number of districts',yTitle='States and Union Territories',title='Number of districts in each state and union territory of India',orientation='v',bargap=0.7,theme='solar')
annual=rainfall[['STATE_UT_NAME','ANNUAL','Jan-Feb','Mar-May','Jun-Sep','Oct-Dec']]
annual
annual=annual.groupby('STATE_UT_NAME')[['ANNUAL','Jan-Feb','Mar-May','Jun-Sep','Oct-Dec']].mean() #The .mean() function will give us the average rainfall received by the state
annual.reset_index(inplace=True)
annual.iplot(x='STATE_UT_NAME',y='ANNUAL',mode='markers',bargroupgap=0.2,orientation='v',legend=True,xTitle='State/Union territory',yTitle='Rainfall (in mm)',title='Average annual rainfall in each state/union territory',color='red',theme='solar')
annual.iplot(x='STATE_UT_NAME',y='Jan-Feb',kind='bar',bargroupgap=0.2,orientation='v',legend=True,xTitle='State/Union territory',yTitle='Rainfall (in mm)',title='Average rainfall in each state/union territory (Jan-Feb)',color='pink',theme='solar')
annual.iplot(x='STATE_UT_NAME',y='Mar-May',kind='bar',bargroupgap=0.2,orientation='v',legend=True,xTitle='State/Union territory',yTitle='Rainfall (in mm)',title='Average rainfall in each state/union territory (Mar-May)',color='lightgreen',theme='solar')
annual.iplot(x='STATE_UT_NAME',y='Jun-Sep',kind='bar',bargroupgap=0.2,orientation='v',legend=True,xTitle='State/Union territory',yTitle='Rainfall (in mm)',title='Average rainfall in each state/union territory (Jun-Sep)',color='orange',theme='solar')
annual.iplot(x='STATE_UT_NAME',y='Oct-Dec',kind='bar',bargroupgap=0.2,orientation='v',legend=True,xTitle='State/Union territory',yTitle='Rainfall (in mm)',title='Average rainfall in each state/union territory (Oct-Dec)',color='purple',theme='solar')
top10=annual.sort_values('ANNUAL',ascending=False).head(10)
top10
fig,ax=plt.subplots(figsize=(8,8))
lab=top10['STATE_UT_NAME']
top10.plot.pie(y='ANNUAL',legend=False,shadow=True,ax=ax,autopct="%1.2f%%",colors=sns.color_palette('Accent'),fontsize=15,labels=lab)
plt.title('Percentage share of top 10 states/union territories receiving the most rainfall',size=15)
fig.show()
top10_first=annual.sort_values('Jan-Feb',ascending=False).head(10)
top10_second=annual.sort_values('Mar-May',ascending=False).head(10)
top10_first
top10_second
fig,(ax1,ax2)=plt.subplots(nrows=1,ncols=2,figsize=(16,16))
fig.subplots_adjust(hspace=0.9)
labels1=top10_first['STATE_UT_NAME']
labels2=top10_second['STATE_UT_NAME']
patches,texts,autotexts=ax1.pie(top10_first['Jan-Feb'],shadow=True,labels=labels1,autopct='%1.2f%%')
plt.setp(autotexts,size=12)
ax1.set_title('% share (Jan-Feb)',size=15)
##############################################
patches,texts,autotexts=ax2.pie(top10_second['Mar-May'],shadow=True,labels=labels2,autopct='%1.2f%%')
plt.setp(autotexts,size=12)
ax2.set_title('% share (Mar-May)',size=15)
fig.show()
top10_third=annual.sort_values('Jun-Sep',ascending=False).head(10)
top10_fourth=annual.sort_values('Oct-Dec',ascending=False).head(10)
top10_third
top10_fourth
fig,(ax1,ax2)=plt.subplots(nrows=1,ncols=2,figsize=(16,16))
fig.subplots_adjust(hspace=0.9)
labels1=top10_third['STATE_UT_NAME']
labels2=top10_fourth['STATE_UT_NAME']
patches,texts,autotexts=ax1.pie(top10_third['Jun-Sep'],shadow=True,labels=labels1,autopct='%1.2f%%')
plt.setp(autotexts,size=12)
ax1.set_title('% share (Jun-Sep)',size=15)
##############################################
patches,texts,autotexts=ax2.pie(top10_fourth['Oct-Dec'],shadow=True,labels=labels2,autopct='%1.2f%%')
plt.setp(autotexts,size=12)
ax2.set_title('% share (Oct-Dec)',size=15)
fig.show()
monthly_rain=rainfall.groupby('STATE_UT_NAME')[['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']].mean()
monthly_rain
mh=monthly_rain.loc['MAHARASHTRA']
mh.iplot(kind='line',mode='markers',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend in Maharashtra',color='blue',fill=True,legend=True,theme='solar')
ors=monthly_rain.loc['ORISSA']
ors.iplot(kind='line',mode='markers',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend in Orissa',color='pink',fill=True,legend=True,theme='solar')
pun=monthly_rain.loc['PUNJAB']
pun.iplot(kind='line',mode='markers',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend in Punjab',color='orange',fill=True,legend=True,theme='solar')
tam=monthly_rain.loc['TAMIL NADU']
tam.iplot(kind='line',mode='markers',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend in Tamil Nadu',color='green',fill=True,legend=True,theme='solar')
meg=monthly_rain.loc['MEGHALAYA']
meg.iplot(kind='line',mode='markers',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend in Meghalaya',color='red',fill=True,legend=True,theme='solar')
pon=monthly_rain.loc['PONDICHERRY']
pon.iplot(kind='line',mode='markers',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend in Pondicherry',color='purple',fill=True,legend=True,theme='solar')
mh_district=rainfall.loc[rainfall['STATE_UT_NAME']=="MAHARASHTRA"]
sub_mh=mh_district.groupby('DISTRICT')['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'].sum()
sub_mh=sub_mh.transpose()
sub_mh.iplot(kind='line',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend over all districts in Maharashtra',theme='solar')
ors_district=rainfall.loc[rainfall['STATE_UT_NAME']=='ORISSA']
sub_ors=ors_district.groupby('DISTRICT')['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'].sum()
sub_ors=sub_ors.transpose()
sub_ors.iplot(kind='line',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend over all districts in Orissa ',theme='solar')
pun_district=rainfall.loc[rainfall['STATE_UT_NAME']=='PUNJAB']
sub_pun=pun_district.groupby('DISTRICT')['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'].sum()
sub_pun=sub_pun.transpose()
sub_pun.iplot(kind='line',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend over all districts in Punjab',theme='solar')
tn_district=rainfall.loc[rainfall['STATE_UT_NAME']=='TAMIL NADU']
sub_tn=tn_district.groupby('DISTRICT')['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'].sum()
sub_tn=sub_tn.transpose()
sub_tn.iplot(kind='line',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall over all districts in Tamil Nadu',theme='solar')
meg_district=rainfall.loc[rainfall['STATE_UT_NAME']=='MEGHALAYA']
sub_meg=meg_district.groupby('DISTRICT')['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'].sum()
sub_meg=sub_meg.transpose()
sub_meg.iplot(kind='line',xTitle='Months',yTitle='Rainfall (in mm)', title='Rainfall over all districts in Meghalaya',theme='solar')
pond_district=rainfall.loc[rainfall['STATE_UT_NAME']=='PONDICHERRY']
sub_pd=pond_district.groupby('DISTRICT')['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'].sum()
sub_pd=sub_pd.transpose()
sub_pd.iplot(kind='line',xTitle='Months',yTitle='Rainfall (in mm)',title='Rainfall trend over all districts in Pondicherry',theme='solar')
Dataset: imported from https://www.kaggle.com/rajanand/rainfall-in-india?select=district+wise+rainfall+normal.csv
Attribution: (websites such as stackoverflow.com, medium.com etc.)