温馨提示×

Python中怎么修改中英文字体

小亿
260
2024-05-29 09:40:12
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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参数指定中文的字体。这样就可以在图表中显示中文了。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:python如何随机生成英文字符串

0