05Legend图例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1,1,50)
y1 = 2*x+1
y2 = x**2

plt.figure()

#范围
plt.xlim((-1,2))
plt.ylim((-2,3))
#轴含义(标签)
plt.xlabel('X')
plt.ylabel('Y')
#更换下标
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],
           ['really bad','bad','normal','good','really good'])

#设置坐标轴位置
#gca = 'get current axis '
ax = plt.gca()
#删除上轴和右轴
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#更改下轴和左轴位置
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

#图列
plt.plot(x,y2,label='up')
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='down')
plt.legend(loc='best')


plt.show()

image-20230131165036676

posted @ 2023-01-31 20:32  不迷路的小孩  阅读(19)  评论(0)    收藏  举报