Solve the problem that Jupyter drawing cannot display Chinese under Mac

The drawing that has just been configured in the Jupyter environment will have a problem of not displaying Chinese characters. The common method of adding two lines of code on the Internet cannot solve the problem. The SimHei.ttf file must be added under the matplotlib file.

Follow these steps to resolve the issue:

1. Download the SimHei.ttf font file

First, we need to download a SimHei font from GitHub, the link is: https://github.com/dolbydu/font

Find the SimHei.ttf file under its unicode folder

2. View the font file path in Jupyter:

import matplotlib
matplotlib.matplotlib_fname()

After getting the directory,

Open Finder, Shift+Command+g, and enter the path:

Find the fonts folder and drag the downloaded SimHei.ttf file into the fonts/ttf file.

3. Modify the matplotlib file

Then return to the mpl-data folder and open the matplotlibrc file with a text editor

Modify the following:

  • font.family remove the # in front

  • font.sans-serif remove the # in front and add SimHei

  • axes.unicode_minus remove the # in front and change True to False

Save and exit after modification.

4. Clear matplotlib cache

Enter the following code in jupyter to view the cache path:

import matplotlib
matplotlib.get_cachedir()

Show cache path: /Users/username/.matplotlib

Enter the following code in jupyter to delete the cache:

rm -rf /Users/用户名/.matplotlib

Note: Some errors may still be reported after the steps are completed. This is because ipykernel is installed when configuring the Jupyter virtual environment on Mac. You only need to restart the Kernel in Jupyter.

Guess you like

Origin blog.csdn.net/h21396577548/article/details/129122439