How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?

Published: August 30, 2023

Updated: August 31, 2023

Tags: Python; Matplotlib;

DMCA.com Protection Status

The indicate_inset_zoom() method is a powerful tool to create a zoom effect on a matplotlib figure. To use this, you will first need to define the axes of your matplotlib figure and then specify the region that should be zoomed in using the xmin, xmax, ymin, and ymax parameters. After defining these parameters, you can then call the indicate_inset_zoom() method with the same parameters. This will create a rectangular overlay on your figure indicating the zoomed in region. You can also specify other optional parameters such as color and linestyle to customize the appearance of your zoom effect:

Step 1: Create a figure

First, let's use matplotlib to create a basic scatter plot.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x1 = [0,1,2,2.05,2.10,3,4,5]
x2 = [0,1,2,2.05,2.10,3,4,5]

ax.scatter(x1,x2)

plt.title("How to create a zoom effect on a matplotlib figure ?")

plt.savefig('zoom_effect_matplotlib_1.png', dpi=100, bbox_inches='tight' )

plt.show()

How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?
How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?

We want to create a zoom feature for the data located in the center of the figure.

Step 2: Create a smaller figure

Matplotlib's "inset_axes" function allows users to place an axes on top of another axes. This is particularly useful when visualizing two related datasets side-by-side, or when a smaller chart needs to be placed within a larger one for comparison and contrast. With inset_axes, you can create a small plot with its own x and y axes located within the area of a larger plot. This makes it easy to compare values between the two plots, as well as add labels and other annotation that helps illustrate key points. To use inset_axes, you provide the coordinates for where you want the smaller plot to be placed (in fractions of the total figure size) and then supply your own data to create the plot:

import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid.inset_locator import inset_axes

fig, ax = plt.subplots()

x1 = [0,1,2,2.05,2.10,3,4,5]
x2 = [0,1,2,2.05,2.10,3,4,5]

ax.scatter(x1,x2)

axin = ax.inset_axes([0.75, 0.1, 0.2, 0.2])

axin.set_xlim(1.9,2.2)
axin.set_ylim(1.9,2.2)

axin.scatter(x1,x2)

plt.title("How to create a zoom effect on a matplotlib figure ?")

plt.savefig('zoom_effect_matplotlib_2.png', dpi=100, bbox_inches='tight' )

plt.show()

How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?
How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?

Step 3: Create a zoom effect using indicate_inset_zoom()

Then we can use the indicate_inset_zoom() method:

import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid.inset_locator import inset_axes

fig, ax = plt.subplots()

x1 = [0,1,2,2.05,2.10,3,4,5]
x2 = [0,1,2,2.05,2.10,3,4,5]

ax.scatter(x1,x2)

axin = ax.inset_axes([0.75, 0.1, 0.2, 0.2])

axin.set_xlim(1.9,2.2)
axin.set_ylim(1.9,2.2)

axin.scatter(x1,x2)

ax.indicate_inset_zoom(axin)

plt.title("How to create a zoom effect on a matplotlib figure ?")

plt.savefig('zoom_effect_matplotlib_3.png', dpi=100, bbox_inches='tight' )

plt.show()

How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?
How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?

Step 4: Remove ticks and labels

Note that we can also remove the labels and ticks

import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid.inset_locator import inset_axes

fig, ax = plt.subplots()

x1 = [0,1,2,2.05,2.10,3,4,5]
x2 = [0,1,2,2.05,2.10,3,4,5]

ax.scatter(x1,x2)

axin = ax.inset_axes([0.75, 0.1, 0.2, 0.2])

axin.set_xlim(1.9,2.2)
axin.set_ylim(1.9,2.2)

axin.scatter(x1,x2)

ax.indicate_inset_zoom(axin)

axin.set_yticklabels([])
axin.set_xticklabels([])

axin.set_yticks([])
axin.set_xticks([])

plt.title("How to create a zoom effect on a matplotlib figure ?")

plt.savefig('zoom_effect_matplotlib_4.png', dpi=100, bbox_inches='tight' )

plt.show()

How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?
How to create a zoom effect on a matplotlib figure using indicate_inset_zoom() ?

References

Image

of