用rcParam参数解决matplot中的中文乱码的问题

python是老外发明的,所以对中文的支持需要额外的代码来进行设置。否则,会出现以下的乱码:
在这里插入图片描述
我们需要使用rcParam参数来设置中文字体,比如仿宋,这样就可以让matplot正确显示中文了。具体的代码如下:

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['fangsong']
plt.rcParams['axes.unicode_minus'] = False
x_axis = [1,2,3,4,5]
y_axis = [3,4,5,6,7]
plt.grid()
plt.title("图1 一次函数'y=x+2'的散点图")
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(x_axis,y_axis)
plt.show()

运行之后结果如下,我们成功的看到了中文的正确显示。
在这里插入图片描述

发布了152 篇原创文章 · 获赞 6 · 访问量 4025

猜你喜欢

转载自blog.csdn.net/weixin_41855010/article/details/104439542