plotly Heatmap in Python (3 Examples)
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 plotly.express 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]]) fig.show()
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") fig.show()
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"]) fig.update_xaxes(side="top") fig.show()
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:
- plotly Area Chart in Python (5 Examples)
- Custom Button in plotly Graph in Python (Example)
- Create Dropdown Menu in plotly Graph in Python (Example)
- Disable Hover Information in plotly Using Python (Example)
- Learn Python Programming
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.
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.
Statistics Globe Newsletter