Python+matplotlib繪制多子圖的方法詳解
本文速覽
matplotlib.pyplot api 繪制子圖

面向對象方式繪制子圖

matplotlib.gridspec.GridSpec繪制子圖

任意位置添加子圖

關于pyplot和面向對象兩種繪圖方式可參考之前文章:matplotlib.pyplot api verus matplotlib object-oriented
1、matplotlib.pyplot api 方式添加子圖
import?matplotlib.pyplot?as?plt
my_dpi=96
plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
plt.subplot(221)
plt.plot([1,2,3])
plt.subplot(222)
plt.bar([1,2,3],[4,5,6])
plt.title('plt.subplot(222)')#注意比較和上面面向對象方式的差異
plt.xlabel('set_xlabel')
plt.ylabel('set_ylabel',fontsize=15,color='g')#設置y軸刻度標簽
plt.xlim(0,8)#設置x軸刻度范圍
plt.xticks(range(0,10,2))???#?設置x軸刻度間距
plt.tick_params(axis='x',?labelsize=20,?rotation=45)#x軸標簽旋轉、字號等
plt.subplot(223)
plt.plot([1,2,3])
plt.subplot(224)
plt.bar([1,2,3],[4,5,6])
plt.suptitle('matplotlib.pyplot?api',color='r')
fig.tight_layout(rect=(0,0,1,0.9))
plt.subplots_adjust(left=0.125,
????????????????????bottom=-0.51,
????????????????????right=1.3,
????????????????????top=0.88,
????????????????????wspace=0.2,
????????????????????hspace=0.2
???????????????????)
???????????????????
#plt.tight_layout()
plt.show()

2、面向對象方式添加子圖
import?matplotlib.pyplot?as?plt
my_dpi=96
fig,?axs?=?plt.subplots(2,2,figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi,
???????????????????????sharex=False,#x軸刻度值共享開啟
???????????????????????sharey=False,#y軸刻度值共享關閉????????????????????????
????????????????????????
???????????????????????)
#fig為matplotlib.figure.Figure對象
#axs為matplotlib.axes.Axes,把fig分成2x2的子圖
axs[0][0].plot([1,2,3])
axs[0][1].bar([1,2,3],[4,5,6])
axs[0][1].set(title='title')#設置axes及子圖標題
axs[0][1].set_xlabel('set_xlabel',fontsize=15,color='g')#設置x軸刻度標簽
axs[0][1].set_ylabel('set_ylabel',fontsize=15,color='g')#設置y軸刻度標簽
axs[0][1].set_xlim(0,8)#設置x軸刻度范圍
axs[0][1].set_xticks(range(0,10,2))???#?設置x軸刻度間距
axs[0][1].tick_params(axis='x',?#可選'y','both'
??????????????????????labelsize=20,?rotation=45)#x軸標簽旋轉、字號等
axs[1][0].plot([1,2,3])
axs[1][1].bar([1,2,3],[4,5,6])
fig.suptitle('matplotlib?object-oriented',color='r')#設置fig即整整張圖的標題
#修改子圖在整個figure中的位置(上下左右)
plt.subplots_adjust(left=0.125,
????????????????????bottom=-0.61,
????????????????????right=1.3,#防止右邊子圖y軸標題與左邊子圖重疊
????????????????????top=0.88,
????????????????????wspace=0.2,
????????????????????hspace=0.2
???????????????????)
#?參數(shù)介紹
'''
##?The?figure?subplot?parameters.??All?dimensions?are?a?fraction?of?the?figure?width?and?height.
#figure.subplot.left:???0.125??#?the?left?side?of?the?subplots?of?the?figure
#figure.subplot.right:??0.9????#?the?right?side?of?the?subplots?of?the?figure
#figure.subplot.bottom:?0.11???#?the?bottom?of?the?subplots?of?the?figure
#figure.subplot.top:????0.88???#?the?top?of?the?subplots?of?the?figure
#figure.subplot.wspace:?0.2????#?the?amount?of?width?reserved?for?space?between?subplots,
???????????????????????????????#?expressed?as?a?fraction?of?the?average?axis?width
#figure.subplot.hspace:?0.2????#?the?amount?of?height?reserved?for?space?between?subplots,
???????????????????????????????#?expressed?as?a?fraction?of?the?average?axis?height
'''
plt.show()

3、matplotlib.pyplot add_subplot方式添加子圖
my_dpi=96
fig?=?plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
fig.add_subplot(221)
plt.plot([1,2,3])
fig.add_subplot(222)
plt.bar([1,2,3],[4,5,6])
plt.title('fig.add_subplot(222)')
fig.add_subplot(223)
plt.plot([1,2,3])
fig.add_subplot(224)
plt.bar([1,2,3],[4,5,6])
plt.suptitle('matplotlib.pyplot?api:add_subplot',color='r')

4、matplotlib.gridspec.GridSpec方式添加子圖
語法:matplotlib.gridspec.GridSpec(nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None)
import?matplotlib.pyplot?as?plt
from?matplotlib.gridspec?import?GridSpec
fig?=?plt.figure(dpi=100,
?????????????????constrained_layout=True,#類似于tight_layout,使得各子圖之間的距離自動調(diào)整【類似excel中行寬根據(jù)內(nèi)容自適應】
?????????????????
????????????????)
gs?=?GridSpec(3,?3,?figure=fig)#GridSpec將fiure分為3行3列,每行三個axes,gs為一個matplotlib.gridspec.GridSpec對象,可靈活的切片figure
ax1?=?fig.add_subplot(gs[0,?0:1])
plt.plot([1,2,3])
ax2?=?fig.add_subplot(gs[0,?1:3])#gs[0,?0:3]中0選取figure的第一行,0:3選取figure第二列和第三列
#ax3?=?fig.add_subplot(gs[1,?0:2])
plt.subplot(gs[1,?0:2])#同樣可以使用基于pyplot?api的方式
plt.scatter([1,2,3],[4,5,6],marker='*')
ax4?=?fig.add_subplot(gs[1:3,?2:3])
plt.bar([1,2,3],[4,5,6])
ax5?=?fig.add_subplot(gs[2,?0:1])
ax6?=?fig.add_subplot(gs[2,?1:2])
fig.suptitle("GridSpec",color='r')
plt.show()

5、子圖中繪制子圖
import?matplotlib.pyplot?as?plt
import?matplotlib.gridspec?as?gridspec
def?format_axes(fig):
????for?i,?ax?in?enumerate(fig.axes):
????????ax.text(0.5,?0.5,?"ax%d"?%?(i+1),?va="center",?ha="center")
????????ax.tick_params(labelbottom=False,?labelleft=False)
#?子圖中再繪制子圖
fig?=?plt.figure(dpi=100,
????????????????constrained_layout=True,
????????????????)
gs0?=?GridSpec(1,?2,?figure=fig)#將figure切片為1行2列的兩個子圖
gs00?=?gridspec.GridSpecFromSubplotSpec(3,?3,?subplot_spec=gs0[0])#將以上第一個子圖gs0[0]再次切片為3行3列的9個axes
#gs0[0]子圖自由切片
ax1?=?fig.add_subplot(gs00[:-1,?:])
ax2?=?fig.add_subplot(gs00[-1,?:-1])
ax3?=?fig.add_subplot(gs00[-1,?-1])
gs01?=?gs0[1].subgridspec(3,?3)#將以上第二個子圖gs0[1]再次切片為3行3列的axes
#gs0[1]子圖自由切片
ax4?=?fig.add_subplot(gs01[:,?:-1])
ax5?=?fig.add_subplot(gs01[:-1,?-1])
ax6?=?fig.add_subplot(gs01[-1,?-1])
plt.suptitle("GridSpec?Inside?GridSpec",color='r')
format_axes(fig)
plt.show()

6、任意位置繪制子圖(plt.axes)
plt.subplots(1,2,dpi=100) plt.subplot(121) plt.plot([1,2,3]) plt.subplot(122) plt.plot([1,2,3]) plt.axes([0.7,?0.2,?0.15,?0.15],?##?[left,?bottom,?width,?height]四個參數(shù)(fractions?of?figure)可以非常靈活的調(diào)節(jié)子圖中子圖的位置????? ????????) plt.bar([1,2,3],[1,2,3],color=['r','b','g']) plt.axes([0.2,?0.6,?0.15,?0.15],? ????????) plt.bar([1,2,3],[1,2,3],color=['r','b','g'])

以上就是Python+matplotlib繪制多子圖的方法詳解的詳細內(nèi)容,更多關于Python matplotlib多子圖的資料請關注腳本之家其它相關文章!
相關文章
基于Python數(shù)據(jù)可視化利器Matplotlib,繪圖入門篇,Pyplot詳解
下面小編就為大家?guī)硪黄赑ython數(shù)據(jù)可視化利器Matplotlib,繪圖入門篇,Pyplot詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
python selenium 對瀏覽器標簽頁進行關閉和切換的方法
今天小編就為大家分享一篇python selenium 對瀏覽器標簽頁進行關閉和切換的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
jupyter關于pandas的dataframe行列顯示不全與復原問題
這篇文章主要介紹了jupyter關于pandas的dataframe行列顯示不全與復原問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Python PyQt5實現(xiàn)的簡易計算器功能示例
這篇文章主要介紹了Python PyQt5實現(xiàn)的簡易計算器功能,涉及Python PyQt5事件響應及數(shù)學運算等相關操作技巧,需要的朋友可以參考下2017-08-08
django框架使用orm實現(xiàn)批量更新數(shù)據(jù)的方法
這篇文章主要介紹了django框架使用orm實現(xiàn)批量更新數(shù)據(jù)的方法,結合實例形式簡單分析了Django基于orm操作數(shù)據(jù)庫更新數(shù)據(jù)的相關實現(xiàn)技巧,需要的朋友可以參考下2019-06-06

