import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,50) #生成50个元素,均匀分布在(-3,3)区间
y1 = x*2 + 1
y2 = x**2
plt.figure()
plt.plot(x,y2,color=‘blue’)
plt.plot(x,y1,color=‘red’,linewidth=1.0,linestyle=’–’)
#在区间内显示两条线
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel(‘I am x’)
plt.ylabel(‘I am y’)
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,0,1,3,], #字符之间对应
[r’ r e a l l y b a d really\ bad really bad’,r’ b a d bad bad’,r’just’,r’good’,r’ r e a l l y g o o d really\ 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’)#X轴作为底部边框
ax.yaxis.set_ticks_position(‘left’)#Y轴作为左边边框
ax.spines[‘bottom’].set_position((‘data’,0)) #底部(x),以0开始
ax.spines[‘left’].set_position((‘data’,0)) #左边同样
plt.show()
l1, = plt.plot(x,y2,color=‘blue’,label=‘up’)
l2, = plt.plot(x,y1,color=‘red’,linewidth=1.0,linestyle=’–’,label=‘down’)
plt.legend(handles=[l1,l2],labels=[‘james’,‘kobe’],loc=‘best’)