Linux 拨号vps windows公众号手机端

matplotlib中文乱码问题怎么解决

lewis 8年前 (2017-08-16) 阅读数 11 #程序编程
文章标签 matplotlib

要解决matplotlib中文乱码问题,可以按照以下步骤进行操作:

  1. 导入matplotlib库和字体模块:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
  1. 选择合适的中文字体,可以使用系统中已经安装的中文字体,也可以下载其他中文字体。这里以SimHei字体为例:
font = FontProperties(fname="SimHei.ttf", size=12)

注意,SimHei.ttf是SimHei字体文件的路径,如果你的电脑上没有这个字体文件,可以使用其他中文字体的路径。

  1. 设置默认字体为中文字体:
plt.rcParams['font.family'] = font.get_name()
  1. 正常使用中文即可,例如:
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title('中文标题', fontproperties=font)
plt.xlabel('横轴', fontproperties=font)
plt.ylabel('纵轴', fontproperties=font)
plt.show()

这样就能够在matplotlib中正常显示中文了。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门