一、Matplotlib画图
要求: Matplotlib画图的默认字体是ejaVu Sans,现在需要将字体修改为Times New Roman。
问题: Matplotlib直接加载字体不能识别,需要把字体Times New Roman添加到 Matplotlib 的字体管理器中。
二、读取txt,画图
1. 下载字体文件——Times New Roman.ttf
可以直接下载:hv88
2. 代码
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
# 字体文件路径
font_path = './Times New Roman.ttf' # 请根据实际路径进行修改
# 添加自定义字体
fm.fontManager.addfont(font_path)
# 设置全局字体:通过 plt.rcParams 设置全局字体,指定 font.family 为使用的字形名称。
plt.rcParams['font.family'] = fm.FontProperties(fname=font_path).get_name()