簡體   English   中英

matplotlib圖中子圖的子子圖

[英]Subsubplot of subplot in a matplotlib figure

為了進行比較,我想使用以下內容在 matplotlib 中創建一個包含 2x2 子圖的圖形:

fig = plt.figure(figsize=(15,15))
ax1 = fig.add_subplot(2, 2, 1)
ax1.plot(x1,y1)
ax2 = fig.add_subplot(2, 2, 2)
ax2.plot(x2,y2)
ax3 = fig.add_subplot(2, 2, 3)
ax3.plot(x3,y3)
ax4 = fig.add_subplot(2, 2, 4)
ax4.plot(x4,y4)`enter code here`
fig.tight_layout(pad=3.0)

但是,讓我們假設應該在subplot(2,2,2)繪制的數據需要一個不連續的 x 軸。 到目前為止,我發現使用不連續的 x 軸繪制數據的所有方法都依賴於將數據拆分並將其繪制為不同的子圖,如這個公認的 stackoverflow 答案中所述:

https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib

因此,我正在尋找一種方法,將 2x2 子圖中的 x 軸不連續的圖顯示為 subplot(2,2,2) 以及不包含連續軸的 3 個軸。

下面的草圖說明了這個想法:子圖中的子圖草圖

有什么建議?

這可以使用fig.add_gridspec作為外軸,然后使用subgridspec創建兩個子軸。

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 8))

outer_grid = fig.add_gridspec(2, 2)

# Add the three "normal" subplots
ax1 = fig.add_subplot(outer_grid[0, 0])
ax2 = fig.add_subplot(outer_grid[1, 0])
ax4 = fig.add_subplot(outer_grid[1, 1])

# Add the two axes to create the "broken" axes
inner_grid = outer_grid[0, 1].subgridspec(ncols=2, nrows=1, wspace=0)
(ax3a, ax3b) = inner_grid.subplots()

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM