plotly

plotly.graph_objects.Heatmap

A heatmap refers to a type of plot that uses a cartesian plane where the data is represented as colored rectangular tiles.

This article will discuss creating heatmap plots using the Plotly graph_objects module.

Parameter and Syntax

To create a heatmap plot using plotly graph_objects, you can follow the following syntax:

plotly.graph_objects.Heatmap(arg=None, autocolorscale=None, colorbar=None, colorscale=None, x=None, y=None, z=None, **kwargs)

Parameter list:

  1. Arg – This defines a dictionary with compatible properties with the Heatmap constructor.
  2. autocolorscale – This sets whether the colorscale uses the default palette or determine by the colorscale parameter.
  3. colorbar – A dictionary containing compatible properties of the colorbar instance.
  4. colorscale – This determines the colorscale as an RGB, RGBA, hex, HSL, HSV, or named color string.
  5. x – This sets the values for the x coordinate.
  6. y – This defines the values for the y coordinate.
  7. z – This sets the values for the z coordinate.

Basic Heatmap Using Plotly Graph Objects

The following code shows how to create a simple heatmap plot using Plotly graph_objects:

import plotly.graph_objects as go
fig = go.Figure(data=go.Heatmap(
    z=[[1,2,3,
4,5 ,6,
7,8,9]]
))
fig.show()

The previous code should create a heatmap plot as follows:

Adding Axis Labels

We can add categorical axis labels by adding the x and y parameters, as shown in the following code:

import plotly.graph_objects as go
x = ['E1', 'E2', 'E3', 'E4', 'E5']
y = ['S1', 'S2', 'S3']
z = [[40.32, 43.33, 39.94], [40.12, 40.13, 43.12], [39.03, 40.23, 40.22]]
fig = go.Figure(data=go.Heatmap(
x=x,
y=y,
    z=z,
    hoverongaps = False
))
fig.show()

An example output figure is as follows:

Adding Text on Heatmap Points

You can add text on heatmap points by specifying the text parameter, as illustrated in the following code:

import plotly.graph_objects as go
x = ['E1', 'E2', 'E3', 'E4', 'E5']
y = ['S1', 'S2', 'S3']
z = [[40.32, 43.33, 39.94], [40.12, 40.13, 43.12], [39.03, 40.23, 40.22]]

fig = go.Figure(data=go.Heatmap(
    x=x,
    y=y,
    z=z,
    text=[[40.32, 43.33, 39.94], [40.12, 40.13, 43.12], [39.03, 40.23, 40.22]],
    texttemplate="%{text}",
    textfont={"size": 10},
    hoverongaps = False
))
fig.show()

Output figure:

Setting Colorscale

We can also specify a custom color scale by setting the colorscale parameter, as shown in the following code sample:

import plotly.graph_objects as go
import plotly.express as px
x = ['E1', 'E2', 'E3', 'E4', 'E5']
y = ['S1', 'S2', 'S3']
z = [[40.32, 43.33, 39.94], [40.12, 40.13, 43.12], [39.03, 40.23, 40.22]]

fig = go.Figure(data=go.Heatmap(
    x=x,
    y=y,
    z=z,
    text=[[40.32, 43.33, 39.94], [40.12, 40.13, 43.12], [39.03, 40.23, 40.22]],
    texttemplate="%{text}",
    textfont={"size": 10},
    hoverongaps = False,
    colorscale=px.colors.sequential.matter_r
))
fig.show()

The resulting figure is as follows:

Conclusion

This article describes creating a heatmap plot using the Plotly graph_objects module. We showed how to add axis labels, add text on heatmap points, and set colorscales.

For articles like this, check out more from Linux Hint.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list