(以下展示均爲先圖片後代碼的形式。)
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> x = np.linspace(-1.,1.,50)
>>> y = x**2 + 1
>>> plt.plot(x,y)
>>> plt.show()
—————————————————————————————
多窗口(多函數)
>>> plt.figure('first window')
<matplotlib.figure.Figure object at 0x11e8320b8>
>>> y1 = np.sqrt(-x**2 + 1)
>>> plt.plot(x,y1)
>>> plt.figure('second window')
<matplotlib.figure.Figure object at 0x11e9da630>
>>> y2 = np.sqrt(x**2 + 1)
>>> y3 = x**3 + 1
>>> plt.plot(x,y2)
>>> plt.plot(x,y3)
>>> plt.show()
—————————————————————————————
更多例子
>>> plt.plot(x,y1,'g-',x,y2,'b--',x,y3,'r.')
>>> plt.show()
—————————————————————————————
>>> plt.figure('graph1')
<matplotlib.figure.Figure object at 0x11ea74588>
>>> plt.xlabel('x軸')
<matplotlib.text.Text object at 0x11e9333c8>
>>> plt.ylabel('y軸')
<matplotlib.text.Text object at 0x11e9bb0f0>
>>> y = 2**x - 3
>>> plt.plot(x,y,'r*')
>>> plt.show()
—————————————————————————————
>>> plt.xlim((-1,1)) #設置座標軸初次顯示的範圍
(-1,1)
>>> plt.ylim((1,2))
(1,2)
>>> plt.xlabel('i\'m x.') #設置座標軸名稱
<matplotlib.text.Text object at 0x1174b23c8>
>>> plt.ylabel('i\'m y.')
<matplotlib.text.Text object at 0x11fa0b6d8>
>>> plt.xticks([-1,-0.5,0,0.5,1]) #設置x的顯示步長
([,,,,],)
>>> plt.yticks([1,1.2,2],['bad','normal','good'])
([,,],)
>>> y1 = x + 1.5
>>> y2 = x**2 + 1
>>> plt.plot(x,y1,'go',x,y2,'b--')
>>> plt.show()
—————————————————————————————
>>> plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--',label='function1')
>>> plt.plot(x,y2,color='green',linewidth=1.0,linestyle='-',label='function2')
>>> plt.legend(loc='lower right') # loc可以爲‘best’:自動找一個數據少的地方顯示
<matplotlib.legend.Legend object at 0x11e829b00>
>>> plt.show()
—————————————————————————————
>>> x = np.random.randint(1,50,size=(50,))
>>> y1 = np.random.randint(1,50,size=(50,))
>>> y2 = np.random.randint(1,50,size=(50,))
>>> plt.xticks(())
>>> plt.yticks(())
>>> plt.scatter(x,y1,c='red') #畫散點圖
<matplotlib.collections.PathCollection object at 0x11ec37b00>
>>> plt.scatter(x,y2,c='blue')
<matplotlib.collections.PathCollection object at 0x11e8fc0b8>
>>> plt.show()
—————————————————————————————
### ‘圖像更新’
plt.ion() # 打開交互模式
plt.plot(x,y) #顯示圖像
plt.pause(0.01) # 暫停功能
plt.clf() # 清除當前的Figure對象
plt.cla() # 清除當前的Axes對象
plt.ioff() #關閉交互模式
以上功能在for循環中可實現圖像(x,y)更新
—————————————————————————————
>>> x=np.array(np.arange(1,2,0.01))
>>> y=np.array(np.arange(1,2,0.01))
>>> xx,yy = np.meshgrid(x,y)#將x、y分別往縱向和橫向擴展成2d數組
>>> xx
array([[1. , 1.01, 1.02,..., 1.97, 1.98, 1.99],
[1. , 1.01, 1.02,..., 1.97, 1.98, 1.99],
[1. , 1.01, 1.02,..., 1.97, 1.98, 1.99],
...,
[1. , 1.01, 1.02,..., 1.97, 1.98, 1.99],
[1. , 1.01, 1.02,..., 1.97, 1.98, 1.99],
[1. , 1.01, 1.02,..., 1.97, 1.98, 1.99]])
>>> yy
array([[1. , 1. , 1. ,..., 1. , 1. , 1. ],
[1.01, 1.01, 1.01,..., 1.01, 1.01, 1.01],
[1.02, 1.02, 1.02,..., 1.02, 1.02, 1.02],
...,
[1.97, 1.97, 1.97,..., 1.97, 1.97, 1.97],
[1.98, 1.98, 1.98,..., 1.98, 1.98, 1.98],
[1.99, 1.99, 1.99,..., 1.99, 1.99, 1.99]])
>>> z=xx*yy < 2 #定義第三維的高度:等於1 or 0
>>> plt.contourf(xx,yy,z,alpha=0.4)#創建等高圖對象
<matplotlib.contour.QuadContourSet object at 0x11e75abe0>
>>> plt.show()