把图中所有的字体(包括坐标轴、图例、注释)统一设置为 Times New Roman(新罗马字体),
✅ 方法一:通过 matplotlib.rcParams
全局设置字体
在程序的最上方(如你设置中文字体那块附近),加入如下配置即可:
# 设置字体为 Times New Roman(适用于英文/公式风格)
matplotlib.rcParams['font.family'] = 'Times New Roman'
⚠️ 注意:这将覆盖之前设置的中文字体(SimHei),所以如果图中还包含中文,就需要做字体兼容或使用备用字体。
✅ 方法二:同时支持中英文(推荐方案)
由于 Times New Roman
不支持中文,你可以设置英文字体为 Times New Roman,中文字体为 SimHei 或思源宋体等,如下所示:
matplotlib.rcParams['font.family'] = ['Times New Roman', 'SimHei'] # 英文优先用 Times,中文 fallback 为黑体
matplotlib.rcParams['axes.unicode_minus'] = False
这样,图中的 ReLU(x)
、Leaky ReLU(x)
会用 Times New Roman,激活函数
、输入
、输出
等中文则自动 fallback 为 SimHei,兼容显示。
✅ 最终整合代码示意(片段)
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# 设置字体样式
matplotlib.rcParams['font.family'] = ['Times New Roman', 'SimHei'] # 同时支持英文字体和中文
matplotlib.rcParams['axes.unicode_minus'] = False