如何让matplotlib显示中文
要让matplotlib显示中文,需要以下步骤:
- 安装中文字体
首先,需要在操作系统中安装中文字体。可以在网上下载并安装,也可以使用系统自带的中文字体。
- 导入字体库
在matplotlib中,需要导入字体库,才能使用中文字体。可以使用以下代码导入字体库:
import matplotlib.font_manager as font_manager
font_manager.fontManager.addfont('path/to/font.ttf')
其中,path/to/font.ttf是你安装的中文字体文件的路径。
- 设置字体
接下来,需要设置使用的字体。可以使用以下代码设置:
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
其中,SimHei是你安装的中文字体的名称。如果使用其他中文字体,需要将SimHei替换成对应的字体名称。
- 使用中文
完成上述步骤后,就可以在matplotlib中使用中文了,例如:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [10, 20, 30, 40])
plt.title('中文标题')
plt.xlabel('中文X轴')
plt.ylabel('中文Y轴')
plt.show()
``
原文地址: http://www.cveoy.top/t/topic/cxDH 著作权归作者所有。请勿转载和采集!