Matplotlib 默认使用的是英文字体,如果需要显示中文字体,需要手动更改字体。
在 Mac 中,可以安装中文字体,例如「SimHei」「STHeiti」等。安装完成后,在 Matplotlib 中通过设置字体名称为「SimHei」「STHeiti」等来显示中文字体。
代码示例:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定中文字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title("中文标题")
plt.xlabel("横轴")
plt.ylabel("纵轴")
plt.show()