使用matplotlib.pyplot
画图,有中文字体会显示乱码问题,这时需要添加如下代码:
import matplotlib.pyplot as plt
...
plt.rcParams["font.sans-serif"] = ["SimHei"]
...
但是那之后,如果是macOs系统,又会出现另一个问题:findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
import matplotlib
matplotlib.matplotlib_fname()
结果如下:venv/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
/Users/chenbinhao/PycharmProjects/learnPython/venv/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc
将下载好的文件放到/Users/chenbinhao/PycharmProjects/learnPython/venv/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/
路径下,如图:
import matplotlib
matplotlib.get_cachedir()
>>> import matplotlib
>>> matplotlib.get_cachedir()
'/Users/chenbinhao/.matplotlib'
rm -rf /Users/chenbinhao/.matplotlib
matplotlibrc
文件# 去掉前面的#号
font.family: sans-serif
# 去掉前面的#号,并在:号后面加上SimHei
font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# 去掉前面的#号,并将True改为False
axes.unicode_minus: False