Skip to content

Using Chinese Characters in Matplotlib

date: 2019-04-21

When you try to display Chinese or other non-ascii characters in matplotlib, your characters may not be displayed properly, like following figure:

Error displaying Chinese characters in matplotlib

It is because the fonts used by matplotlib couldn’t decode the characters properly.

The Fix

To fix it, we should add the appropriate fonts and update matplotlib font cache.

  1. locate the matplotlib fonts folder:
import matplotlib

this is the location of matplotlib config file, you will get something like …/matplotlib/mpl-data/matplotlibrc

The font folder is …/matplotlib/mpl-data/fonts/ttf , put your ttf file there.

  1. Get ttf from ttc file (skip if you have ttf file already)

For macOS, the system Chinese fonts is Heiti, which is embed in a ttc file (ttc is a collection of multiple ttf files). Get the system ttc file in /System/Library/Fonts/STHeiti Medium.ttc Copy this file out and convert it to ttf. Here is an online ttc converter:

  1. Rebuild the Matplotlib Cache
import matplotlib.font_manager

restart your Jupyter / ipython kernel, then test if matplotlib can load your font or not by the ttflist function of the font manager

[f for f in matplotlib.font_manager.fontManager.ttflist if 'Heiti' in]

change ‘Heiti’ to your own font name. If you see your fonts object above. You are ready to use the new fonts.

  1. Using Appropriate Fonts in Matplotlib
matplotlib.rcParams[''] = ['Heiti TC']

This will tell matplotlib to use the specific font as default, result as follow~

map with Chinese characters enabled