Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a legend to my heatmap plot

I'm trying to translate datas from csv file to a heatmap. For the moment, my heatmap looks like this : enter image description here

But it miss a legend! This is my code :

import matplotlib.pyplot as plt
import numpy as np

column_labels = list(range(0,24))
row_labels = ["Lundi",
              "Mardi",
              "Mercredi",
              "Jeudi",
              "Vendredi",
              "Samedi",
              "Dimanche"]
data = np.array([
                [0,0,0,0,0,0,0,0,0,0,434,560,650,340,980,880,434,434,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,434,560,0,650,0,0,0,0,340,980,0,0,0,880,0,434,343],
                [0,0,0,0,0,0,0,0,0,0,434,560,650,340,980,880,434,434,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,434,560,0,650,0,0,0,0,340,980,0,0,0,880,0,434,343],
                [0,0,0,0,0,0,0,0,0,0,434,560,650,340,980,880,434,434,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,434,560,0,650,0,0,0,0,340,980,0,0,0,880,0,434,343],
                [0,0,0,0,0,0,0,0,0,0,434,560,650,340,980,880,434,434,0,0,0,0,0,0]
                ])
fig, axis = plt.subplots() # il me semble que c'est une bonne habitude de faire supbplots
heatmap = axis.pcolor(data, cmap=plt.cm.Blues) # heatmap contient les valeurs

axis.set_yticks(np.arange(data.shape[0])+0.5, minor=False)
axis.set_xticks(np.arange(data.shape[1])+0.5, minor=False)

axis.invert_yaxis()

axis.set_yticklabels(row_labels, minor=False)
axis.set_xticklabels(column_labels, minor=False)

fig.set_size_inches(11.03, 3.5)

plt.savefig('test.png', dpi=100)

How can I have a legend to this plot ? Like in this one : Heatmap made in javascript

Thanks a lot for your help !

like image 764
Louis Etienne Avatar asked Aug 26 '15 20:08

Louis Etienne


People also ask

Do heat maps have legends?

The legends for heatmaps are composed with a color bar, labels and titles. ComplexHeatmap automatically generates legends according to the input matrix and annotations, while also provide flexibility to customize and add new legends.

How do you add a legend to a subplot?

Create a figure and a set of subplots, using the subplots() method, considering 3 subplots. Plot the curve on all the subplots(3), with different labels, colors. To place the legend for each curve or subplot adding label. To activate label for each curve, use the legend() method.


1 Answers

It sounds like what you want is a colorbar. Once you plot your heatmap, you can use plt.colorbar(heatmap) to add a colorbar.

Heatmap with colorbar

like image 127
Amy Teegarden Avatar answered Oct 12 '22 00:10

Amy Teegarden