Python matplotlib: change axis labels / legend from bold to normal weight

I am trying to make some publications, but I ran into a little problem. Apparently, by default, matplotlib label marks and legend records are weighted heavier than axis markers. In any case, in order to make the legend axis / legend labels equal in weight with the tick marks?

import matplotlib.pyplot as plt import numpy as np plt.rc('text',usetex=True) font = {'family':'serif','size':16} plt.rc('font',**font) plt.rc('legend',**{'fontsize':14}) x = np.linspace(0,2*np.pi,100) y = np.sin(x) fig = plt.figure(figsize=(5,5)) p1, = plt.plot(x,y) p2, = plt.plot(x,x**2) plt.xlabel('x-Axis') plt.ylabel('y-Axis') plt.legend([p1,p2],['Sin(x)','x$^2$']) plt.gcf().subplots_adjust(left=0.2) plt.gcf().subplots_adjust(bottom=0.15) plt.savefig('Test.eps',bbox_inches='tight',format='eps') plt.show() 

I can use math mode, but a problem (annoyance) occurs when I have a suggestion for a label, i.e.

 plt.xlabel('$\mathrm{This is the x-axis}$') 

which compresses it all together. I can fix it using

 plt.xlabel('$\mathrm{This\: is\: the\: x-axis}$') 

but this requires many punctuation marks. I was hoping there was something that I could change that would allow me to bypass the \mathrm{} format and use the standard TeX format.

Another option I tried used \text instead of \mathrm , but it seems like the Python interpreter won't recognize this without loading the amsmath package. I also tried:

 import matplotlib import matplotlib.pyplot as plt import numpy as np plt.rc('text',usetex=True) font = {'family':'serif','size':16} plt.rc('font',**font) plt.rc('legend',**{'fontsize':14}) matplotlib.rcParams['text.latex.preamble']=[r'\usepackage{amsmath}'] x = np.linspace(0,2*np.pi,100) y = np.sin(x) fig = plt.figure(figsize=(5,5)) p1, = plt.plot(x,y) p2, = plt.plot(x,x**2) plt.xlabel(r'$\text{this is the x-Axis}$') plt.ylabel('$y-Axis$') plt.legend([p1,p2],['Sin(x)','x$^2$']) plt.gcf().subplots_adjust(left=0.2) plt.gcf().subplots_adjust(bottom=0.15) plt.savefig('Test.eps',bbox_inches='tight',format='eps') plt.show() 

It also does not return the desired result.

+7
source share
2 answers

Another answer provides a workaround. However, the problem is very specific to matplotlib and the implementation of the LateX backend.

First of all, the rc parameter, which controls the font weight of the axis labels, is equal to 'axes.labelweight' , which defaults to u'normal' . This means that the marks should be already at normal weight.

The reason the font looks bold can be found in matplotlib/texmanager.py :

  • The font family is selected by 'font.family' , in the case of OP it is serif .

  • Then the array 'font.<font.family>' (here: font.serif ) is font.serif , and the declaration of all fonts is added to the LateX preamble. Among these ads is a line

     \renewcommand{\rmdefault}{pnc} 

    which sets the default value for the New Century School Book, which appears to be a bold version of Computer Modern Roman .

In conclusion, the shortest way to solve the problem is to install font.serif only on "Modern Computer":

 font = {'family':'serif','size':16, 'serif': ['computer modern roman']} 

This has the added benefit that it works for all elements, even for tags and other titles - without dirty tricks using the math mode: enter image description here


Here is the complete code for generating the chart:

 import matplotlib import matplotlib.pyplot as plt import numpy as np plt.rc('text',usetex=True) #font = {'family':'serif','size':16} font = {'family':'serif','size':16, 'serif': ['computer modern roman']} plt.rc('font',**font) plt.rc('legend',**{'fontsize':14}) matplotlib.rcParams['text.latex.preamble']=[r'\usepackage{amsmath}'] x = np.linspace(0,2*np.pi,100) y = np.sin(x) fig = plt.figure(figsize=(5,5)) p1, = plt.plot(x,y) p2, = plt.plot(x,x**2) plt.xlabel(r'$\text{this is the x-Axis}$') plt.ylabel('$y-Axis$') plt.legend([p1,p2],['Sin(x)','x$^2$']) plt.gcf().subplots_adjust(left=0.2) plt.gcf().subplots_adjust(bottom=0.15) plt.savefig('Test.eps',bbox_inches='tight',format='eps') plt.show() 
+5
source

What about:

enter image description here

 import matplotlib.pyplot as plt import numpy as np plt.rc('text',usetex=True) font = {'family':'serif','size':16} plt.rc('font',**font) plt.rc('legend',**{'fontsize':14}) x = np.linspace(0,2*np.pi,100) y = np.sin(x) fig = plt.figure(figsize=(5,5)) p1, = plt.plot(x,y) p2, = plt.plot(x,x**2) plt.xlabel('$\mathrm{This is the }x\mathrm{-axis}$'.replace(' ','\: ')) plt.ylabel('$y\mathrm{-axis}$') plt.legend([p1,p2],['$\sin(x)$','$x^2$'], loc='best') fig.subplots_adjust(left=0.2, bottom=0.15) plt.savefig('Test.eps',bbox_inches='tight',format='eps') plt.show() 

In this case, used

 plt.xlabel('$\mathrm{This is the }x\mathrm{-axis}$'.replace(' ','\: ')) 

replace spaces with '\: ' . However, TeX gurus may object to this. You can ask TeX stackexchange if there is a better way.

+5
source

All Articles