python中的matplotlib(1)
調(diào)用matplotlib畫圖的流程:
add_subplot()
返回一個(gè)axes對(duì)象,里面的參數(shù)abc表示在一個(gè)figure窗口中,有a行b列個(gè)小窗口,然后本次plot在第c個(gè)窗口中
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()結(jié)果:
如果一塊畫布中要顯示多個(gè)圖:
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()結(jié)果:
畫散點(diǎn)圖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)
繪制散點(diǎn)圖時(shí),其中x和y是相同長(zhǎng)度的數(shù)組:
其中散點(diǎn)的形狀參數(shù)marker如下:
其中顏色參數(shù)c如下:
b—(blue) g–(green) k—(blace) y–(yellow)
c—(cyan) m–(magenta) r–(red) w–(white )
顯示標(biāo)題,坐標(biāo)軸,和圖標(biāo):
from numpy import * import matplotlib.pyplot as plt #產(chǎn)生測(cè)試數(shù)據(jù) x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設(shè)置標(biāo)題 ax1.set_title('Scatter Plot') #設(shè)置X軸標(biāo)簽 plt.xlabel('X') #設(shè)置Y軸標(biāo)簽 plt.ylabel('Y') #畫散點(diǎn)圖 ax1.scatter(x,y,c = 'r',marker = 'o') #設(shè)置圖標(biāo) plt.legend('y') #顯示所畫的圖 plt.show()標(biāo)記不同大小:
from numpy import * import matplotlib.pyplot as plt #產(chǎn)生測(cè)試數(shù)據(jù) x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設(shè)置標(biāo)題 ax1.set_title('Scatter Plot') #設(shè)置X軸標(biāo)簽 plt.xlabel('X') #設(shè)置Y軸標(biāo)簽 plt.ylabel('Y') # .......................... #畫散點(diǎn)圖 sValue = x*10 ax1.scatter(x,y,s=sValue,c='r',marker='x') #設(shè)置圖標(biāo) plt.legend('x1') #顯示所畫的圖 plt.show()標(biāo)記不同顏色:
from numpy import * #import operator # 運(yùn)算符模塊,執(zhí)行排序操作時(shí)將用到 import matplotlib.pyplot as plt #產(chǎn)生測(cè)試數(shù)據(jù) x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #設(shè)置標(biāo)題 ax1.set_title('Scatter Plot') #設(shè)置X軸標(biāo)簽 plt.xlabel('X') #設(shè)置Y軸標(biāo)簽 plt.ylabel('Y') #畫散點(diǎn)圖 cValue = ['r','y','g','b','r','y','g','b','r'] ax1.scatter(x,y,c=cValue,marker='s') #設(shè)置圖標(biāo) plt.legend('x1') #顯示所畫的圖 plt.show()線寬linewidths
from numpy import * import matplotlib.pyplot as plt #產(chǎn)生測(cè)試數(shù)據(jù) 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) #設(shè)置標(biāo)題 ax1.set_title('Scatter Plot') #設(shè)置X軸標(biāo)簽 plt.xlabel('X') #設(shè)置Y軸標(biāo)簽 plt.ylabel('Y') #畫散點(diǎn)圖,其中c=z表示有1,2,3種顏色,s=100表示固定大小為100 ax1.scatter(x,y,c=z,s=100,marker='o') #設(shè)置圖標(biāo) plt.legend('x1') #顯示所畫的圖 plt.show()當(dāng)然也可以讓其圖標(biāo)大小和顏色隨樣本的屬性而變化:
from numpy import * import matplotlib.pyplot as plt #產(chǎn)生測(cè)試數(shù)據(jù) 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) #設(shè)置標(biāo)題 ax1.set_title('Scatter Plot') #設(shè)置X軸標(biāo)簽 plt.xlabel('X') #設(shè)置Y軸標(biāo)簽 plt.ylabel('Y') #畫散點(diǎn)圖 #ax1.scatter(x,y,c=z,s=100,marker='o') ax1.scatter(x,y,c=x,s=50*x,marker='o') #設(shè)置圖標(biāo) plt.legend('x1') #顯示所畫的圖 plt.show()matplotlib的matplotlib.pyplot
在機(jī)器學(xué)習(xí)的決策樹中要繪制樹形圖,會(huì)用到pyplot函數(shù)
效果如下:
其他的以后用到了再添加。。。。。
參考:
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
總結(jié)
以上是生活随笔為你收集整理的python中的matplotlib(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比亚迪冠军版和海豹22有什么区别?
- 下一篇: 全新荣威RX9有哪些独特之处,有人了解吗