plotly Heatmap in Python (3 Examples)


Hi! This tutorial will show you how to make plotly heatmaps in the Python programming language.

First, though, here is an overview:

Let’s get into the Python code!


Install & Import plotly

If you do not have plotly already installed in your Python environment, then in your preferred Python coding IDE, run the line of code below to install plotly:

pip install plotly

Having installed plotly, the next thing to do is to import plotly express like so:

import as px

Now, we have access to plotly’s plot-building functions, including the one we will make use of in this tutorial.


Example 1: Plot Basic plotly Heatmap

In this first example, we will build a basic plotly heatmap:

fig = px.imshow([[2, 20, 40],
                 [20, 2, 50],
                 [40, 50, 2]])

The px.imshow() function is used to visualize heatmaps as well as images, and it can accept array-like objects and pandas DataFrames too.

Example 2: Control Aspect Ratio on Heatmap

In this second example, we will change the heatmap’s aspect ratio, which by default comes in square tiles:

fig = px.imshow([[2, 20, 40],
                 [20, 2, 50],
                 [40, 50, 2]],aspect = "auto")

Setting the aspect = argument in the px.imshow() function to “auto” fills the plotting area with non-square tiles which cover the entire axes of the graph panel. Otherwise, the plot would be displayed as shown in Example 1.


Example 3: Customize Axes & Labels on Heatmap

In this third example, we will add labels to the heatmap, and customize its axes:

fig = px.imshow([[2, 20, 40],
                 [20, 2, 50],
                 [40, 50, 2]],
                aspect = "auto",
                labels=dict(x="Months of the Year", y="Weeks of the Month", color="Hours"),
                x=["January", "February", "March"],
                y=["First Week", "Third Week", "Second Week"])

In the above code, we created a Python dictionary containing the x and y axes titles and the title of the color legend. Then, we created lists for the x and y axes labels. Since it is a 3 x 3 grid, the label lists contain 3 elements per list.

Lastly, we used the update_xaxes() function to bring the X axis title to the top of the heatmap, by parsing “top” to the side = argument.


Video, Further Resources & Summary

Do you need more explanations on how to make plotly heatmaps in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to generate plotly heatmaps in Python.


The YouTube video will be added soon.


I hope this tutorial has helped you learn how to form plotly heatmaps in Python. If you would like to learn more about using plotly in the Python programming language, then be sure to check out other interesting plotly in Python tutorials on Statistics Globe:

This post has shown how to create plotly heatmaps (sometimes also called tile matrix plot) in Python. In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.