Matplotlib Greek characters differ in show () and savefig ()

I am trying to get a simple Greek letter mu to display in Roman font in a saved shape with matplotlib. I tried two methods:

  • plt.xlabel(u'Wavelength (\u03bc m)')

This method works fine when I do show() , but when I try to use savefig() , the mu character is displayed as a rectangle when saved as .png. If I save as .pdf, the character is completely missing.

  1. plt.xlabel(r'Wavelength ($\mathrm{\mu}$m)')

This method displays the Greek letter with both show() and savefig() , but the character is still italicized in each case, despite the Roman font request.

What trick?

+5
source share
1 answer

I have very good experience working with all the text (plain and mathematical) that LaTeX is typing. Just set your rc settings accordingly before doing the following:

 import matplotlib.pyplot as plt plt.rcParams['text.usetex'] = True #Let TeX do the typsetting plt.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}',r'\sansmath'] #Force sans-serif math mode plt.rcParams['font.family'] = 'sans-serif' # ... for regular text plt.rcParams['font.sans-serif'] = 'Helvetica' # Choose a nice font here 

You can simply say:

 plt.xlabel('Wavelength ($\mu$)') 

Inspired by my own answer here: Type 1 fonts with log charts

+1
source

Source: https://habr.com/ru/post/1205207/


All Articles