使用matplotlib在另一个轴上嵌入几个插入轴

时间:2014-12-30 06:28:38

标签: python matplotlib plot axes

是否可以在matplotlib轴中嵌入更改数量的图?例如,inset_axes方法用于将插入轴放置在父轴内:

enter image description here

但是,我有几行图,我想在每行的最后一个轴对象中包含一些插入轴。

fig, ax = plt.subplots(2,4, figsize=(15,15))
for i in range(2):
    ax[i][0].plot(np.random.random(40))
    ax[i][2].plot(np.random.random(40))
    ax[i][3].plot(np.random.random(40))

    # number of inset axes
    number_inset = 5
    for j in range(number_inset):
        ax[i][4].plot(np.random.random(40))

enter image description here

在这里,我想要几个包含绘图的插入轴,而不是最后一列中绘制的5个绘图。像这样:

enter image description here

这样做的原因是每一行都指向要绘制的不同项目,而最后一列应该包含此类项目的组件。有没有办法在matplotlib中执行此操作,或者可能是另一种可视化的方法?

由于

2 个答案:

答案 0 :(得分:2)

正如@hitzg所提到的,完成这样的事情最常见的方法是使用GridSpecGridSpec创建一个虚构的网格对象,您可以对其进行切片以生成子图。这是一种简单的方法来对齐您想要遵循常规网格的相当复杂的布局。

然而,在这种情况下,如何使用它可能不是很明显。您需要在GridSpec列之前创建numrows * numinsetsnumcols行,然后通过以numinsets的间隔对其进行切片来创建“主”轴。

在下面的示例中(2行,4列,3个插图),我们将gs[:3, 0]切片以获得左上角的“主”轴,gs[3:, 0]以获得左下角“main” “轴,gs[:3, 1]以获得下一个上轴等。对于插图,每个都是gs[i, -1]

作为一个完整的例子:

import numpy as np
import matplotlib.pyplot as plt

def build_axes_with_insets(numrows, numcols, numinsets, **kwargs):
    """
    Makes a *numrows* x *numcols* grid of subplots with *numinsets* subplots
    embedded as "sub-rows" in the last column of each row.

    Returns a figure object and a *numrows* x *numcols* object ndarray where
    all but the last column consists of axes objects, and the last column is a
    *numinsets* length object ndarray of axes objects.
    """
    fig = plt.figure(**kwargs)
    gs = plt.GridSpec(numrows*numinsets, numcols)

    axes = np.empty([numrows, numcols], dtype=object)
    for i in range(numrows):
        # Add "main" axes...
        for j in range(numcols - 1):
            axes[i, j] = fig.add_subplot(gs[i*numinsets:(i+1)*numinsets, j])

        # Add inset axes...
        for k in range(numinsets):
            m = k + i * numinsets
            axes[i, -1][k] = fig.add_subplot(gs[m, -1])

    return fig, axes

def plot(axes):
    """Recursive plotting function just to put something on each axes."""
    for ax in axes.flat:
        data = np.random.normal(0, 1, 100).cumsum()
        try:
            ax.plot(data)
            ax.set(xticklabels=[], yticklabels=[])
        except AttributeError:
            plot(ax)

fig, axes = build_axes_with_insets(2, 4, 3, figsize=(12, 6))
plot(axes)
fig.tight_layout()
plt.show()

enter image description here

答案 1 :(得分:1)

这是我在没有预先设置插图数量的情况下获得相同结果的方法。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

fig = plt.figure(figsize=(12,6))

nrows = 2
ncols = 4

# changing the shape of GridSpec's output
outer_grid = gridspec.GridSpec(nrows, ncols)
grid = []
for i in range(nrows*ncols):
    grid.append(outer_grid[i])
outer_grid = np.array(grid).reshape(nrows,ncols)

for i in range(nrows):
    inner_grid_1 = gridspec.GridSpecFromSubplotSpec(1, 1,
                subplot_spec=outer_grid[i][0])
    ax = plt.Subplot(fig, inner_grid_1[0])
    ax.plot(np.random.normal(0,1,50).cumsum())
    fig.add_subplot(ax)

    inner_grid_2 = gridspec.GridSpecFromSubplotSpec(1, 1,
                subplot_spec=outer_grid[i][1])
    ax2 = plt.Subplot(fig, inner_grid_2[0])
    ax2.plot(np.random.normal(0,1,50).cumsum())
    fig.add_subplot(ax2)

    inner_grid_3 = gridspec.GridSpecFromSubplotSpec(1, 1,
                subplot_spec=outer_grid[i][2])
    ax3 = plt.Subplot(fig, inner_grid_3[0])
    ax3.plot(np.random.normal(0,1,50).cumsum())
    fig.add_subplot(ax3)

    # this value can be set based on some other calculation depending 
    # on each row
    numinsets = 3 
    inner_grid_4 = gridspec.GridSpecFromSubplotSpec(numinsets, 1,
                subplot_spec=outer_grid[i][3])

    # Adding subplots to the last inner grid
    for j in range(inner_grid_4.get_geometry()[0]):
        ax4 = plt.Subplot(fig, inner_grid_4[j])
        ax4.plot(np.random.normal(0,1,50).cumsum())
        fig.add_subplot(ax4)

# Removing labels
for ax in fig.axes:
    ax.set(xticklabels=[], yticklabels=[])

fig.tight_layout()

enter image description here