Annotated heatmap
It is often desirable to show data which depends on two independent variables as a color coded image plot. This is often referred to as a heatmap. If the data is categorical, this would be called a categorical heatmap.
Matplotlib's imshow function makes
production of such plots particularly easy.
The following examples show how to create a heatmap with annotations. We will start with an easy example and expand it to be usable as a universal function.
A simple categorical heatmap
We may start by defining some data. What we need is a 2D list or array
which defines the data to color code. We then also need two lists or arrays
of categories; of course the number of elements in those lists
need to match the data along the respective axes.
The heatmap itself is an imshow plot
with the labels set to the categories we have.
Note that it is important to set both, the tick locations
() as well as the
tick labels (),
otherwise they would become out of sync. The locations are just
the ascending integer numbers, while the ticklabels are the labels to show.
Finally, we can label the data itself by creating a
within each cell showing the value of that cell.

