日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python+matplotlib繪制多子圖的方法詳解_python

作者:pythonic生物人 ? 更新時間: 2022-08-27 編程語言

本文速覽

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
???????????????????)

#?參數介紹
'''
##?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,使得各子圖之間的距離自動調整【類似excel中行寬根據內容自適應】
?????????????????
????????????????)

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]四個參數(fractions?of?figure)可以非常靈活的調節子圖中子圖的位置?????
????????)
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'])

原文鏈接:https://mp.weixin.qq.com/s/SeqyfzNBO_mcGJbPLHbjlQ

欄目分類
最近更新