python中的matplotlib(1)
調用matplotlib畫圖的流程:
add_subplot()
返回一個axes對象,里面的參數abc表示在一個figure窗口中,有a行b列個小窗口,然后本次plot在第c個窗口中
from numpy import * import matplotlib.pyplot as plt x=arange(0,10,0.1) # [ 0. 0.1 0.2 ..., 9.7 9.8 9.9] print(len(x)) y=random.randn(len(x)) fig=plt.figure() ax=fig.add_subplot(111) plt.plot(x,y) ax.set_title('random numbers') plt.show()結果:
如果一塊畫布中要顯示多個圖:
from numpy import * import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(2,1,1) ax.plot(x,y) ax = fig.add_subplot(2,2,3) ax.plot(x,y) plt.show()結果:
畫散點圖scatter
matplotlib.pyplot.scatter(x,y,s=20,c=’b’,marker=’o’,cmap=None,norm=None,vmin=None,
vmax=None,linewidths=None,verts=None,hold=None,**kwargs)
繪制散點圖時,其中x和y是相同長度的數組:
其中散點的形狀參數marker如下:
其中顏色參數c如下:
b—(blue) g–(green) k—(blace) y–(yellow)
c—(cyan) m–(magenta) r–(red) w–(white )
顯示標題,坐標軸,和圖標:
from numpy import * import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 ax1.scatter(x,y,c = 'r',marker = 'o') #設置圖標 plt.legend('y') #顯示所畫的圖 plt.show()標記不同大小:
from numpy import * import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') # .......................... #畫散點圖 sValue = x*10 ax1.scatter(x,y,s=sValue,c='r',marker='x') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show()標記不同顏色:
from numpy import * #import operator # 運算符模塊,執行排序操作時將用到 import matplotlib.pyplot as plt #產生測試數據 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 cValue = ['r','y','g','b','r','y','g','b','r'] ax1.scatter(x,y,c=cValue,marker='s') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show()線寬linewidths
from numpy import * import matplotlib.pyplot as plt #產生測試數據 x = arange(1,10) y = x z=[1,1,1,2,2,2,3,3,3] print(z) fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖,其中c=z表示有1,2,3種顏色,s=100表示固定大小為100 ax1.scatter(x,y,c=z,s=100,marker='o') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show()當然也可以讓其圖標大小和顏色隨樣本的屬性而變化:
from numpy import * import matplotlib.pyplot as plt #產生測試數據 x = arange(1,10) y = x z=[1,1,1,2,2,2,3,3,3] print(z) fig = plt.figure() ax1 = fig.add_subplot(111) #設置標題 ax1.set_title('Scatter Plot') #設置X軸標簽 plt.xlabel('X') #設置Y軸標簽 plt.ylabel('Y') #畫散點圖 #ax1.scatter(x,y,c=z,s=100,marker='o') ax1.scatter(x,y,c=x,s=50*x,marker='o') #設置圖標 plt.legend('x1') #顯示所畫的圖 plt.show()matplotlib的matplotlib.pyplot
在機器學習的決策樹中要繪制樹形圖,會用到pyplot函數
效果如下:
其他的以后用到了再添加。。。。。
參考:
http://blog.csdn.net/pipisorry/article/details/40005163
http://www.cnblogs.com/bovine/archive/2012/11/09/2763374.html
http://blog.csdn.net/anneqiqi/article/details/64125186
總結
以上是生活随笔為你收集整理的python中的matplotlib(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比亚迪冠军版和海豹22有什么区别?
- 下一篇: 全新荣威RX9有哪些独特之处,有人了解吗