Different X and Y scales in zoomed inset in Matplotlib

To show different X and Y scales in zoomed inset in Matplotlib, we can use the inset_axes() method from mpl_toolkits.axes_grid1.inset_locator. This allows us to create a magnified view of a specific region with custom scaling.

Creating a Basic Inset with Different Scales

Here's how to create a zoomed inset that focuses on a specific region of your plot ?

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, inset_axes

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create data points
x = np.linspace(0, 1, 100)
y = x ** 2

# Create main plot
ax = plt.subplot(1, 1, 1)
ax.plot(x, y, 'b-', linewidth=2, label='y = x²')
ax.set_xlabel('X values')
ax.set_ylabel('Y values')
ax.set_title('Main Plot with Zoomed Inset')

# Create inset axes
axins = inset_axes(ax, width=1, height=1, loc=2, 
                   bbox_to_anchor=(0.2, 0.55),
                   bbox_transform=ax.figure.transFigure)

# Plot same data in inset
axins.plot(x, y, 'b-', linewidth=2)

# Define zoom region
x1, x2 = 0.4, 0.6
y1, y2 = x1 ** 2, x2 ** 2

# Set different scales for the inset
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)

# Mark the inset region on main plot
mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec="0.5")

plt.show()
[A plot showing a parabola with a zoomed inset focusing on the region between x=0.4 and x=0.6]

Key Parameters

inset_axes() Parameters

  • width, height − Size of the inset in inches
  • loc − Location code (1=upper right, 2=upper left, 3=lower left, 4=lower right)
  • bbox_to_anchor − Position tuple (x, y) for precise placement
  • bbox_transform − Coordinate system for positioning

mark_inset() Parameters

  • loc1, loc2 − Corner positions to connect with lines
  • fc − Face color of connecting lines
  • ec − Edge color of connecting lines

Advanced Example with Custom Scaling

Here's a more complex example showing how to create multiple insets with different scales ?

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, inset_axes

# Create more complex data
x = np.linspace(0, 10, 1000)
y = np.sin(x) * np.exp(-x/5)

fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y, 'b-', linewidth=1.5)
ax.set_xlabel('Time')
ax.set_ylabel('Amplitude')
ax.set_title('Damped Sine Wave with Multiple Insets')
ax.grid(True, alpha=0.3)

# First inset - early oscillations
axins1 = inset_axes(ax, width=1.5, height=1, loc=1)
axins1.plot(x, y, 'b-', linewidth=1.5)
axins1.set_xlim(0, 2)
axins1.set_ylim(-0.5, 1)
axins1.grid(True, alpha=0.3)
mark_inset(ax, axins1, loc1=2, loc2=4, fc="none", ec="red")

# Second inset - decay region
axins2 = inset_axes(ax, width=1.5, height=1, loc=4)
axins2.plot(x, y, 'b-', linewidth=1.5)
axins2.set_xlim(6, 8)
axins2.set_ylim(-0.1, 0.1)
axins2.grid(True, alpha=0.3)
mark_inset(ax, axins2, loc1=1, loc2=3, fc="none", ec="green")

plt.tight_layout()
plt.show()
[A plot showing a damped sine wave with two zoomed insets highlighting different regions with different scales]

Comparison of Positioning Methods

Method Coordinate System Best For
loc parameter only Relative to axes Quick positioning
bbox_to_anchor Figure coordinates Precise placement
bbox_transform Custom transform Advanced positioning

Conclusion

Use inset_axes() to create zoomed views with different X and Y scales. The mark_inset() function helps visualize the connection between the main plot and the inset region. This technique is particularly useful for highlighting specific features in complex datasets.

Updated on: 2026-03-25T23:11:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements