【问题标题】:Subsubplot of subplot in a matplotlib figurematplotlib图中子图的子图
【发布时间】:2020-12-15 01:34:42
【问题描述】:

为了比较,我想在 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) 以及包含 no 不连续轴的 3 个轴。

下面的草图说明了这个想法: sketch subsubplot in a subplot

有什么建议吗?

【问题讨论】:

    标签: python matplotlib plot subplot


    【解决方案1】:

    这可以使用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()
    

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 2016-06-01
      • 2016-04-28
      • 2017-09-08
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多