Python?Matplotlib通過(guò)plt.subplots創(chuàng)建子繪圖
前言
plt.subplots調(diào)用后將會(huì)產(chǎn)生一個(gè)圖表(Figure)和默認(rèn)網(wǎng)格(Grid),與此同時(shí)提供一個(gè)合理的控制策略布局子繪圖。
一、只有子圖的繪制
如果沒(méi)有提供參數(shù)給subplots將會(huì)返回:
Figure一個(gè)Axes對(duì)象
例子:
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')
二、單個(gè)方向堆疊子圖
堆疊子圖就需要用到額外的可選參數(shù),分別是子圖的行和列數(shù),如果你只傳遞一個(gè)數(shù)字,默認(rèn)列數(shù)為1,行堆疊。
比如:
fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)
當(dāng)然如果你的子圖比較少,可以考慮用元組接收axes對(duì)象:
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)如果想要按照行排列,將參數(shù)改成(1,2)即可。
三、行列方向擴(kuò)展子圖
如果行列擴(kuò)展子圖,那么axes返回的則是一個(gè)二維Numpy數(shù)組。利用axe的flat屬性,可以批量對(duì)軸進(jìn)行賦值。
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')# 等價(jià)于axes[0][0]
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')
for ax in axs.flat:
ax.set(xlabel='x-label', ylabel='y-label')
# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
ax.label_outer()當(dāng)然你可以用單個(gè)軸對(duì)象接收:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')
for ax in fig.get_axes():
ax.label_outer()四、共享軸
默認(rèn)情況下,每個(gè)子圖都是獨(dú)立創(chuàng)建的。
看下面這個(gè)例子:
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Axes values are scaled individually by default')
ax1.plot(x, y)
ax2.plot(x + 1, -y)
可以看出兩者的橫坐標(biāo)刻度并不對(duì)齊,那么應(yīng)該如何設(shè)置共享?答:在subplot創(chuàng)建之時(shí)使用sharex=True和sharedy=True分別創(chuàng)建X軸共享或者Y軸共享。
將上邊的例子修改為以下:
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
fig.suptitle('Aligning x-axis using sharex')
ax1.plot(x, y)
ax2.plot(x + 1, -y)結(jié)果如下:

OK,看上去確實(shí)統(tǒng)一了坐標(biāo)軸,除此,python幫你移除了多余的坐標(biāo)刻度,上面中間的刻度被刪除了。
如果你覺(jué)得中間的留白不太舒服的話(huà),也有辦法去除。方法是通過(guò)GridSpec對(duì)象,但是使用上就比較麻煩了,因?yàn)槟阈枰约簞?chuàng)建一個(gè)figure并使用add_gridspec返回這個(gè)對(duì)象,然后再通過(guò)subplot進(jìn)行接下來(lái)的操作。
直接看例子吧:
fig = plt.figure()
gs = fig.add_gridspec(3, hspace=0)
axs = gs.subplots(sharex=True, sharey=True)
fig.suptitle('Sharing both axes')
axs[0].plot(x, y ** 2)
axs[1].plot(x, 0.3 * y, 'o')
axs[2].plot(x, y, '+')
# Hide x labels and tick labels for all but bottom plot.
for ax in axs:
ax.label_outer()
這里還用到了軸的label_outer方法,這是用來(lái)隱藏非邊界的坐標(biāo)軸的。“share”在這里的意思是:共享一個(gè)坐標(biāo)軸,也就意味著刻度的位置是對(duì)齊的。
請(qǐng)注意,修改sharex和sharey是全局修改的,所以你如果想讓每一行和每一列共享一個(gè)坐標(biāo)軸,可以考慮用sharex='col', sharey='row'。
fig = plt.figure()
gs = fig.add_gridspec(2, 2, hspace=0, wspace=0)
(ax1, ax2), (ax3, ax4) = gs.subplots(sharex='col', sharey='row')
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x + 1, -y, 'tab:green')
ax4.plot(x + 2, -y**2, 'tab:red')
for ax in axs.flat:
ax.label_outer()
如果你需要關(guān)聯(lián)更加復(fù)雜的共享軸關(guān)系,可以創(chuàng)建出來(lái)使用axe的成員sharex、sharey進(jìn)行設(shè)置:
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title("main")
axs[1, 0].plot(x, y**2)
axs[1, 0].set_title("shares x with main")
axs[1, 0].sharex(axs[0, 0])
axs[0, 1].plot(x + 1, y + 1)
axs[0, 1].set_title("unrelated")
axs[1, 1].plot(x + 2, y + 2)
axs[1, 1].set_title("also unrelated")
fig.tight_layout()# 讓繪圖更加緊湊
五、極坐標(biāo)子圖
fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar')) ax1.plot(x, y) ax2.plot(x, y ** 2) plt.show()

到此這篇關(guān)于Python Matplotlib通過(guò)plt.subplots創(chuàng)建子繪圖的文章就介紹到這了,更多相關(guān)Python創(chuàng)建子繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django中g(shù)et()和filter()返回值區(qū)別詳解
在django中,我們查詢(xún)經(jīng)常用的兩個(gè)API中,會(huì)經(jīng)常用到get()和filter()兩個(gè)方法,兩者的區(qū)別是什么呢?本文就一起來(lái)了解一下2021-05-05
Python類(lèi)class參數(shù)self原理解析
這篇文章主要介紹了Python類(lèi)class參數(shù)self原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
使用Dataframe.info()顯示空值與類(lèi)型信息
這篇文章主要介紹了使用Dataframe.info()顯示空值與類(lèi)型信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
簡(jiǎn)單介紹Python虛擬環(huán)境及使用方法
Python的虛擬環(huán)境極大地方便了人們的生活.本文介紹了虛擬環(huán)境的基礎(chǔ)知識(shí)以及使用方法,文中有非常詳細(xì)的說(shuō)明,需要的朋友可以參考下2021-06-06
使用PyCharm安裝pytest及requests的問(wèn)題
這篇文章主要介紹了使用PyCharm安裝pytest及requests的相關(guān)資料,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
python使用ddddocr庫(kù)識(shí)別滑動(dòng)驗(yàn)證碼簡(jiǎn)單代碼示例
這篇文章主要介紹了如何使用ddddocr庫(kù)來(lái)識(shí)別滑塊驗(yàn)證碼,并提供了一個(gè)示例代碼和識(shí)別結(jié)果,同時(shí)提醒注意ddddocr庫(kù)的大小限制,可能會(huì)影響某些無(wú)服務(wù)器函數(shù)的部署,需要的朋友可以參考下2024-11-11
Python隨機(jī)數(shù)函數(shù)代碼實(shí)例解析
這篇文章主要介紹了Python隨機(jī)數(shù)函數(shù)代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Python RabbitMQ消息隊(duì)列實(shí)現(xiàn)rpc
這篇文章主要介紹了python 之rabbitmq實(shí)現(xiàn)rpc,主要實(shí)現(xiàn)客戶(hù)端通過(guò)發(fā)送命令來(lái)調(diào)用服務(wù)端的某些服務(wù),服務(wù)端把結(jié)果再返回給客戶(hù)端,感興趣的小伙伴們可以參考一下2018-05-05

