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()
运行之后结果如下,我们成功的看到了中文的正确显示。