画图显示中文会出现乱码,出现这种情况的原因是因为matplotlib仅支持Unicode编码,默认是不显示中文的
如果是Windows系统,加入以下一段代码就可以成功显示中文.
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
如果是Mac系统,加入下一段代码就可以成功显示中文,其中PingFang.ttc为字体文件。,这个方法同样也适合Windows系统,但是要修改一下字体文件的路径和名称。
from matplotlib.font_manager import FontManager, FontProperties
import matplotlib.pyplot as plot
def getChineseFont():
return FontProperties(fname='/System/Library/Fonts/PingFang.ttc')
if __name__ == '__main__':
plot.title("标题", fontproperties=getChineseFont())
## 让图例显示中文
plt.legend(prop = getChineseFont())
plot.show()
参考连接:https://blog.csdn.net/lixiaodaoaaa/article/details/72597852