Annoted Heatmaps using Plotpy in Python


Introduction

A heat map, is a graphical representation of data in which each value in a matrix is assigned a unique colour. The primary purpose of heat maps is to increase the accuracy of the visualization of the total number of places and events included within a dataset, as well as the accuracy with which they direct viewers' attention to the most important aspects of data visualizations.

Heat maps, which rely on colour to communicate values, are most commonly used to offer a more holistic picture of numerical values. This is due to the fact that heat maps rely on colour to express values. The discipline of analytics has seen an uptick in the utilisation of heat maps in recent years due, in large part, to the adaptability and efficiency with which they bring to light recurring trends.

Plotpy

The Plotly is a library for the Python programming language that can be used to create graphs, particularly interactive graphs. It is able to generate many different graphs and charts, among which are a histogram, barplot, boxplot, and spread plot. Most of its applications are concentrated within the fields of data analysis and financial analytics. Users are able to construct interactive visualizations with the help of plotly, which is a library. The plotly Python library is a plotting toolkit that is interactive and open-source. It supports over 40 distinct chart types. There is a wide variety of work that can be done with these sorts of charts, including statistical, financial, geographical, scientific, and even three-dimensional work.

Users of Python can build interactive web-based visualizations via the usage of plotly, which was built on top of the Plotly JavaScript library (plotly.js). These visualizations can be seen in Jupyter notebooks, saved as standalone HTML files, or provided as part of Python-only web applications using Dash. All three of these options are available. plotly is the company that created plotly. To distinguish itself from the plotly JavaScript library, the plotly Python library is often known to as "plotly.py." This is done in order to avoid confusion with the plotly JavaScript library.

Heat maps

Heat maps are, by their very nature, self-explanatory. The quantity increases proportionally with the depth of color. The Heatmap() function can be found in the Plotly application's graph objects module. It is required that x, y, and z qualities be present. Their value can be a list, a numpy array, or a Pandas data frame. All three options are valid.

A heatmap is a type of data visualisation that is two-dimensional and displays information by assigning a distinct colour to each number included inside a matrix.

The Seaborn package makes it possible to create annotated heatmaps, which can then be customised using the tools provided by Matplotlib in accordance with the requirements of the developer.

Code Example for Heatmaps

import plotly.figure_factory as ff
import numpy as npp
import seaborn as sbn

xt = [0,1,3,5,6,7,8,9,11]
yt = [2,5,11,7,8,3,5,12,3]
  
[Xx, Yy] = npp.meshgrid(xt, yt)
  
Zz = (Xx / 2) + (Yy / 4)
    
fig_plot = sbn.heatmap(Zz)
fig_plot.show()

Output

Annoted Heatmaps

Because they reveal additional information linked with rows or columns, annotated heatmaps are the most essential component of a heatmap. The annotation heatmaps will be shown as rows of grids, which will make it possible to compare a great number of measurements with one another.

Syntax

create_annotated_heatmap(z, x=None, annotation_text=None,colorscale=’Plasma’, font_colors=None, showscale=False, reversescale=False)

Parameter explanation

x − here x is the x-axis label.

y − here y is the y-axis label.

z − z matrix for heatmap generation.

annotation text − consists of text strings used for annotations. The dimensions must match those of the z matrix. If no text is supplied, the z matrix's values are annotated.

Default − z matrix values.

Colour scale − heatmap colour scale.

Code example for Annoted heatmaps

import plotly.figure_factory as ff
import numpy as npp
  
xt = [0,1,3,5,6,7,8,9,11]
yt = [2,5,11,7,8,3,5,12,3]
  
[Xx, Yy] = npp.meshgrid(xt, yt)
  
Zz = (Xx / 2) + (Yy / 4)
    
fig_plot = ff.create_annotated_heatmap(Zz)
fig_plot.show()

Output

How to define colour scale in heatmap?

In plotly, a colorscale or colour chart is a flat physical object containing various hues. It can be set using the colorscale option in this graph.

Code Example

import plotly.figure_factory as pf
import numpy as npp
xt = [0,1,3,5,6,7,8,9,11]
yt = [2,5,11,7,8,3,5,12,3]
  
[Xx, Yy] = npp.meshgrid(xt, yt)
  
Zz = (Xx / 2) + (Yy / 4)
    
fig_plot = ff.create_annotated_heatmap(Zz, colorscale='rainbow')
fig_plot.show()  

Output

Adding custom colour scale in heatmap

import plotly.figure_factory as ff
import numpy as npp
xt = [0,1,3,5,6,7,8,9,11]

yt= [2,5,11,7,8,3,5,12,3]
[Xx, Yy] = npp.meshgrid(xt, yt)
  
Zz = (Xx / 2) + (Yy / 4)
  
custom_col = [[0.2, 'pink'], [0.3, 'blue']]
  
fig_plot = ff.create_annotated_heatmap(Zz, colorscale=custom_col)
fig_plot.show()

Output

Conclusion

In this article, we looked at how to use the Plotly figure factory module to make annotated heatmap plots.

Updated on: 24-Aug-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements