Annotated Heatmaps using Plotly in Python


Annotated heatmaps are a great way to visualize data in a matrix format, where each cell is color-coded based on its value. Plotly is a powerful Python library that can be used to create interactive and customizable visualizations, including annotated heatmaps.

Here's an example of how to create an annotated heatmap using Plotly in Python:

import plotly.graph_objs as go
import numpy as np

# Create a random matrix
matrix = np.random.rand(10, 10)

# Define the x and y axis labels
x_labels = ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5', 'Label 6', 'Label 7', 'Label 8', 'Label 9', 'Label 10']
y_labels = ['Label A', 'Label B', 'Label C', 'Label D', 'Label E', 'Label F', 'Label G', 'Label H', 'Label I', 'Label J']

# Create the heatmap trace
heatmap = go.Heatmap(
    z=matrix,
    x=x_labels,
    y=y_labels,
    colorscale='Viridis'
)

# Define the layout
layout = go.Layout(
    title='Annotated Heatmap',
    xaxis=dict(title='X Axis Label'),
    yaxis=dict(title='Y Axis Label')
)

# Create the figure
fig = go.Figure(data=[heatmap], layout=layout)

# Add annotations to the heatmap
annotations = []
for i in range(len(y_labels)):
    for j in range(len(x_labels)):
        annotations.append(
            dict(
                x=x_labels[j],
                y=y_labels[i],
                text=str(matrix[i][j]),
                showarrow=False,
                font=dict(color='white')
            )
        )

fig.update_layout(annotations=annotations)

# Show the figure
fig.show()

In this example, we first create a random matrix using NumPy. We then define the x and y axis labels for the heatmap. Next, we create the heatmap trace using the go.Heatmap function and specify the matrix, x labels, y labels, and colorscale. We also define the layout for the figure, including the title and axis labels.

To add annotations to the heatmap, we create an empty list called annotations and loop through each cell in the matrix. For each cell, we create a dictionary containing the x and y coordinates, the text to display (which is the value of the cell), and some formatting options. We then append this dictionary to the annotations list.

Finally, we update the layout of the figure to include the annotations using the fig.update_layout function. We set the annotations parameter to the annotations list we created earlier.

When we run this code, we should see an interactive heatmap with annotations displayed for each cell.

Another way to create an annotated heatmap using Plotly is to use the annotated_heatmap function from the plotly.figure_factory module. Here's an example:

import plotly.figure_factory as ff
import numpy as np

# Create a random matrix
matrix = np.random.rand(10, 10)

# Define the x and y axis labels
x_labels = ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5', 'Label 6', 'Label 7', 'Label 8', 'Label 9', 'Label 10']
y_labels = ['Label A', 'Label B', 'Label C', 'Label D', 'Label E', 'Label F', 'Label G', 'Label H', 'Label I', 'Label J']

# Create the annotated heatmap
fig = ff.create_annotated_heatmap(
    z=matrix,
    x=x_labels,
    y=y_labels,
    colorscale='Viridis'
)

# Define the layout
fig.update_layout(
    title='Annotated Heatmap',
    xaxis=dict(title='X Axis Label'),
    yaxis=dict(title='Y Axis Label')
)

# Show the figure
fig.show()

In this example, we first create a random matrix and define the x and y axis labels. We then create the annotated heatmap using the ff.create_annotated_heatmap function and specify the matrix, x labels, y labels, and colorscale.

We then update the layout of the figure to include the title and axis labels using the fig.update_layout function.

When we run this code, we should see an interactive annotated heatmap displayed.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.