InsetPlot

class stonerplots.InsetPlot(ax=None, loc='best', width=0.33, height=0.33, dimension='fraction', switch_to_inset=True, padding=(0.02, 0.02))

Bases: _PreserveFigureMixin

A context manager for creating inset plots in matplotlib with minimal effort.

The InsetPlot class simplifies the process of generating inset plots that are properly positioned relative to the parent axes. It ensures that the inset labels do not overlap with the parent axes and provides flexibility in specifying the dimensions, position, and behavior of the inset plot.

Args:
ax (matplotlib.Axes, None):

The parent axes in which to create the inset plot. If None (default), the current axes from plt.gca() are used.

loc (str, int, None):

Location of the inset. Accepts location strings compatible with matplotlib legends (e.g., “upper left”, “center”, etc.) or their numeric equivalents. Includes “best” or “auto” (or 0) in which case an auto-placement algorithm is used. This is the default.

width (float):

The width of the inset. Units depend on the dimension argument. Default is 0.33.

height (float):

The height of the inset. Units depend on the dimension argument. Default is 0.33.

dimension (str):

Specifies whether width and height are given as fractions of the parent axes (“fraction”) or in inches. Default is “fraction”.

switch_to_inset (bool):

If True, the inset axes become the current axes within the context, and the parent axes are restored when the context ends. Default is True.

padding (float, float):

The padding between the inset and the parent axes. Default is 0.02,0.02.

Attributes:
locations (dict):

A dictionary mapping location strings (e.g., “upper right”) to their numeric equivalents.

Notes:
  • The dimension parameter determines whether the width and height of the inset are interpreted as fractions of the parent axes or as absolute values in inches.

  • The context manager ensures that matplotlib’s pyplot functions act on the inset axes while inside the context, making plotting within the inset more convenient.

  • After exiting the context, the placement of the inset is automatically adjusted to prevent overlaps with the parent axes labels.

Examples:

Creating an inset plot within a parent plot:

>>> fig, ax = plt.subplots()
>>> ax.plot([1, 2, 3], [4, 5, 6])
>>> with InsetPlot(ax=ax, loc="upper right", width=0.3, height=0.3) as inset_ax:
...     inset_ax.plot([1, 2, 3], [6, 5, 4])
>>> plt.show()

The inset plot is placed in the upper right corner of the parent plot.

Using an inset on the current axes:

>>> plt.plot([1, 2, 3], [4, 5, 6], label="Main Plot")
>>> with InsetPlot(loc="best", width=0.25, height=0.25) as inset_ax:
...     inset_ax.scatter([1, 2, 3], [10, 9, 8], color="red")
>>> plt.show()

Attributes Summary

locations

Attributes Documentation

locations = {'auto': 0, 'best': 0, 'center': 10, 'center left': 6, 'center right': 7, 'lower center': 8, 'lower left': 3, 'lower right': 4, 'right': 5, 'upper center': 9, 'upper left': 2, 'upper right': 1}