import numpy as np
import matplotlib.pyplot as plt
1.一些常规操作
x = np.linspace(-3,3,50)
y1 =2*x+1
y2 = x*x
plt.figure(num=1)
l1,=plt.plot(x,y2,label='up')
l2,=plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='down')#其实也可以直接写plt.plot,但是为了后面演示添加legend,就增加了l1 l2 ;l1,l2后面那个逗号一定要加
plt.xlim(-1,2)
plt.ylim(-2,3)
plt.xlabel('i am x')
plt.ylabel('i am y')
new_xticks = np.linspace(-1,2,5)
new_yticks =[r'$really\ bad$',r'$bad\ \alpha$',r'$normal$',r'$good$',r'$really\ good$']
plt.xticks(new_xticks)
plt.yticks([-2,-1.8,-1,1.22,3],new_yticks)#移动坐标轴#gca abbr for 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))
ax.spines['left'].set_position(('data',0))# data表示定位的方式是具体数值# 此外还可以填'axes',后面的数值表示坐标轴在x(或y)的范围的百分之多少# 比如ax.spines['bottom'].set_position(('axes',0.8))就表示把x轴设置在y整体范围的80%处#设置legend
plt.legend(handles=[l2,],labels=['aaa'],loc='lower right')#只给l2设置图例,如果不指定,就从第一条线开始添加图例#设置标注# method1
x0 =0.5
y0 =2*x0+1
plt.scatter(x0,y0,s=50,color='b')
plt.plot([x0,x0],[0,y0],'k--',lw=2.5)#画出那根黑色的虚线
plt.annotate(r'$2x+1=3$',
xycoords='data',#用data定位标注的点,即下一行xy是多少,那么标注的点就算是那个
xy=(x0,y0),#标注的是哪个点
textcoords='offset points',#标注语句的定位方式:offset points:Offset (in points) from the *xy* value;offset pixels:Offset (in pixels) from the *xy* value
xytext=(30,-30),#标注语句相对于标注点的偏移量
fontsize=16,#标注语句的字号
arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))#标注点与标注语句之间连接线的样式# method2
plt.text(-1,2,'a simple pic',fontdict={
'size':16,'color':'purple'})
plt.show()
2.设置坐标轴标签的可见程度
x = np.linspace(-3,3,50)
y =0.1*x
plt.figure()
plt.plot(x, y, linewidth=10, zorder=1)# set zorder for ordering the plot in plt 2.0.2 or higher
plt.ylim(-2,2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0