In this article, we will explore the method of creating the annotated heatmaps using the Plotly figure_factory module and the annotated_heatmap() function.
Let’s dive in.

Function Syntax and Parameter List

The function syntax is as shown in the following code snippet:

plotly.figure_factory.create_annotated_heatmap(z, x=None, y=None,

annotation_text=None, colorscale='Plasma', font_colors=None, showscale=False,

reversescale=False, **kwargs)

The following are some important function parameters that you will use when working with this function:

  1. z – Defines the matrix used to create the annotated heatmap.
  2. x – Sets the values for the x axis labels.
  3. y – Defines the values for the y axis labels.
  4. colorscale – Sets the colorscale for the heatmap.
  5. reverscale – Sets a reverse colorscale.

Basic Annotated Heatmap

The following code illustrates how to create a simple annotated heatmap using the figure_factory function.

from plotly.figure_factory import create_annotated_heatmap
z = z = [[.1, .3, .5, .7, .9],
     [1, .8, .6, .4, .2],
     [.2, 0, .5, .7, .9],
     [.9, .8, .4, .2, 0],
     [.3, .4, .5, .7, 1]]
fig = create_annotated_heatmap(z)

The previous code returns a heatmap as shown in the following illustration:

By default, the colorscale is disabled by default. You can enable it by setting the showscale parameter to True as shown in the following:

fig = create_annotated_heatmap(z, showscale=True)

The output figure is as shown in the following:

We can also set a custom colorscale by setting the colorscale parameter. An example is as follows:

fig = create_annotated_heatmap(z, showscale=True, colorscale='Viridis')

The resulting figure is as shown in the following illustration:


In this article, we explored how to create the annotated heatmap plots using the Plotly figure_factory module.

Thanks for reading. Happy coding!

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