Denver Dang
Denver Dang

Reputation: 2615

Zoom in on subplot (with inset_axes plot) on click

So I have followed the approach from this thread:

How can I plot the same figure standalone and in a subplot in Matplotlib?

And it actually works quite well, as seen in his example. However, one issue is that I have inset_axes plots in my plots. So when it zooms, all the inset plots remains, and actually overlaps the enlarged plot.

I am however not sure how to remove them, and maybe even also zoom in on the inset plot that are together with the subplot being clicked.

So from the thread I have just used this class for the zoom approach:

class ZoomingSubplots(object):
    def __init__(self, *args, **kwargs):
        """All parameters passed on to 'subplots`."""
        self.fig, self.axes = plt.subplots(*args, **kwargs)
        self._zoomed = False
        self.fig.canvas.mpl_connect('button_press_event', self.on_click)
        self.fig.subplots_adjust(hspace=0.3)

    def zoom(self, selected_ax):
        for ax in self.axes.flat:
            ax.set_visible(False)
        self._original_size = selected_ax.get_position()
        selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
        selected_ax.set_visible(True)
        self._zoomed = True

    def unzoom(self, selected_ax):
        selected_ax.set_position(self._original_size)
        for ax in self.axes.flat:
            ax.set_visible(True)
        self._zoomed = False

    def on_click(self, event):
        if event.inaxes is None:
            return
        if self._zoomed:
            self.unzoom(event.inaxes)
        else:
            self.zoom(event.inaxes)
        self.fig.canvas.draw()

And then when I plot I use this:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

__name__ = '__main__':
    subplots = ZoomingSubplots(2, 2)

    for ax in subplots.axes.flat:
        ax.plot(x, y)
        axins = inset_axes(ax, width=1.3, height=0.9, loc=2)
        axins.plot(x2, y2)

    plt.show()

But as stated, with this the inset plot for all subplots will remain in their position and overlap with the enlarged plot. How can I change this, so that they don't intervene, and maybe even the inset_plot for the subplot remains and is also enlarged ?

Upvotes: 0

Views: 247

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

I couldn't find a way to change the position of the inset_axes, probably because their position and size are locked-in to their parent axes.

In the meantime, here is a new class that creates the inset_axes automatically, and hides/shows the relevant inset_axes when clicking on one of the base axes.

class ZoomingSubplotsWithInset(ZoomingSubplots):
    def __init__(self, *args, inset_width=1.3, inset_height=0.9, inset_loc=2, **kwargs):
        super(ZoomingSubplotsWithInset, self).__init__(*args, **kwargs)
        self.inset_axes = []
        for ax in self.axes.flat:
            axins = inset_axes(ax, width=inset_width, height=inset_height, loc=inset_loc)
            self.inset_axes.append(axins)
        self.inset_axes = np.array(self.inset_axes)

    def on_click(self, event):
        if event.inaxes in self.axes.flat:
            super(ZoomingSubplotsWithInset, self).on_click(event)

    def zoom(self, selected_ax):
        for ax in self.inset_axes.flat:
            ax.set_visible(False)
        super(ZoomingSubplotsWithInset, self).zoom(selected_ax)
        # restore visibility of the inset_axes corresponding to the zoomed axes
        _, _, i = selected_ax.get_geometry()
        self.inset_axes[i-1].set_visible(True)

    def unzoom(self, selected_ax):
        for ax in self.inset_axes.flat:
            ax.set_visible(True)
        super(ZoomingSubplotsWithInset, self).unzoom(selected_ax)


if __name__ == '__main__':
    N = 10

    subplots = ZoomingSubplotsWithInset(2, 2)

    for ax, axins in zip(subplots.axes.flat, subplots.inset_axes.flat):
        x, y = np.random.random(size=(2, N))
        x2, y2 = np.random.random(size=(2, N))
        ax.plot(x, y)
        axins.plot(x2, y2)

    plt.show()

Upvotes: 1

Related Questions