import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1,y2=np.sin(x),np.cos(x)
# plt.plot(x, y1, x, y2)
# plt.xlabel('x轴')
# plt.ylabel('y轴')
# 方二
fig=plt.figure()
ax=fig.add_axes([0, 0, 1, 1,])
ax.plot(x, y1, x, y2)
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
#设置刻度
plt.xlim(x.min()*1.5, x.max()*1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
n=np.linspace(-5, 4, 30)
m1=3 * n + 2
m2=n ** 2
plt.xlabel('时间')
plt.ylabel('心情')
line1 = plt.plot(n, m1, color='r', linewidth=1.5, linestyle='-', label='女生购物欲望')
line2 = plt.plot(n, m2, 'b', label='男生购物欲望')
#plt.legend(handles=[line1, line2], labels=['girl购物欲望', 'boy购物欲望'], loc='best')
plt.legend()
plt.show()
给legend赋值时不知道为啥报错,解决了改正