Python?Matplotlib通過plt.subplots創(chuàng)建子繪圖
前言
plt.subplots
調用后將會產生一個圖表(Figure)和默認網格(Grid),與此同時提供一個合理的控制策略布局子繪圖。
一、只有子圖的繪制
如果沒有提供參數給subplots
將會返回:
Figure一個Axes對象
例子:
fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('A single plot')
二、單個方向堆疊子圖
堆疊子圖就需要用到額外的可選參數,分別是子圖的行和列數,如果你只傳遞一個數字,默認列數為1,行堆疊。
比如:
fig, axs = plt.subplots(2) fig.suptitle('Vertically stacked subplots') axs[0].plot(x, y) axs[1].plot(x, -y)
當然如果你的子圖比較少,可以考慮用元組接收axes對象:
fig, (ax1, ax2) = plt.subplots(2) fig.suptitle('Vertically stacked subplots') ax1.plot(x, y) ax2.plot(x, -y)
如果想要按照行排列,將參數改成(1,2)即可。
三、行列方向擴展子圖
如果行列擴展子圖,那么axes返回的則是一個二維Numpy數組。利用axe的flat屬性,可以批量對軸進行賦值。
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()
當然你可以用單個軸對象接收:
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()
四、共享軸
默認情況下,每個子圖都是獨立創(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)
可以看出兩者的橫坐標刻度并不對齊,那么應該如何設置共享?答:在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)
結果如下:
OK,看上去確實統(tǒng)一了坐標軸,除此,python幫你移除了多余的坐標刻度,上面中間的刻度被刪除了。
如果你覺得中間的留白不太舒服的話,也有辦法去除。方法是通過GridSpec對象,但是使用上就比較麻煩了,因為你需要自己創(chuàng)建一個figure并使用add_gridspec
返回這個對象,然后再通過subplot
進行接下來的操作。
直接看例子吧:
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
方法,這是用來隱藏非邊界的坐標軸的。“share”在這里的意思是:共享一個坐標軸,也就意味著刻度的位置是對齊的。
請注意,修改sharex和sharey是全局修改的,所以你如果想讓每一行和每一列共享一個坐標軸,可以考慮用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()
如果你需要關聯(lián)更加復雜的共享軸關系,可以創(chuàng)建出來使用axe的成員sharex、sharey進行設置:
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()# 讓繪圖更加緊湊
五、極坐標子圖
fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar')) ax1.plot(x, y) ax2.plot(x, y ** 2) plt.show()
到此這篇關于Python Matplotlib通過plt.subplots創(chuàng)建子繪圖的文章就介紹到這了,更多相關Python創(chuàng)建子繪圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Pytorch Geometric進行鏈接預測的實現代碼
PyTorch Geometric (PyG)是構建圖神經網絡模型和實驗各種圖卷積的主要工具,在本文中我們將通過鏈接預測來對其進行介紹,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2023-10-10Python實戰(zhàn)之實現簡易的學生選課系統(tǒng)
又到了小伙伴們最喜歡的python實戰(zhàn)環(huán)節(jié),文中對實現簡易的學生選課系統(tǒng)作了非常詳細的代碼示例,對正在學習python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05