1

Is it possible to create ticklabels with different fontweights within the same label? In the MWE below I'd like to have the first line of each xticklabel appear in bold font and the second in normal. I'm glad for any advise.

import matplotlib.pyplot as plt

plt.plot(range(3), marker='o', lw=0)
plt.gca().set_xticks(range(3))
plt.gca().set_xticklabels(['\n'.join(['bold', 'normal'])] * 3)  # , fontdict={'weight': 'bold'})
plt.show()

The first line of each xlabeltick should appear in bold font

Lukas
  • 407
  • 1
  • 4
  • 21

1 Answers1

1

You can use LaTex for text in matplotlib (docs)

import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
# plt.rc('text.latex', preamble=r'\usepackage{sfmath}')  # use sans-serif text

plt.plot(range(3), marker='o', lw=0)
plt.gca().set_xticks(range(3))
plt.gca().set_xticklabels(['$\\textbf{bold}$ \n normal']*3)
plt.show()

enter image description here

scleronomic
  • 4,392
  • 1
  • 13
  • 43
  • Is it possible to keep the font-type and -size of the original plot when using this? (for consistency across different figures) – Lukas Jun 08 '20 at 08:40
  • 1
    Have a look at [this](https://stackoverflow.com/questions/11611374/sans-serif-font-for-axes-tick-labels-with-latex) and [this](https://stackoverflow.com/questions/11367736/matplotlib-consistent-font-using-latex) questions. I edited my answer; if you uncomment the added line you should be closer to your desired result – scleronomic Jun 08 '20 at 09:43