Fundamentals 8 min read

Creating Inset Zoomed Plots in Matplotlib for Detailed Comparison

This tutorial explains how to use Python's Matplotlib library to embed a zoomed inset subplot, configure its size and position, plot the same data inside it, set custom axis limits, and connect the inset to the main figure with visual cues.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating Inset Zoomed Plots in Matplotlib for Detailed Comparison

When comparing experiments, it is often necessary to zoom into a specific region of a plot to observe finer details.

Tool : Python's Matplotlib library.

Steps :

1. Import dependencies

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.patches import ConnectionPatch

2. Prepare data

The reward array reward_demaddpg[] stores 300 reward results from the DEMADDPG algorithm. The x‑axis is set up as:

MAX_EPISODES = 300
x_axis_data = []
for l in range(MAX_EPISODES):
    x_axis_data.append(l)

Five reward series corresponding to different learning rates are plotted.

3. Plot the main figure

fig, ax = plt.subplots(1, 1)
ax.plot(x_axis_data, reward_demaddpg5, color='#4169E1', alpha=0.8, label='$1*10^{-5}$')
ax.plot(x_axis_data, reward_demaddpg10, color='#848484', alpha=0.8, label='$5*10^{-6}$')
ax.plot(x_axis_data, reward_demaddpg15, color='#FF774A', alpha=0.8, label='$1*10^{-6}$')
ax.plot(x_axis_data, reward_demaddpg20, color='#575B20', alpha=0.8, label='$5*10^{-7}$')
ax.plot(x_axis_data, reward_demaddpg25, color='#B84D37', alpha=0.8, label='$1*10^{-7}$')
ax.legend(loc="lower right")
ax.set_xlabel('Episodes')
ax.set_ylabel('Total reward')

4. Embed an inset axes for the zoomed region

axins = inset_axes(ax, width="40%", height="30%", loc='lower left',
                   bbox_to_anchor=(0.3, 0.1, 1, 1),
                   bbox_transform=ax.transAxes)

The parameters are explained: ax is the parent axes, width and height define the size of the inset, loc its position, bbox_to_anchor the bounding box, and bbox_transform the coordinate transformation.

A more concise way to create the inset is:

axins = ax.inset_axes((0.2, 0.2, 0.4, 0.3))

5. Plot the same data inside the inset

axins.plot(x_axis_data, reward_demaddpg5, color='#4169E1', alpha=0.8, label='$1*10^{-5}$')
axins.plot(x_axis_data, reward_demaddpg10, color='#848484', alpha=0.8, label='$5*10^{-6}$')
axins.plot(x_axis_data, reward_demaddpg15, color='#FF774A', alpha=0.8, label='$1*10^{-6}$')
axins.plot(x_axis_data, reward_demaddpg20, color='#575B20', alpha=0.8, label='$5*10^{-7}$')
axins.plot(x_axis_data, reward_demaddpg25, color='#B84D37', alpha=0.8, label='$1*10^{-7}$')

6. Set the zoomed region limits

# Set zoom region
zone_left = 100
zone_right = 150

# Axis expansion ratios
x_ratio = 0
y_ratio = 0.05

xlim0 = x_axis_data[zone_left]-(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio
xlim1 = x_axis_data[zone_right]+(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio

y = np.hstack((reward_demaddpg5[zone_left:zone_right],
               reward_demaddpg10[zone_left:zone_right],
               reward_demaddpg15[zone_left:zone_right],
               reward_demaddpg20[zone_left:zone_right],
               reward_demaddpg25[zone_left:zone_right]))
ylim0 = np.min(y)-(np.max(y)-np.min(y))*y_ratio
ylim1 = np.max(y)+(np.max(y)-np.min(y))*y_ratio

axins.set_xlim(xlim0, xlim1)
axins.set_ylim(ylim0, ylim1)

7. Connect the inset with the main plot using ConnectionPatch

# Draw rectangle on main plot
tx0 = xlim0
tx1 = xlim1
ty0 = ylim0
ty1 = ylim1
sx = [tx0, tx1, tx1, tx0, tx0]
sy = [ty0, ty0, ty1, ty1, ty0]
ax.plot(sx, sy, "black")

# Draw connecting lines
xy = (xlim0, ylim0)
xy2 = (xlim0, ylim1)
con = ConnectionPatch(xyA=xy2, xyB=xy, coordsA="data", coordsB="data",
                      axesA=axins, axesB=ax)
axins.add_artist(con)

xy = (xlim1, ylim0)
xy2 = (xlim1, ylim1)
con = ConnectionPatch(xyA=xy2, xyB=xy, coordsA="data", coordsB="data",
                      axesA=axins, axesB=ax)
axins.add_artist(con)

The rectangle and connecting lines highlight the zoomed area, and the final figure shows the main curve with an enlarged inset.

This method provides a simple way to create detailed zoomed sub‑plots in Matplotlib.

data-visualizationMatplotlibZoomInset Plot
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.