Saturday, March 28, 2020

Sankey Diagram : power consumption by states in India

In [18]:
import pandas as pd
import numpy as np
import plotly.graph_objs as go
In [19]:
#read excel files in to pandas dataframe
file_nodes = r'D:\Python Scripts\Sankey_Power_Generation\nodes.xlsx'
In [20]:
df_nodes = pd.read_excel(file_nodes)

All values are in MW. Source of data is Wikipedia. Data is as of 31st March 2019.

In [21]:
df_nodes
Out[21]:
ID Label Colour
0 0 Thermal (MW) Coal #4994CE
1 1 Thermal (MW) Gas #4994CE
2 2 Thermal (MW) Diesel #4994CE
3 3 Thermal (MW) Sub-Total Thermal #8A5988
4 4 Nuclear (MW) #449E9E
5 5 Renewable (MW) Hydro #7FC241
6 6 Renewable (MW) Other Renewable #7FC241
7 7 Renewable (MW) Sub-Total Renewable #7FC241
8 8 Total (356,100 MW) #D3D3D3
In [22]:
#read excel files in to pandas dataframe
file_links = r'D:\Python Scripts\Sankey_Power_Generation\link.xlsx'
In [23]:
df_links = pd.read_excel(file_links)
In [24]:
df_links
Out[24]:
Source Value Target LinkColour
0 0 200704 3 rgba(127, 194, 65, 0.2)
1 1 24937 3 rgba(127, 194, 65, 0.2)
2 2 637 3 rgba(127, 194, 65, 0.2)
3 3 226279 8 rgba(211, 211, 211, 0.5)
4 4 6780 8 rgba(211, 211, 211, 0.5)
5 5 45399 7 rgba(253, 227, 212, 1)
6 6 77641 7 rgba(253, 227, 212, 1)
7 7 123040 8 rgba(211, 211, 211, 0.5)
In [ ]:
fig = go.Figure(data=[go.Sankey(node=dict(
pad=15,
thickness=20,
line=dict(color="black",width=0.5),
label=df_nodes['Label'].dropna(axis=0, how='any'),
color=df_nodes['Colour']),link=dict(
source=df_links['Source'].dropna(axis=0, how='any'),
target=df_links['Target'].dropna(axis=0, how='any'),
value=df_links['Value'].dropna(axis=0, how='any'),
color=df_links['LinkColour'].dropna(axis=0, how='any'),
))])
# print(fig)
fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()