温馨提示×

Python中怎么修改中英文字体

小亿
328
2024-05-29 09:40:12
栏目: 编程语言

在Python中,可以通过使用第三方库matplotlib来修改中英文字体。下面是一个简单的例子:

import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体 plt.rcParams['font.family'] = 'sans-serif' plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.title('示例标题', fontproperties='SimHei') # 设置标题为中文 plt.xlabel('X轴', fontproperties='SimHei') # 设置X轴标签为中文 plt.ylabel('Y轴', fontproperties='SimHei') # 设置Y轴标签为中文 plt.show() 

在上面的代码中,首先通过设置plt.rcParams['font.sans-serif']来指定中文字体为黑体(SimHei),然后在绘制图表时通过fontproperties参数指定中文的字体。这样就可以在图表中显示中文了。

0