1

I am exporting matplotlib.pyplot plots to .pgf files to use them in Latex, and therefore I specify the right font family and font sizes beforhand in python. While the legend is formated correctly, the axis labels show around 2pt offset to the font size, and they are not in bold, even though specified . Is it not possible to use RC parameters together with a stylesheet? Here is my code:

import matplotlib.pyplot as plt
import matplotlib.style as pltstyle
pltstyle.use('ggplot')
from matplotlib import rc
from matplotlib import rcParams
rc('font', **{'family': 'serif', 'serif': ['Latin Modern Roman']})
rc('text', usetex=True)
rc('font', size=8)
rc('font', weight='bold')
rc('text.latex', preamble=r'\usepackage{amsmath}')
testx = [1,2]
testy = [1,2]
fig, ax1 = plt.subplots()
ax1.set_xlabel('This Font is too large')
ax1.set_ylabel('And not bold')
ax1.plot(testx, testy, color='tab:red', marker='o', label='Right Font Style')
plt.legend()
plt.savefig('test.png', bbox_inches='tight')

The Resulting Image: Wrong Font Styles from RC Parameters

Can I force the plot to use the RC parameters?

Camill Trüeb
  • 348
  • 1
  • 2
  • 16
  • 2
    The font weight of the axes labels is steered by the `axes.labelweight`; the font size by `axes.labelsize`. The size of the ticklabels, `xtick.labelsize`. There is no font weight parameter for ticklabels, but in case of using latex that wouldn't work anyways, because they are rendered in math mode. So you would need to set the math mode font in the latex preamble. – ImportanceOfBeingErnest Jul 24 '19 at 15:31
  • Thank you very much! Somehow `plt.rcParams['axes.labelweight'] = 'bold` is not working, but at least the fontsize is now correct. – Camill Trüeb Jul 24 '19 at 16:46
  • 1
    That's strange; without the use of latex (`rc('text', usetex=False)`) it works as expected. Looks very much like a bug, given that it works for the legend. – ImportanceOfBeingErnest Jul 24 '19 at 16:58

1 Answers1

0

when you use the latex mode (usetex=True), and if you want to have your axis in bold, you should write:

ax1.set_xlabel(r'\textbf{This Font is too large}')

instead of

ax1.set_xlabel('This Font is too large')

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Thomas
  • 1