Home > Enterprise >  Subscript labels in heatmap (in python seaborn)
Subscript labels in heatmap (in python seaborn)

Time:04-23

I am trying to subscript my axis labels in a Python/sns heatmap; however, I don't know how to do it. Below is an example code from where I get the heatmap but labels are normal (attached figure), but I want them as subscript. For example, I would like to have Tair as air in subscript. Another example is Tair-Ts variable, where I want air and s in subscript.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

d = {'Tair': [25.55,  5.25, 1.99,  9.78], 
     'Ts': [0.45,  0.10,  0.69, 0.52],
    'Tair-Ts': [-10, -7, -8, -5]}

df = pd.DataFrame(data=d) # data frame
corr = df.corr() # correlation matrix
ax = sns.heatmap(corr, linewidth=0.5) # heatmap

I would be extremely grateful if anyone can help me to get the labels as subscript.

Thank you.

Sample Figure enter image description here

CodePudding user response:

In matplotlib, you can use dollar signs to indicate LaTeX formatting.

Your example could look like:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

d = {'Tair': [25.55, 5.25, 1.99, 9.78],
     'Ts': [0.45, 0.10, 0.69, 0.52],
     'Tair-Ts': [-10, -7, -8, -5]}

df = pd.DataFrame(data=d)
corr = df.corr()
labels = ['$T_{air}$', '$T_{s}$', '$T_{air}-T_{s}$']
ax = sns.heatmap(corr, xticklabels=labels, yticklabels=labels, linewidth=0.5)
plt.tight_layout()
plt.show()

sns.heatmap with latex labels

  • Related