Home > Blockchain >  Seaborn a lower subscript with f-string
Seaborn a lower subscript with f-string

Time:05-03

I'd like to plot a figure with lower subscript xticks using f-string not only one-digit case but also two-digit cases like L_1, L_2, ... L_10,L_11,... .

How could I correct my code?

import matplotlib.pylab as plt
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set_theme()
import numpy as np

fig, ax = plt.subplots(figsize=(12, 10))
cmap = sns.diverging_palette(0, 230, 90, 60, as_cmap=True)
# plot heatmap
connectivity = np.zeros((14,14))
sns.heatmap(connectivity, annot=True, fmt=".2f",
           linewidths=5, cmap=cmap, vmax = 1, vmin = -1, cbar_kws={"shrink": .8}, square=True)

label = [f"$L_{i}$" for i in [1,2,3,4,5,7,9,10,11,12,13,14,15,16]]

plt.yticks(plt.yticks()[0], labels=label, rotation=0)
plt.xticks(plt.xticks()[0], labels=label)

plt.show()

enter image description here

CodePudding user response:

The LaTeX subscript command only applies to one character unless you wrap it in curly brackets.

As you have an f-string you need to escape literal curly brackets.

This gives:

label = [f"$L_{{{i}}}$" for i in [1,2,3,4,5,7,9,10,11,12,13,14,15,16]]

Output:

enter image description here

  • Related