转载自 https://blog.csdn.net/baidu_38963740/article/details/123839178

python中用matplotlib库画图时,把中文设置为宋体,英文设置为Time New Roman,有时候还需要显示公式。设置方法如下:

 1 import matplotlib.pyplot as plt
 2 from matplotlib import rcParams
 3 import numpy as np
 4 
 5 config = {
 6     "font.family":'serif',
 7     "font.size": 18,
 8     "mathtext.fontset":'stix',
 9     "font.serif": ['SimSun'],
10 }
11 rcParams.update(config)
12 
13 x = np.random.random((10,))
14 
15 
16 plt.plot(x,label='随机数')
17 plt.title('中文:宋体 \n 英文:$\mathrm{Times \; New \; Roman}$ \n 公式: $\\alpha_i + \\beta_i = \\gamma^k$')
18 plt.xlabel('横坐标')
19 plt.ylabel('纵坐标')
20 plt.legend()
21 plt.yticks(fontproperties='Times New Roman', size=18)
22 plt.xticks(fontproperties='Times New Roman', size=18)
23 plt.show()

null