Skip to content

Latest commit

 

History

History
75 lines (49 loc) · 2.48 KB

module-matplotlib.md

File metadata and controls

75 lines (49 loc) · 2.48 KB

matplotlib

Troubleshooting Chinese characters on MAC

This notebook gives detailed steps in troubleshooting the Chinese characters on MAC. You will learn:

  • Where are the fonts stored on your system
  • How to filter out the font files that include CJK characters
  • How to load a font file into matplotlib. Note that ttc does not work for some version of matplotlib. ttf works.

How to display Chinese characters when using matplotlib

Step 1: Download the SimHei font here

Step 2: Find font folder in mpl-data folder

import matplotlib
print(matplotlib.matplotlib_fname())
#/Users/xuyucan/Desktop/Chicoxyc/venv/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc

!open /Users/xuyucan/Desktop/Chicoxyc/venv/lib/python3.6/site-packages/matplotlib/mpl-data

Step 3: Enter the font folder, put the font you just download into the folder,then click to install

font folder

Step 4: Back to mpl-data folder, click matplotlibrc command+f search font family, delete #; search font.sans-serif,delete #, and add SimHei,; search axes.unicode_minus, replace True to False.

Matplotlib add font

Step 5: Delete the cache

import matplotlib as mpl
mpl.get_cachedir() #dind the path of cache
!open /Users/xuyucan/.matplotlib
#delete files in this folder

Step 6: Restart the Jupyter notebook

  1. comman+c in terminal, enter y
  2. type jupyter notebook again

Step 7: add following two lines into your codes

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

There is a similar issue about Chinese characters (CJK characters) when you use wordcloud. Please refer to this section for the solution.

Characters overlapping in text marker

When plotting the charts, you may encounter problem like characters overlapping. One solution is to change the figure size to enlarge the text margin.

fig = plt.figure(figsize=(7,7))

character overlapping

After resizing by change the figsize

fig = plt.figure(figsize=(15,7))

Resize the charts