To put a zoomed-in plot inside a Matplotlib chart, create a child axes with ax.inset_axes(), plot the same data on it, narrow its limits with set_xlim() and set_ylim(), and call ax.indicate_inset_zoom() to draw a box and connector lines.
Quick Example
A year of hourly server memory use, with one leaky week magnified:
import matplotlib.pyplot as plt
import numpy as np
rng = np.random.default_rng(8)
day = np.arange(0, 365, 1 / 24)
memory = 40 + 6 * np.sin(day / 58) + rng.normal(0, 1.2, day.size)
# A leak between days 200 and 207, with a restart each night
leak = (day >= 200) & (day < 207)
memory[leak] += 30 * ((day[leak] - 200) % 1)
fig, ax = plt.subplots(figsize=(9, 5), layout="constrained")
ax.plot(day, memory, color="#33658a", linewidth=0.6)
ax.set_ylim(25, 120)
# [left, bottom, width, height] as fractions of the parent plot
zoom = ax.inset_axes([0.06, 0.58, 0.42, 0.38])
zoom.plot(day, memory, color="#33658a", linewidth=1)
zoom.set_xlim(198, 209)
zoom.set_ylim(30, 72)
ax.indicate_inset_zoom(zoom, edgecolor="black")
ax.set_xlabel("Day of year")
ax.set_ylabel("Memory used (GB)")
ax.set_title("A one-week memory leak")
plt.show()

At full scale the leak is a small bump near day 200. The inset shows seven separate ramps, one per day.
Position and Size the Inset
The list passed to inset_axes() is [x0, y0, width, height]. By default these are fractions of the parent axes: (0, 0) is its bottom-left corner and (1, 1) its top-right. Pass transform=ax.transData to give the position in data units instead.
fig, ax = plt.subplots(figsize=(9, 4.5), layout="constrained")
ax.plot(day, memory, color="#bbbbbb", linewidth=0.6)
ax.set_ylim(0, 110)
corner = ax.inset_axes([0.07, 0.62, 0.25, 0.29])
corner.set_title("axes fraction", fontsize=10)
dated = ax.inset_axes([240, 70, 100, 30], transform=ax.transData)
dated.set_title("data units", fontsize=10)
outside = ax.inset_axes([1.08, 0.3, 0.3, 0.4])
outside.set_title("outside", fontsize=10)
plt.show()

Values above 1 or below 0 place the inset outside the plot, and layout="constrained" makes room for it.
Style the Zoom Box and Connector Lines
indicate_inset_zoom() draws the rectangle from the inset's current limits, so set the limits first (since Matplotlib 3.10 the rectangle also follows later changes). Pass line settings such as edgecolor, linewidth, and alpha. The default alpha is 0.5, which is why the lines can look faint.
fig, ax = plt.subplots(figsize=(9, 5), layout="constrained")
ax.plot(day, memory, color="#33658a", linewidth=0.6)
ax.set_ylim(25, 120)
zoom = ax.inset_axes([0.06, 0.58, 0.42, 0.38])
zoom.plot(day, memory, color="#f26419", linewidth=1)
zoom.set_xlim(198, 209)
zoom.set_ylim(30, 72)
zoom.set_facecolor("#fff8ee")
zoom.tick_params(labelsize=8)
indicator = ax.indicate_inset_zoom(zoom, edgecolor="#f26419", linewidth=2, alpha=1)
indicator.rectangle.set_facecolor("#f26419")
indicator.rectangle.set_alpha(0.15)
plt.show()

indicate_inset_zoom() returns an object with a rectangle and four connectors. Matplotlib shows the two connectors that don't cross the boxes; you can override that with indicator.connectors[i].set_visible(True). On Matplotlib 3.9 and older it returned a (rectangle, connectors) tuple instead.
Zoom Into an Image
The same steps work with imshow(). For images the y-axis runs downward, so pass the limits to set_ylim() as bottom then top, with the larger number first:
x = np.linspace(-3, 3, 600)
y = np.linspace(-2, 2, 400)
X, Y = np.meshgrid(x, y)
field = np.exp(-(X**2 + Y**2) / 2) + 0.25 * np.exp(
-((X - 1.9)**2 + (Y - 0.9)**2) / 0.05) * np.cos(40 * X)
fig, ax = plt.subplots(figsize=(8, 4.8), layout="constrained")
ax.imshow(field, cmap="viridis")
zoom = ax.inset_axes([0.03, 0.55, 0.3, 0.42])
zoom.imshow(field, cmap="viridis")
zoom.set_xlim(450, 540)
zoom.set_ylim(330, 250)
zoom.set_xticks([])
zoom.set_yticks([])
ax.indicate_inset_zoom(zoom, edgecolor="white", alpha=1)
plt.show()

Show the Zoom as a Separate Panel
If the inset would cover data, give the close-up its own subplot. The connectors from indicate_inset_zoom() join matching corners, which crosses the chart when panels are stacked. Draw your own with ConnectionPatch, from the bottom of the zoomed range in one panel to the top of the other:
from matplotlib.patches import ConnectionPatch
fig, (full, detail) = plt.subplots(
2, 1, figsize=(9, 5.5), height_ratios=[1, 1.3], layout="constrained")
for ax in (full, detail):
ax.plot(day, memory, color="#33658a", linewidth=0.7)
detail.set_xlim(198, 209)
detail.set_ylim(30, 72)
full.axvspan(198, 209, color="#f6ae2d", alpha=0.35)
for edge in (198, 209):
fig.add_artist(ConnectionPatch(
xyA=(edge, full.get_ylim()[0]), coordsA=full.transData,
xyB=(edge, 72), coordsB=detail.transData, color="#888888"))
full.set_title("Full year")
detail.set_title("Days 198 to 209")
plt.show()

For other multi-panel arrangements, see subplot_mosaic and GridSpec.
Practical Tips
- Leave headroom with
set_ylim()on the parent so the inset has somewhere to sit. ax.inset_axes()is built into theAxesclass. The olderzoomed_inset_axes()andmark_inset()frommpl_toolkits.axes_grid1.inset_locatorare still available if you want to specify a magnification factor instead of a size.- Keep tick labels on the inset. Without them, readers can't tell how large the zoomed region is.
- To remove an empty stretch of an axis instead of magnifying a region, use a broken axis.