设置方法
导入包时,加入这两句即可
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文
mpl.rcParams['axes.unicode_minus'] = False # 设置支持负号显示
案例
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文
mpl.rcParams['axes.unicode_minus'] = False # 设置支持负号显示
fig = plt.figure(figsize=(16, 8))
ax = fig.add_subplot(1, 1, 1)
x = np.linspace(-10,10,500)
y = np.sin(x**2.0) + np.abs(x)**0.5
ax.plot(x,y,label=r'函数$\mathcal{y} = \mathrm{sin(x^2)} + \sqrt{\vert x \vert} $')
ax.set_title(r'$\mathcal{y} = \mathrm{sin(x^2)} + \sqrt{\vert x \vert} $', fontsize=18)
ax.text(6, 1.2, r'函数$\mathcal{y} = \mathrm{sin(x^2)} + \sqrt{\vert x \vert} $', fontsize=16)
ax.set_xlabel(r'$\mathcal{x}$', fontsize=16)
ax.set_ylabel(r'$\mathcal{y}$', fontsize=16)
ax.legend(fontsize=14)
ax.grid()
plt.savefig('sin.png',dpi=300)
plt.show()