可以通过以下代码设置matplotlib的全局字体:

import matplotlib.pyplot as plt

# 设置字体
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['font.size'] = 12

其中,font.family表示字体的名称,可以使用系统中已安装的字体,也可以使用ttf字体文件路径。font.size表示字体大小。

此外,还可以通过以下代码设置不同元素的字体:

import matplotlib.pyplot as plt

plt.title('Title', fontdict={'family': 'Times New Roman', 'size': 14})
plt.xlabel('X Label', fontdict={'family': 'Times New Roman', 'size': 12})
plt.ylabel('Y Label', fontdict={'family': 'Times New Roman', 'size': 12})
plt.xticks(fontfamily='Times New Roman', fontsize=10)
plt.yticks(fontfamily='Times New Roman', fontsize=10)

在每个元素的fontdict参数中,可以设置familysize属性。同时,xticksyticks方法中也可以直接设置字体。

matplotlib全局字体设置

原文地址: https://www.cveoy.top/t/topic/bxi8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录