1.基本圖形
散點圖
plt.scatter(x,y,s=20,c='b',marker='')
折線圖
plt.plot(x,y)
plt.plot_date(date,y,'-') ? ?plt的plot_date默認(rèn)畫的的是散點
條形圖
plt.bar(left=, height= , color='blue',width=0.8) 常用位置與默認(rèn)參數(shù)
plt.barh(left=0,bottom=,height=)
示例:
index=np.arange(4)
s_a=[1,2,3,4]
s_b=[2,3,4,5]
bar_width=0.3
并列圖形
plt.bar( index, s_a,?bar_width, color='b' )
plt.bar(index+bar_width , ?s_b , ?bar_width , color='r' )
層疊:
plt.bar(index,a,wd)
plt.bar(index,b,wd,color='r',bottom=a)
直方圖
單變量直方圖:y軸表示頻率
plt.hist(x,bins=10,clolor='blue',normed=True) ? normed標(biāo)準(zhǔn)化,不顯示個數(shù)顯示頻率
雙變量直方圖,xy表示維度,顏色亮度表示分布
餅狀圖
plt.axes(aspect=1) ?將x與y軸的比例調(diào)為1,不為1則餅狀圖的圖形有壓縮
plt.pie(x= , labels=labels,autopct='%0.f%%' , explode=[ ] , shadow=True) ?
參數(shù): x為數(shù)據(jù),labels為標(biāo)簽 ?autopet顯示各區(qū)域的半分比 explode為與x同長度,將圖形突出顯示 ?shadow顯示陰影
示例:
plt.axes(aspect=1)
X=[1,2,3,4]
labels=['a','b','c','d']
explode=[0,0.05,0.08,0]
plt.pie(X,labels=labels,explode=explode,autopct='%.2f')
箱型圖
顯示數(shù)據(jù)分散情況
上邊緣,上四分位數(shù),中位數(shù),下四分位數(shù),下邊緣,異常值
data=np.random.normal(size=1000,loc=0,scale=1)
plt.boxplot(data,sym='o',whis=1.5) ?sym調(diào)整異常值點的形狀,
whis調(diào)整上下邊緣長度,為比例值,whis越大,邊緣越長
示例2:箱型圖對比
data=np.random.normal(size=(1000,4))
lables=['a','b','c','d']
plt.boxplot(data,lables=lables)
2.顏色和樣式
基本顏色
內(nèi)建顏色
b(blue) ?g(grenn) ?r(red) ?c(cyan) ?m(magenta) ?y(yellow) ?k(boack) ?w(white)
其他顏色:灰色 RGB 十六進(jìn)制
y=np.arange(5)
plt.plot(y,color='r')
plt.plot(y+1,color='0.5') ? ?灰度
plt.plot(y+2,color='#ff00ff') ?
plt.plot(y+3,color=(0.1,0.2,0.3))
點的形狀
marker 顯示指定marker時有線段,隱式時,按位置指定時只有形狀
點的形狀有多種,不同形狀默認(rèn)使用不同顏色
. , o v < > 1 2 3 4 8 s ?p * h H + x D d
線的形狀
- 實線 ? ?-- 虛線 ? ?-. 點劃線 ?:點線
樣式字符串 顏色 點形 線形 字符串表示'
3.面向?qū)ο?/h2>
多圖與子圖
FigureCanvas ?Figure Axes 三個對象
fig=plt.figure() ? ? ? ? ?畫布對象,每個figure對象是一個圖
ax1=fig.add_subplot(2,2,1) ? ? 子圖,設(shè)定規(guī)格和位置
ax1.plot(x,y) ? 在子圖上繪制圖形
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
ax4=fig.add_subplot(2,2,4)
4.格式
4.1網(wǎng)格
ax.grid(color='r', linestyle=' ') ?設(shè)置網(wǎng)格效果,可以改顏色和線性
4.2圖例
plt.plot(x,x,label='figneme')
plt.plot(x,x**2, label='secordname')
plt.legend() ? ? ? ?顯示設(shè)置的圖例,位置默認(rèn)best
plt.legend( [ 'a', 'b' ?] , loc=[0~10] , ncol=2) ? 位置參數(shù) loc ,用數(shù)字代替位置,0是自適應(yīng),ncol設(shè)置圖例的列數(shù),圖例很多時將圖例扁平化換
4.2.1設(shè)置圖例的方法
方法1:繪圖時設(shè)置
plt.plot(x,x,label='name')?
ax.plot(x,x,label='b') ? 在繪圖時用label參數(shù)設(shè)置圖例名稱,用該方法時必須用plt.legend ()和ax.legend()將圖例顯示出力
方法2:legend()方法設(shè)置
plt.legend([ ]) 和 ax.legend([ ]) ?傳入列表參數(shù)即為設(shè)置圖例,同時設(shè)置位置和列數(shù)
4.3坐標(biāo)軸范圍
plt.axis() ?顯示坐標(biāo)軸范圍
plt.axis([ -10, 10 ?, 0 ,10 ?]) 可以直接傳入列表參數(shù)設(shè)置坐標(biāo)軸范圍?
plt.xlim() 顯示x的范圍 ,傳入列表參數(shù)為設(shè)置,plt.ylim([ 0,100]) ,也可以用ymin 和ymax 指定單邊
plt.xlim([0,10]) 或 plt.xlim(xmin=0,xmax=10) 相同效果
ax.set_xlim() 和 ax.set_ylim()為對應(yīng)的面向?qū)ο蟮姆绞皆O(shè)置坐標(biāo)軸范圍
4.4坐標(biāo)軸刻度
ax=plt.gca() ? ?plt.gca()函數(shù)用來獲取當(dāng)前圖形的坐標(biāo)軸
ax.locator_params(nbins=20) ?可以指定軸 plt.locator_params('x' , nbins=5)?
4.5 副坐標(biāo)軸
方法1:plt方式
plt.plot() ?
plt.twinx() 添加一個副坐標(biāo)軸,默認(rèn)0到1
plt.plot(x,y) 默認(rèn)的圖形對應(yīng)副坐標(biāo)軸
方法2:面向?qū)ο蠓绞?/p>
x=np.arange(2,20)
y1=x*x
y2=np.log(x)
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(x,y1)
ax1.set_ylabel('y1')
ax1.set_xlabel('y1 vs? y2')
ax1.set_ylim([0,400])
ax2=ax1.twinx()
ax2.plot(x,y2)
ax2.set_ylabel('y2')
plt.show()
類似twinx() 方法,也有twiny()方法,共享y軸,x軸上下不同
4.6 注釋
4.6.1 annotate() 注釋
plt.annotate('this is zhusi', xy=(0,2), xytext=(0,10),arrowprops=dict(facecolor='r', headlength=5 ,headwidth=5, width=2 ) ) ?注釋函數(shù)?
.annotate()參數(shù),第一個str類型參數(shù),為注釋文本內(nèi)容,xy參數(shù)為元祖,設(shè)置注釋所在坐標(biāo)位置,xytext參數(shù)為元祖,設(shè)置注釋文本所在內(nèi)容,arrowprops為顯示注釋圖形,dict()為箭頭,包含多個參數(shù),常用的 facecolor設(shè)置注釋顏色,headlength設(shè)置箭頭長度,headwidth設(shè)置箭頭寬度,width設(shè)置箭尾寬度
x=np.linspace(-10,10,100)
y=x*x
plt.plot(x,y)
plt.ylim(ymin=0)
plt.annotate('this is zhusi', xy=(0,2), xytext=(0,10),arrowprops=dict(facecolor='r', headlength=5 ,headwidth=5, width=2? ) )
4.6.2 文本注釋
plt.text(x, y , str , family=' ' , size= , color= ?,style= , weight= , bbox=dict(facecolor= , alpha=0.2)) 文本注釋,設(shè)置文本的一些參數(shù),x,y為設(shè)置文本的起始坐標(biāo),str為文本的字符串內(nèi)容,family設(shè)置字體的樣式如宋體等,size設(shè)置大小,color設(shè)置顏色,style設(shè)置樣式如傾斜,weight設(shè)置粗體等款細(xì),bbox設(shè)置文本的邊框,在dict()中設(shè)置參數(shù),facecolor設(shè)置邊框顏色,alpha設(shè)置透明度。
x=np.linspace(-10,10,100)
y=x*x
plt.plot(x,y)
plt.ylim(ymin=0)
plt.text(0, 25, 'zhushi', family='serif' , size=20, color='r' ,style='italic', weight='black', bbox=dict(facecolor='r',alpha=0.2))
plt.text(0,5,'duibi')
4.6.3公式
matplotlib自帶 mathtext引擎,不用安裝Tex系統(tǒng)
參考matplotlib公式手冊
.text() 方法,將str參數(shù)傳入數(shù)學(xué)公式的文本編輯內(nèi)容,參考matplotlib文檔規(guī)則
ax.text(2,4, r'$ \alpha_i \beta_j \pi \lambda \omega $',size=15)
ax.text(4,4 ,r'$ \sin(0)=\cos(\frac{\pi} {2}) $',size=15)
ax.text(2,2, r'$ \lim_{x \rightarrow y} \frac{1}{x^3} $',size=15)
ax.text(4,2,r'$ \sqrt[4]{x}=\sqrt{y} $',size=15)