Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding units to heatmap annotation in Seaborn

Tags:

I am trying to show a table of percentages as a heatmap in Seaborn:

sns.heatmap(S, annot=True, fmt=".1f", linewidths=1.0, square=1) 

However, I'd like the percent sign to appear after each number in the heatmap annotations. The fmt flag only seems to accept number format specifiers. Is there a way to do this either within Seaborn or with some matplotlib tweaking?

like image 839
Jsl Avatar asked Jun 02 '16 22:06

Jsl


People also ask

What is FMT in heatmap?

cmap: The mapping from data values to color space. center: The value at which to center the colormap when plotting divergent data. annot: If True, write the data value in each cell. fmt: String formatting code to use when adding annotations. linewidths: Width of the lines that will divide each cell.

How do I add a title to my Seaborn heatmap?

Use the set_title() Function to Add a Title to a Seaborn Plot. A seaborn plot returns a matplotlib axes instance type object. For such objects, we can use the set_title() function to add a title to the plot. We can also control the size of the title using the fontsize parameter.

What is Seaborn correlation heatmap?

Correlation Heatmap Pandas / Seaborn Code Example Method corr() is invoked on the Pandas DataFrame to determine the correlation between different variables including predictor and response variables. The Seaborn heatmap() method is used to create the heat map representing the correlation matrix.


1 Answers

You have to iterate over all text values of a heatmap and add % sign:

import matplotlib.pyplot as plt import seaborn as sns import numpy as np from matplotlib.ticker import FuncFormatter  sns.set() fig, ax0 = plt.subplots(1,1) data = np.array([[10.01,20.20],[-0.23,0.],[23.1049,-100.000000]]) ax = sns.heatmap(data, annot=True, fmt = '.1f', square=1, linewidth=1.) for t in ax.texts: t.set_text(t.get_text() + " %") plt.show() 

enter image description here

like image 81
Serenity Avatar answered Sep 24 '22 08:09

Serenity