Step By Step
下载字符集:SimHei.tff到fonts目录
## 查看文件位置
import matplotlib
matplotlib.matplotlib_fname()
- 输出
'/home/pai/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc'
修改配置文件:matplotlibrc(249、400行取消注释;257行取消注释并添加SimHei)
249行: font.family: sans-serif
257行:font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif,SimHei
400行:axes.unicode_minus: False
修改后需要更新以下缓存,并重启kernel
from matplotlib.font_manager import _rebuild
_rebuild()
测试
import numpy as np
import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = ['sans-serif'] # 用来正常显示中文标签,黑体的 name 为 SimHei
plt.rcParams['font.size'] = 16 # 设置字体大小
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
# plt.rcParams["font.sans-serif"] = "SimHei"
# plt.rcParams["axes.unicode_minus"] = False
plt.rcParams["figure.figsize"] = (10.0, 4.0) # 设置图片尺寸
plt.ylim(0, 400000)
x = np.array(['20220817', '20220818', '20220819', '20220820', '20220821', '20220822', '20220823', '20220824'])
y = np.array([371474, 329045, 328917, 329708, 327453, 328251, 327969, 327334])
plt.title("整体实例数变化趋势")
plt.xlabel("日期")
plt.ylabel("总实例数目")
plt.plot(x, y, color = 'g',linewidth = 3) # 画图
plt.grid(color = 'r', linestyle = '--', linewidth = 0.5)
plt.savefig('./total_count_change.png')
plt.show()
更多参考
解决Linux环境下Jupyter中matplotlib中文乱码问题
解决ubuntu中jupyter中文及"-"乱码显示问题