Python?Matplotlib通過plt.subplots創(chuàng)建子繪圖
前言
plt.subplots調(diào)用后將會產(chǎn)生一個圖表(Figure)和默認(rèn)網(wǎng)格(Grid),與此同時提供一個合理的控制策略布局子繪圖。
一、只有子圖的繪制
如果沒有提供參數(shù)給subplots將會返回:
Figure一個Axes對象
例子:
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')
二、單個方向堆疊子圖
堆疊子圖就需要用到額外的可選參數(shù),分別是子圖的行和列數(shù),如果你只傳遞一個數(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對象:
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返回的則是一個二維Numpy數(shù)組。利用axe的flat屬性,可以批量對軸進(jìn)行賦值。
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')# 等價于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)然你可以用單個軸對象接收:
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)情況下,每個子圖都是獨(dú)立創(chuàng)建的。
看下面這個例子:
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)刻度并不對齊,那么應(yīng)該如何設(shè)置共享?答:在subplot創(chuàng)建之時使用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對象,但是使用上就比較麻煩了,因?yàn)槟阈枰约簞?chuàng)建一個figure并使用add_gridspec返回這個對象,然后再通過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”在這里的意思是:共享一個坐標(biāo)軸,也就意味著刻度的位置是對齊的。
請注意,修改sharex和sharey是全局修改的,所以你如果想讓每一行和每一列共享一個坐標(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django中g(shù)et()和filter()返回值區(qū)別詳解
在django中,我們查詢經(jīng)常用的兩個API中,會經(jīng)常用到get()和filter()兩個方法,兩者的區(qū)別是什么呢?本文就一起來了解一下2021-05-05
python使用ddddocr庫識別滑動驗(yàn)證碼簡單代碼示例
這篇文章主要介紹了如何使用ddddocr庫來識別滑塊驗(yàn)證碼,并提供了一個示例代碼和識別結(jié)果,同時提醒注意ddddocr庫的大小限制,可能會影響某些無服務(wù)器函數(shù)的部署,需要的朋友可以參考下2024-11-11
Python隨機(jī)數(shù)函數(shù)代碼實(shí)例解析
這篇文章主要介紹了Python隨機(jī)數(shù)函數(shù)代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
Python RabbitMQ消息隊列實(shí)現(xiàn)rpc
這篇文章主要介紹了python 之rabbitmq實(shí)現(xiàn)rpc,主要實(shí)現(xiàn)客戶端通過發(fā)送命令來調(diào)用服務(wù)端的某些服務(wù),服務(wù)端把結(jié)果再返回給客戶端,感興趣的小伙伴們可以參考一下2018-05-05

