Python?Matplotlib通過plt.subplots創(chuàng)建子繪圖
前言
plt.subplots
調(diào)用后將會(huì)產(chǎn)生一個(gè)圖表(Figure)和默認(rèn)網(wǎng)格(Grid),與此同時(shí)提供一個(gè)合理的控制策略布局子繪圖。
一、只有子圖的繪制
如果沒有提供參數(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)刻度,上面中間的刻度被刪除了。
如果你覺得中間的留白不太舒服的話,也有辦法去除。方法是通過GridSpec對(duì)象,但是使用上就比較麻煩了,因?yàn)槟阈枰约簞?chuàng)建一個(gè)figure并使用add_gridspec
返回這個(gè)對(duì)象,然后再通過subplot
進(jìn)行接下來的操作。
直接看例子吧:
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
方法,這是用來隱藏非邊界的坐標(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)建出來使用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通過plt.subplots創(chuàng)建子繪圖的文章就介紹到這了,更多相關(guān)Python創(chuàng)建子繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+OpenCV圖像處理—— 色彩空間轉(zhuǎn)換
這篇文章主要介紹了Python+OpenCV如何對(duì)圖片進(jìn)行色彩空間轉(zhuǎn)換,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下下2020-10-10python學(xué)生管理系統(tǒng)代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了python學(xué)生管理系統(tǒng)代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03使用Pytorch Geometric進(jìn)行鏈接預(yù)測(cè)的實(shí)現(xiàn)代碼
PyTorch Geometric (PyG)是構(gòu)建圖神經(jīng)網(wǎng)絡(luò)模型和實(shí)驗(yàn)各種圖卷積的主要工具,在本文中我們將通過鏈接預(yù)測(cè)來對(duì)其進(jìn)行介紹,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-10-10Pytest?fixture及conftest相關(guān)詳解
這篇文章主要介紹了Pytest?fixture及conftest相關(guān)詳解,fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù),更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-09-09pymongo實(shí)現(xiàn)控制mongodb中數(shù)字字段做加法的方法
這篇文章主要介紹了pymongo實(shí)現(xiàn)控制mongodb中數(shù)字字段做加法的方法,涉及Python使用pymongo模塊操作mongodb數(shù)據(jù)庫字段的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Python實(shí)戰(zhàn)之實(shí)現(xiàn)簡(jiǎn)易的學(xué)生選課系統(tǒng)
又到了小伙伴們最喜歡的python實(shí)戰(zhàn)環(huán)節(jié),文中對(duì)實(shí)現(xiàn)簡(jiǎn)易的學(xué)生選課系統(tǒng)作了非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05