3DRX

// post

Using Matplotlib to Produce Figures in Paper

Some configurations and environment setup

Updated

In academic paper, figures are required to use certain fonts, a common choice is Arial. However, on macOS and Linux, Matplotlib does not use the preferred configurations by default. We need to manually configure it.

Install font

Solving: findfont: Font family [‘Arial’] not found

Use fc-match Arial to check if the font is already installed.

If Arial is not installed, use sudo apt install ttf-mscorefonts-installer to install it. Then sudo fc-cache -f -v to refresh cache.

You may also need to clear Matplotlib cache by rm ~/.cache/matplotlib/fontlist-v300.json

Using the font in plots

import matplotlib.pyplot as plt
import matplotlib

plt.rcParams['font.family'] = 'Arial'
plt.rcParams['font.sans-serif'] = ['Arial']
plt.rcParams['font.serif'] = ['Arial']

# Default Type 3 fonts are not suitable in some cases
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42

Export as PDF

plt.savefig('./figure_name.pdf', bbox_inches='tight', dpi=300)