1.查看缓存目录
import matplotlib as plt
print(plt.get_cachedir())
清楚缓存:rm -rf /Users/***/.matplotlib
python里的matplotlib包的字体库:print(matplotlib.matplotlib_fname())
把系统字体文件:(/System/Library/Fonts/Hiragino\ Sans\ GB.ttc)
将系统字体复制到python字体库(不要复制,请找到自己系统目录):
cp /System/Library/Fonts/Hiragino\ Sans\ GB.ttc /Users/**/anaconda3/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf
方式一:代码上直接引用
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] =['Hiragino Sans GB']
plt.rcParams['axes.unicode_minus']=False
方式二(临时使用):可以引用系统字体库,上面的不需要拷贝到python字体库中
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font= FontProperties(fname='/System/Library/Fonts/Hiragino Sans GB.ttc', size=14)
#下面是任意有中文代码使用的地方:
plt.ylabel('Y轴', fontsize=15, fontproperties=font)