This post extends the post on Heatmap in matplotlib and seaborn. Heatmap is a data visualization method of presenting data points as a matrix of colours whose intensity is relative to the sizes of values. They show a relationship between two variables with colour showing the strength of the relationship. Heatmaps are good at providing insights from complex data. The magnitude of the data points are represented by the strength of the colour. Heatmaps comes in different types such as biological heatmaps, treemaps, mossaic plots and density function plots. In this post we will look at Heatmaps, when to use them and how to create Heatmaps in plotly. The data for this post can be downloaded here.

plotly-logo

When to Use Heatmap

  1. Heatmaps are primarily used to visualize the relationship between two variables with color denoting the strength of the relationship.
  2. Heatmaps are heavily used in web analytics to visualize the user behaviour and interaction in the website. By getting data of the most clicked places and less clicked website places we can visualize the data in a heatmap and understand website usage.

When to Use Heatmap

  1. Select the colour palette that best represents the data points and blends well with other components.
  2. Display values in each cell to give a relative view for comparison.
  3. Add legend where possible

Heatmaps in Plotly

Import Required Libraries

                    

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

Load Data

                    

ex_rate_df=pd.read_csv('Exchange_Rates.csv')
heatmap_ex_rate_df= pd.crosstab(ex_rate_df['TIME'],ex_rate_df['LOCATION'],values=ex_rate_df['Value'],aggfunc='mean')

heatmap_ex_rate_df.iloc[0:,0:13]

plotly-heatmap-load-data

Heatmap with imshow in Plotly

                    

heatmap_data = np.random.random(( 10 , 10 ))

fig = px.imshow(heatmap_data)

fig.update_layout(title={'text': 'Random Numbers Heatmap with imshow','y':0.95,'x':0.45, 'xanchor': 'center','yanchor': 'top'},
                          legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
                  autosize=True,margin=dict(t=70,b=0,l=0,r=0),
                  font=dict(size=20, family='Times New Romans', color='brown'),
                 width=900, height=600)

fig.show()

plotly-heatmap-with-imshow

Heatmap with Plotly Graph Object

                    

fig=go.Figure()
fig.add_trace(go.Heatmap(z=heatmap_ex_rate_df.iloc[0:5,0:5],
                        x=heatmap_ex_rate_df.iloc[0:5,0:5].columns,
                        y=heatmap_ex_rate_df.iloc[0:5,0:5].index))

fig.update_layout(title={'text': 'Exchange Rate Heatmap','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
                          legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
                  autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='Currency', yaxis_title='Year',
                  font=dict(size=20, family='Times New Romans', color='brown'),
                 width=900, height=600)

fig.show()

plotly-heatmap-with-plotly-graph-object

Annotated Heatmap

                    

import plotly.figure_factory as ff
heatmap_data = np.random.random(( 10 , 10 ))

fig=ff.create_annotated_heatmap(heatmap_data.round(2))

fig.update_layout(title={'text': 'Random Numbers Heatmap with Figure Factory','y':1.0,'x':0.5,
                         'xanchor': 'center','yanchor': 'top'},
                          legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
                  autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='',
                  font=dict(size=20, family='Times New Romans', color='brown'),
                 width=900, height=600)

fig.show()

plotly-annotated-heatmap-with-figure-factory

Annotated Heatmap with Pandas DataFrame

                    

z = heatmap_ex_rate_df.iloc[0:5,0:5].round(2).values.tolist()
x = heatmap_ex_rate_df.iloc[0:5,0:5].columns.tolist()
y = heatmap_ex_rate_df.iloc[0:5,0:5].index.tolist()

fig=ff.create_annotated_heatmap(z,x=x,y=y, annotation_text=z)
fig.update_layout(title={'text': 'Exchange Rate Heatmap','y':1.0,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
                          legend=dict(yanchor="top",y=0.95,xanchor="right",x=0.95),
                  autosize=True,margin=dict(t=70,b=0,l=0,r=0), xaxis_title='', yaxis_title='Year',
                  font=dict(size=20, family='Times New Romans', color='brown'),
                 width=900, height=600)

fig.show()

plotly-annotated-heatmap-with-figure-factory-and-pandas-df-data

For complete code check the jupyter notebook here.

Conclusion

In this post we have seen at Heatmaps, when to use them and how to create them in plotly. Heatmaps are important kind of visualizations used to depict relationship between two variables. The data points are presented by colour of varying intensity relative to the magnitude of the values. Heatmaps are heavily used in web analytics to analyze engagements and user behaviour in the websites. In the next post we will look at Tree Maps and how to use them in ploty. To learn about Distribution plots in plotly and how to use it check our previous post here. To learn about heatmap in seaborn check our post here.

Heatmaps in Plotly

Post navigation


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x