標籤:.text ott 產生 oat point def nump center ace
散佈圖
import matplotlib.pyplot as pltimport numpy as np#n個點n = 1024#平均值是0,方差是1X = np.random.normal(0,1,n)Y = np.random.normal(0,1,n)#確定顏色T = np.arctan2(Y,X)plt.scatter(X,Y,s=75,c=T,alpha=0.5)#plt.scatter(np.arange(5),np.arange(5))plt.xlim(-1.5,1.5)plt.ylim(-1.5,1.5)#隱藏所有的ticksplt.xticks(())plt.yticks(())plt.show()
柱狀圖:
import matplotlib.pyplot as pltimport numpy as np#12個柱狀圖n=12X = np.arange(n)#x會產生0到11Y1 = (1-X/float(n))*np.random.uniform(0.5, 1.0, n)#隨機隨機產生0.5到1的數Y2 = (1-X/float(n))*np.random.uniform(0.5, 1.0, n)plt.bar(X,+Y1,facecolor=‘#9999ff‘,edgecolor=‘white‘)plt.bar(X,-Y2,facecolor=‘#ff9999‘,edgecolor=‘white‘)#zip是把X,Y1中的值分別給x和y#plt.text(x位置,y位置,值)for x,y in zip(X,Y1): #ha:horizontal alignment對齊 plt.text(x,y+0.05,‘%.2f‘ % y,ha=‘center‘,va=‘bottom‘) for x,y in zip(X,Y2): #ha:horizontal alignment對齊 plt.text(x,-y-0.05,‘-%.2f‘ % y,ha=‘center‘,va=‘top‘) plt.xlim(-.5,n)plt.xticks(())plt.ylim(-1.25,1.25)plt.yticks(())plt.show()
等高線
import matplotlib.pyplot as pltimport numpy as np#通過x,y計算高度def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)n=256x=np.linspace(-3,3,n)y=np.linspace(-3,3,n)#把x,y綁定成網格的輸入值X,Y = np.meshgrid(x,y)# use plt.contourf to filling contours#X,Y and value for (X,Y) point#contour為網格#8代表分成10部分#0分成2部分#plt.cm.cool為冷色調,plt.cm.hot為暖色調,plt.cm.Spectral,plt.cm.hsv,plt.cm.oceanplt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hsv)#畫上顏色#use plt.contour to add contounlines#畫線,contour為等高線的線C = plt.contour(X,Y,f(X,Y),8,colors=‘black‘,linwidth=.5)#adding labelplt.clabel(C,inline=True,fontsize=10) plt.xticks(())plt.yticks(())plt.show()
本知識學於 莫煩python
python 可視化 散佈圖。柱狀圖、等高線