網站首頁 編程語言 正文
前言
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()
四、共享軸
默認情況下,每個子圖都是獨立創建的。
看下面這個例子:
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創建之時使用sharex=True
和sharedy=True
分別創建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,看上去確實統一了坐標軸,除此,python幫你移除了多余的坐標刻度,上面中間的刻度被刪除了。
如果你覺得中間的留白不太舒服的話,也有辦法去除。方法是通過GridSpec對象,但是使用上就比較麻煩了,因為你需要自己創建一個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()
如果你需要關聯更加復雜的共享軸關系,可以創建出來使用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()
原文鏈接:https://blog.csdn.net/weixin_39258979/article/details/126039763
- 上一篇:C#中流的使用和分類_C#教程
- 下一篇:C#實現對象的序列化和反序列化_C#教程
相關推薦
- 2022-06-17 C語言詳解函數與指針的使用_C 語言
- 2022-10-14 element form表單數據未雙向綁定
- 2022-10-15 詳解C語言中雙指針算法的使用_C 語言
- 2022-04-08 go實現圖片拼接與文字書寫的方法實例_Golang
- 2022-12-11 Python?中用多種方式實現單例模式_python
- 2023-04-24 pandas常用表連接merge/concat/join/append詳解_python
- 2022-08-10 go?字符串修改的操作代碼_Golang
- 2022-12-15 Apache中偽靜態Rewrite的使用方法和URL重寫規則表達式講解_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支