Next Level Interactive Visualizations for Data Science

In this article, I will show you, the most advanced interactive visualizations techniques for Data Science, using python, plotly and bar chart race.

I will use the Electricity power consumption Data set for this tutorial. I will analyse the power consumption of according to the states in India using Interactive Visualizations.

An Interactive EDA of Electricity Consumption

Power is one of the most critical components of infrastructure crucial for the economic growth and welfare of nations. The existence and development of adequate infrastructure is essential for sustained growth of the Indian economy.

India is the world’s third largest producer and third largest consumer of electricity. Sustained economic growth continues to drive electricity demand in India.

Consumption of electricity is known to follow economic activity closely. The industries that produce essential goods are operating at very low utilization levels. Hence, in such a scenario one expects electricity demands to go down.

Download the data sets

Lets start with Importing the libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib notebook
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
from IPython.display import HTML
import calendar
from plotly.subplots import make_subplots
import bar_chart_race as bcr

Read the data sets

df = pd.read_csv('power data.csv')
df_long = pd.read_csv('long_data_.csv')

Let’s see some insights of the data

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 210 entries, 0 to 209
Data columns (total 35 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   Date               210 non-null    object 
 1   Punjab             210 non-null    float64
 2   Haryana            210 non-null    float64
 3   Rajasthan          210 non-null    float64
 4   Delhi              210 non-null    float64
 5   UP                 210 non-null    float64
 6   Uttarakhand        210 non-null    float64
 7   HP                 210 non-null    float64
 8   J&K                210 non-null    float64
 9   Chandigarh         210 non-null    float64
 10  Chhattisgarh       210 non-null    float64
 11  Gujarat            210 non-null    float64
 12  MP                 210 non-null    float64
 13  Maharashtra        210 non-null    float64
 14  Goa                210 non-null    float64
 15  DNH                210 non-null    float64
 16  Andhra Pradesh     210 non-null    float64
 17  Telangana          210 non-null    float64
 18  Karnataka          210 non-null    float64
 19  Kerala             210 non-null    float64
 20  Tamil Nadu         210 non-null    float64
 21  Pondy              210 non-null    float64
 22  Bihar              210 non-null    float64
 23  Jharkhand          210 non-null    float64
 24  Odisha             210 non-null    float64
 25  West Bengal        210 non-null    float64
 26  Sikkim             210 non-null    float64
 27  Arunachal Pradesh  210 non-null    float64
 28  Assam              210 non-null    float64
 29  Manipur            210 non-null    float64
 30  Meghalaya          210 non-null    float64
 31  Mizoram            210 non-null    float64
 32  Nagaland           210 non-null    float64
 33  Tripura            210 non-null    float64
 34  ALL INDIA TOTAL    210 non-null    float64
dtypes: float64(34), object(1)
memory usage: 57.5+ KB
df['Date'] = pd.to_datetime(df.Date, dayfirst=True)
df_long['Dates'] = pd.to_datetime(df_long.Dates, dayfirst=True)

Region Wise Daily Power Consumption

df['NR'] = df['Punjab']+ df['Haryana']+ df['Rajasthan']+ df['Delhi']+df['UP']+df['Uttarakhand']+df['HP']+df['J&K']+df['Chandigarh']

df['WR'] = df['Chhattisgarh']+df['Gujarat']+df['MP']+df['Maharashtra']+df['Goa']+df['DNH']

df['SR'] = df['Andhra Pradesh']+df['Telangana']+df['Karnataka']+df['Kerala']+df['Tamil Nadu']+df['Pondy']

df['ER'] = df['Bihar']+df['Jharkhand']+ df['Odisha']+df['West Bengal']+df['Sikkim']

df['NER'] =df['Arunachal Pradesh']+df['Assam']+df['Manipur']+df['Meghalaya']+df['Mizoram']+df['Nagaland']+df['Tripura']
fig = go.Figure()

fig.add_trace(go.Scatter(
    x=df.Date, y=df.NR,
    mode='lines+markers',
    name='Northern region',
    marker=dict(
            color='rgba(300, 50, 50, 0.8)',
            size=5,
            line=dict(
                color='DarkSlateGrey',
                width = 1
                     )
                )
))

fig.add_trace(go.Scatter(
    x=df.Date, y=df.SR,
    mode='lines+markers',
    name='Southern Region',
    marker=dict(
            color='rgba(50, 300, 50, 0.8)',
            size=5,
            line=dict(
                color='DarkSlateGrey',
                width = 1
                     )
                )
))

fig.add_trace(go.Scatter(
    x=df.Date, y=df.ER,
    mode='lines+markers',
    name='Eastern Region',
    marker=dict(
            color='rgba(50, 50, 300, 0.8)',
            size=5,
            line=dict(
                color='DarkSlateGrey',
                width = 1
                     )
                )
))

fig.add_trace(go.Scatter(
    x=df.Date, y=df.WR,
    mode='lines+markers',
    name='Western Region',
    marker=dict(
            color='rgba(300, 100, 200, 0.8)',
            size=5,
            line=dict(
                color='DarkSlateGrey',
                width = 1
                     )
                )
))

fig.add_trace(go.Scatter(
    x=df.Date, y=df.NER,
    mode='lines+markers',
    name='North-Eastern',
    marker=dict(
            color='rgba(100, 200, 300, 0.8)',
            size=5,
            line=dict(
                color='DarkSlateGrey',
                width = 1
                     )
                )
))


fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month", stepmode="backward"),
            dict(count=3, label="3m", step="month", stepmode="backward"),
            dict(count=6, label="6m", step="month", stepmode="backward"),
            dict(step="all")
        ])
    )
)

fig.update_layout(title='Power Consumption in Various Region')
fig.update_layout(width=800,height=500)
fig.show()

State-wise mean Power consumption

df1= df[['Date', 'Punjab', 'Haryana', 'Rajasthan', 'Delhi', 'UP',
       'Uttarakhand', 'HP', 'J&K', 'Chandigarh', 'Chhattisgarh', 'Gujarat',
       'MP', 'Maharashtra', 'Goa', 'DNH', 
       'Andhra Pradesh', 'Telangana', 'Karnataka', 'Kerala', 'Tamil Nadu',
       'Pondy', 'Bihar', 'Jharkhand', 'Odisha', 'West Bengal', 'Sikkim',
       'Arunachal Pradesh', 'Assam', 'Manipur', 'Meghalaya', 'Mizoram',
       'Nagaland', 'Tripura']]

df1 = df1.set_index('Date')
bcr.bar_chart_race(df1, figsize=(4, 3.5),period_length =500,filename = None, title='power usage by states')
monthly_df = df_long.groupby([df_long.Dates.dt.year, df_long.Dates.dt.month,df_long.States,df_long.Regions, df_long.latitude,df_long.longitude])['Usage'].mean()
monthly_df.index = monthly_df.index.set_names(['year', 'month','State','Region','latitude','longitude'])
monthly_df = monthly_df.reset_index()
monthly_df['month'] = monthly_df['month'].apply(lambda x: calendar.month_abbr[x])
fig = px.sunburst(monthly_df, path=['Region', 'State','month'], values='Usage',
                  color='Usage',
                  color_continuous_scale='RdBu')
fig.update_layout(title='Click various Regions/States to view power distribution')
fig.update_layout( width=800,height=600)
fig.show()
fig = px.bar(monthly_df, x="Region", y="Usage",color='State',animation_frame = 'month')
fig.update_layout(xaxis={'categoryorder':'total descending'})
fig.update_layout(title='Region-wise Bar plots')
fig.show()

Before and After lock down Scenarios

df_before = df.iloc[0:150,:]
df_after = df.iloc[151:,]

fig = go.Figure()
fig.add_trace(go.Scatter( x=df_before['Date'], y=df_before['Gujarat'], name='Gujarat before lockdown',fill='tonexty',
    line=dict(width=2,dash='dot',color='firebrick') 
))
fig.add_trace(go.Scatter( x=df_before['Date'], y=df_before['Maharashtra'], name='Maharashtra before lockdown',fill='tonexty',
    line=dict(width=2,dash='dot',color='coral')
))

fig.add_trace(go.Scatter( x=df_before['Date'], y=df_before['MP'], name='MP before lockdown',fill='tozeroy',
    line=dict(width=2,dash='dot',color='darkred')
))

fig.add_trace(go.Scatter(x=df_after['Date'], y=df_after['Gujarat'],name='Gujarat after lockdown',fill='tozeroy',
    line=dict(color='firebrick', width=2)
))

fig.add_trace(go.Scatter(x=df_after['Date'], y=df_after['Maharashtra'],name='Maharashtra after lockdown',fill='tozeroy',
    line=dict(color='coral', width=2)
))

fig.add_trace(go.Scatter(x=df_after['Date'], y=df_after['MP'],name='MP after lockdown',fill='tozeroy',
    line=dict(color='darkred', width=2)
))

fig.update_layout(title='Power Consumption in top 3 WR states')
fig.update_layout( width=800,height=500)
fig.show()
Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1536

Leave a Reply