最后一次更新日期: 2019/4/20
Matplotlib 是一個 Python 的 2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質(zhì)量級別的圖形。
按需導(dǎo)入以下模塊:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
1. 注解annotate
fig=plt.figure(figsize=(10,3.5))
ax=fig.add_subplot(121,title='annotate')
ax.scatter([-0.25],[-0.25],s=100)
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
bbox_style = dict(boxstyle="square", fc='white', ec='black',lw=1)
arrow_style = dict(arrowstyle='->',color='black',lw=2)
ax.annotate('This is a dot.',(-0.24,-0.24),(-0.14,0.16),
arrowprops=arrow_style,bbox=bbox_style,fontsize=15)
ax=fig.add_subplot(122,title='arrow+text')
ax.scatter([-0.25],[-0.25],s=100)
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.arrow(0.09,0.06,-0.25,-0.23,fc='k',ec='k',
width=0.01,head_width=0.07,head_length=0.07)
ax.text(-0.14,0.16,'This is a dot.',fontsize=15,bbox=bbox_style)
annotate
方法用于向圖像上添加注解:第一個參數(shù)
s
設(shè)置注釋的文本;第二個參數(shù)
xy
設(shè)置要注釋的點位置,tuple
類型表示的坐標(biāo);第三個參數(shù)
xytext
設(shè)置注釋文本的位置,tuple
類型表示的坐標(biāo);參數(shù)
xycoords
和textcoords
設(shè)置注釋點位置和文本位置所采用的坐標(biāo)系,默認'data'
和數(shù)據(jù)的坐標(biāo)系一致;參數(shù)
arrowprops
設(shè)置箭頭,dict
類型,其中arrowstyle
屬性設(shè)置樣式,color
屬性設(shè)置顏色,lw
屬性設(shè)置箭頭寬度;以上三個參數(shù)具體信息建議參考官方文檔-annotate;
參數(shù)
bbox
設(shè)置文本框樣式,dict
類型,其中boxstyle
屬性設(shè)置樣式,fc
屬性設(shè)置填充顏色,ec
屬性設(shè)置邊緣顏色,lw
屬性設(shè)置邊緣線寬度;bbox
中的詳細設(shè)置建議參考官方文檔-Rectangle;參數(shù)
fontsize
設(shè)置字體大小。
注解也可通過arrow
+text
實現(xiàn)。
arrow
方法用于添加箭頭:
第1,2個參數(shù)x
,y
設(shè)置箭頭的起始位置;
第3,4個參數(shù)dx
,dy
設(shè)置箭頭往xy方向延伸的長度;
fc
參數(shù)設(shè)置填充顏色;ec
參數(shù)設(shè)置邊緣顏色;
width
參數(shù)設(shè)置箭頭線寬度;
head_width
參數(shù)設(shè)置箭頭頭部的寬度;
head_length
參數(shù)設(shè)置箭頭頭部的長度。
text
方法用于添加文本:
第1,2個參數(shù)x
,y
設(shè)置文本的位置;
第3個參數(shù)s
設(shè)置要顯示的文本;
參數(shù)fontsize
設(shè)置字體大小;
參數(shù)bbox
設(shè)置文本框樣式,與annotate
相同。
arrow
繪制的箭頭在有所傾斜時無法保證頭部的底部與線垂直,對此有要求只能使用annotate
。
2. 區(qū)域填充fill
x=np.arange(0,720,1)
y1=np.sin(x*np.pi/180)
y2=np.cos(x*np.pi/180)
fig=plt.figure(figsize=(10,3.5))
ax=fig.add_subplot(121,title='fill')
ax.plot(x,y1)
ax.plot(x,y2)
ax.fill(x,y1,color='g',alpha=0.3)
ax.fill(x,y2,color='b',alpha=0.3)
ax=fig.add_subplot(122,title='fill between')
ax.plot(x,y1)
ax.plot(x,y2)
ax.fill_between(x,y1,y2,color='g',alpha=0.3)
fill
方法用于填充多邊形:第1,2個參數(shù)
x
,y
設(shè)置邊的xy坐標(biāo),該繪圖方法不適合填充不封閉的曲線,會如上圖出現(xiàn)無法預(yù)估的繪制效果。
fill_between
方法用于填充兩條曲線中間的區(qū)域:
第1,2,3個參數(shù)x
,y1
,y2
設(shè)置x坐標(biāo)和兩條曲線的y坐標(biāo);
第4個參數(shù)where
設(shè)置繪制的橫坐標(biāo)范圍,布爾數(shù)組類型,相當(dāng)于對前三個參數(shù)執(zhí)行索引篩選。
3. 圖片image
from PIL import Image
image1=Image.open('D:\\training_data\\used\\cifar-10-batches-py\\test\\1_貓.png')
image2=Image.open('D:\\training_data\\used\\cifar-10-batches-py\\test\\2_船.png')
fig=plt.figure(figsize=(8,4))
ax=fig.add_subplot(121,title='image1')
ax.imshow(image1)
ax=fig.add_subplot(122,title='image2')
ax.imshow(image2)
imshow
用于顯示圖片,默認是會顯示坐標(biāo)軸和刻度的,可通過Axes.axis('off')
關(guān)閉。
4. 基本圖形patch
import matplotlib.patches as patches
from matplotlib.collections import PatchCollection
fig=plt.figure(figsize=(9,3))
ax=fig.add_subplot(121,title='Rectangle')
rects=[]
x=[1.5,3.5,5.5,]
y=[3,4.5,3]
for i in range(3):
rect=patches.Rectangle((x[i],y[i]),3,3)
rects.append(rect)
pc=PatchCollection(rects,linewidth=1,edgecolor='r',facecolor='none')
#ax.add_patch(rect)
ax.add_collection(pc)
ax.set_xlim([0,10])
ax.set_ylim([0,10])
ax=fig.add_subplot(122,title='Ellipse')
ells=[]
for i in range(5):
ell=patches.Ellipse((5,5),6,3,angle=i*36)
ells.append(ell)
pc=PatchCollection(ells,facecolor='g',alpha=0.5)
#ax.add_patch(ell)
ax.add_collection(pc)
ax.set_xlim([0,10])
ax.set_ylim([0,10])
繪制基本圖形和相應(yīng)集合需要導(dǎo)入
patches
和PatchCollection
。
patches
提供了各種圖形的構(gòu)造:
Rectangle
是矩形類,第1個參數(shù)xy
設(shè)置左下角頂點的坐標(biāo),第2,3個參數(shù)width
,height
設(shè)置寬度和高度,第4個參數(shù)angle
設(shè)置旋轉(zhuǎn)角度;
Ellipse
是橢圓類,第1個參數(shù)xy
設(shè)置橢圓中心的坐標(biāo),第2,3個參數(shù)width
,height
設(shè)置橫軸和豎軸的長度(直徑),第4個參數(shù)angle
設(shè)置旋轉(zhuǎn)角度。
PatchCollection
用于構(gòu)造patches
集合并設(shè)置通用的拓展參數(shù):
linewidth
參數(shù)設(shè)置邊緣線寬;
edgecolor
參數(shù)設(shè)置邊緣顏色;
facecolor
參數(shù)設(shè)置填充顏色,facecolor='none'
可以設(shè)置不填充(在創(chuàng)建圖形類時,fill=False
也能設(shè)置不填充);
alpha
參數(shù)設(shè)置透明度。
add_patch
用于向Axes
中添加單個圖形;
add_collection
用于向Axes
中添加圖形集合;
Axes.patches
可以查看Axes
下的所有Patch
繪圖對象;
Axes.collections
可以查看Axes
下的所有繪圖集合。
5. 數(shù)據(jù)表格table
data=np.array([[1,2,3],[4,5,6],[7,8,9]])
row_labels=['row1','row2','row3']
row_colors=plt.cm.BuPu(np.linspace(0, 0.5, len(row_labels)))
col_labels=['col1','col2','col3']
col_index=np.arange(3)
fig=plt.figure()
ax=fig.add_subplot(111,title='table')
for i in range(len(row_labels)-1,-1,-1):
ax.bar(col_index,data[i],color=row_colors[i],linewidth=0.5,edgecolor='k')
ax.table(cellText=data,
rowLabels=row_labels,
rowColours=row_colors,
colLabels=col_labels,
loc='bottom')
ax.set_xticks([])
table
方法用于添加表格:
cellText
參數(shù)設(shè)置單元格數(shù)據(jù),二維序列類型,默認None
;
cellColors
參數(shù)設(shè)置單元格顏色,二維序列類型,默認None
;
cellText
和cellColors
兩個參數(shù)至少有一個要賦值;
cellLoc
參數(shù)設(shè)置單元格位置,默認'right'
;
colWidths
參數(shù)設(shè)置列寬,一維序列類型,可選;
rowLabels
參數(shù)設(shè)置行標(biāo)簽,一維序列類型,可選;
rowColors
參數(shù)設(shè)置行標(biāo)簽顏色,一維序列類型,可選;
rowLoc
參數(shù)設(shè)置行標(biāo)簽位置,默認'left'
;
colLabels
參數(shù)設(shè)置列標(biāo)簽,一維序列類型,可選;
colColors
參數(shù)設(shè)置列標(biāo)簽顏色,一維序列類型,可選;
colLoc
參數(shù)設(shè)置列標(biāo)簽位置,默認'center'
;
loc
參數(shù)設(shè)置表格位置,默認bottom
;
bbox
參數(shù)設(shè)置方框樣式,可選。
更詳細的設(shè)置可以自行創(chuàng)建Table
對象,通過Axes.add_table
方法添加;
Axes.tables
可以查看Axes
下的所有Table
繪圖對象。