COVID-19 India state wise data visualization using Matplot and python

Ankit Kumar Rajpoot
5 min readMar 30, 2020

--

As we know the whole world is being affected by COVID-19 pandemic and almost everyone is working from home.COVID-19 is the name of corona-virus which is given by the World Health Organisation (WHO). We all should utilize this duration at best, to improve our technical skills or make some visualization.

Let’s see a simple Python code with Jupyter Notebook, pandas and Matplot to demonstrate the state-wise corona virus cases in India. This data fetched from Kaggle, till 29th march 2020. Then data is represented in the bar graph and Pie graph and line graph.

Data Set —

This data set got from Kaggle. Data from Jan 2020 up to 29th March 2020, 9 columns are here these like following. You can find the dataset by clicking here.

Date
Time
State/UnionTerritory
ConfirmedIndianNational
ConfirmedForeignNational
Cured
Deaths
Confirmed

Data Frame —

Data set is in CSV format so we will convert in pandas data frame through given code.

import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv(‘covid_19_india.csv’,parse_dates=[0], dayfirst=True)

Data Cleaning With Pandas —

Now we will clean the dataset and modify for our graph.

dateData = df.groupby([‘Date’])[‘ConfirmedIndianNational’,’Deaths’].sum().reset_index()
# Convert that column into a datetime datatype
dateData[‘Date’] = pd.to_datetime(dateData[‘Date’], format=’%d/%m/%y’)
#Sort by date
df_sorted = dateData.sort_values(by=”Date”,ascending=True).set_index(“Date”)
df_sorted = df_sorted.groupby([‘Date’])[‘ConfirmedIndianNational’,’Deaths’].sum().reset_index()

Visualization —

Data visualization is the act of taking information (data) and placing it into a visual context, such as a map or graph.

Data visualizations make big and small data easier for the human brain to understand, and visualization also makes it easier to detect patterns, trends, and outliers in groups of data.

Good data visualizations should place meaning into complicated datasets so that their message is clear and concise.

There are different types of graphs for visualizing the data. These are the following.

Date Vs Cases line Graph —

This graph is a line graph that shows number of positive(+ve) cases on the date. There are dates on the X-axis and the number of cases are on Y-axis.

plt.plot(df_sorted.Date,df_sorted.ConfirmedIndianNational)
plt.title("COVID-19 INDIA")
plt.xticks(rotation=60)
plt.xlabel("Date",labelpad=10)
plt.ylabel("Number of Cases",labelpad=10)
plt.show()

Date Vs Deaths line Graph —

This graph is a line graph that shows deaths due to the corona virus on the date. There are dates on the X-axis and the number of deaths are on Y-axis.

plt.plot(df_sorted.Date,df_sorted.Deaths,color=’red’)
plt.title(“COVID-19 INDIA”)
plt.xticks(rotation=60)
plt.xlabel(“Date”,labelpad=10)
plt.ylabel(“Number of Deaths”,labelpad=10)
plt.show()

States Vs Cases line Graph —

This graph is a line graph that shows the number of +ve cases in the states. There are states on the X-axis and the number of +ve cases are on Y-axis.

plt.plot(stateData[‘State/UnionTerritory’],stateData[‘ConfirmedIndianNational’])
plt.title(“COVID-19 INDIA”)
plt.xticks(rotation=90)
plt.xlabel(“State Name”)
plt.ylabel(“Number of Cases”)
plt.legend(“Cases”,loc=’center left’, bbox_to_anchor=(1, 0.5))
plt.show()

States Vs Deaths line Graph —

This graph is a line graph that shows the number of deaths in the states. There are states on the X-axis and the number of deaths are on Y-axis.

plt.plot(stateData[‘State/UnionTerritory’],stateData[‘Deaths’],color=”red”)
plt.title(“COVID-19 INDIA”)
plt.xticks(rotation=90)
plt.xlabel(“State Name”)
plt.ylabel(“Number of Deaths”)
plt.legend(“Deaths”,loc=’center left’, bbox_to_anchor=(1, 0.5))
plt.show()

States Vs Cases Bar Graph —

This graph is a bar graph that shows the number of +ve cases in the states. There are states on the X-axis and the number of +ve cases are on Y-axis.

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(stateData[‘State/UnionTerritory’],stateData[‘ConfirmedIndianNational’], color = list(‘rgbkymc’), width = 0.25)
plt.title(“COVID-19 INDIA”)
plt.xticks(rotation=90)
plt.xlabel(“State Name”)
plt.ylabel(“Number of Cases”)
plt.show()

States Vs Deaths Bar Graph —

This graph is a bar graph that shows the number of +ve cases in the states. There are states on the X-axis and the number of +ve cases are on Y-axis.

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(stateData[‘State/UnionTerritory’],stateData[‘Deaths’], color = list(‘rgbkymc’), width = 0.25)
plt.title(“COVID-19 INDIA”)
plt.xticks(rotation=90)
plt.xlabel(“State Name”)
plt.ylabel(“Number of Deaths”)
plt.show()

States Vs Cases Pie Graph —

This graph is a Pie graph that shows the percentage number of +ve cases in the states.

state = stateData[‘State/UnionTerritory’]
cases = stateData[‘ConfirmedIndianNational’]
explode = stateData.ConfirmedIndianNational.apply(lambda x:x > 100)
explode = explode.apply(lambda x:0.2 if x == True else 0)
plt.title(“COVID-19 Deaths Vs Cases”, bbox={‘facecolor’:’0.8', ‘pad’:5}).set_position([.5,1.8])
plt.pie(cases, explode=explode,autopct=’%1.2f%%’,shadow=True, radius=3)
plt.legend(state, loc=”center”,bbox_to_anchor=(2.5, 0.5))
plt.show()

States Vs Deaths Pie Graph —

This graph is a Pie graph that shows the percentage number of deaths in the states.

state = stateData[‘State/UnionTerritory’]
cases = stateData[‘Deaths’]
explode = stateData.ConfirmedIndianNational.apply(lambda x:x > 100)
explode = explode.apply(lambda x:0 if x == True else 0)
plt.title(“Covid 19”)
plt.pie(cases, explode=explode,autopct=’%1.2f%%’,shadow=True, radius=2)
plt.legend(state, loc=”center”,bbox_to_anchor=(2.5, 0.5))
plt.title(“COVID-19 Deaths Vs States”, bbox={‘facecolor’:’0.8', ‘pad’:5}).set_position([.5,1.4])
plt.show()

These all are India state-wise data or visualization. You can also analyze the world’s data.

--

--

Ankit Kumar Rajpoot
Ankit Kumar Rajpoot

Written by Ankit Kumar Rajpoot

I’m a MERN Developer. ( Redux | AWS | Python ) I enjoy taking on new things, building skills, and sharing what I’ve learned.

No responses yet