1 import numpy 2 from matplotlib import pyplot 3 x=numpy.linspace(-3,3,50) 4 y1=2*x+1 5 y2=x**2 6 7 8 pyplot.figure(num='第一幅圖',figsize=(5,5)) 9 pyplot.plot(x,y2) 10 pyplot.plot(x,y1,color='red',linewidth=1,linestyle='--') 11 12 13 #設定坐標軸范圍 14 pyplot.xlim((-1,2)) 15 pyplot.ylim((-2,2)) 16 17 #設定坐標軸標簽 18 pyplot.xlabel('i am x') 19 pyplot.ylabel('i am y') 20 21 #設定坐標軸刻度值 22 a=numpy.linspace(-1,2,5) 23 pyplot.xticks(a) 24 pyplot.yticks([-2,0,2],[r'$really\ bad$',r'$normal$',r'$really\ good$']) #加$可以美化字體 25 26 27 ax=pyplot.gca() #拿到坐標軸 28 ax.spines['right'].set_color('none') 29 ax.spines['top'].set_color('none') 30 ax.xaxis.set_ticks_position('bottom') #把底部線設定為x坐標 31 ax.yaxis.set_ticks_position('left') #把左邊設定為y坐標 32 ax.spines['bottom'].set_position(('data',0)) #移動x坐標軸到y軸為0的位置 33 ax.spines['left'].set_position(('data',0)) #移動y坐標軸到x坐標軸等于0的位置 34 35 36 pyplot.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124147.html
標籤:Python
