The result is:
In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
In [2]:
x = np.random.normal(0,1,1000)
y = np.random.normal(0,1,1000)
plot normally
In [3]:
fig, ax = plt.subplots(figsize=[5, 4])
ax.scatter(x, y, s=1)
ax.set_xlim(-3, 6)
ax.set_ylim(-3, 6)
plt.savefig('without_zoomed_inset_axes.png',dpi=200)
plt.show()
In [4]:
zoomed_inset_axes?
Signature: zoomed_inset_axes(parent_axes, zoom, loc=1, bbox_to_anchor=None, bbox_transform=None, axes_class=None, axes_kwargs=None, borderpad=0.5) Docstring: Create an anchored inset axes by scaling a parent axes. Parameters ---------- parent_axes : `matplotlib.axes.Axes` Axes to place the inset axes. zoom : float Scaling factor of the data axes. *zoom* > 1 will enlargen the coordinates (i.e., "zoomed in"), while *zoom* < 1 will shrink the coordinates (i.e., "zoomed out"). loc : int or string, optional, default to 1 Location to place the inset axes. The valid locations are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 bbox_to_anchor : tuple or `matplotlib.transforms.BboxBase`, optional Bbox that the inset axes will be anchored. Can be a tuple of [left, bottom, width, height], or a tuple of [left, bottom]. bbox_transform : `matplotlib.transforms.Transform`, optional Transformation for the bbox. if None, `parent_axes.transAxes` is used. axes_class : `matplotlib.axes.Axes` type, optional If specified, the inset axes created with be created with this class's constructor. axes_kwargs : dict, optional Keyworded arguments to pass to the constructor of the inset axes. Valid arguments include: adjustable: [ 'box' | 'datalim' | 'box-forced'] agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alpha: float (0.0 transparent through 1.0 opaque) anchor: [ 'C' | 'SW' | 'S' | 'SE' | 'E' | 'NE' | 'N' | 'NW' | 'W' ] animated: bool aspect: unknown autoscale_on: bool autoscalex_on: bool autoscaley_on: bool axes_locator: a callable object which takes an axes instance and renderer and returns a bbox. axisbelow: [ bool | 'line' ] clip_box: a `~.Bbox` instance clip_on: bool clip_path: [(`~matplotlib.path.Path`, `~.Transform`) | `~.Patch` | None] color_cycle: unknown contains: a callable function facecolor: color fc: color figure: `~.Figure` frame_on: bool gid: an id string label: object navigate: bool navigate_mode: unknown path_effects: `~.AbstractPathEffect` picker: [None | bool | float | callable] position: unknown rasterization_zorder: float or None rasterized: bool or None sketch_params: (scale: float, length: float, randomness: float) snap: bool or None title: unknown transform: `~.Transform` url: a url string visible: bool xbound: (lower: float, upper: float) xlabel: unknown xlim: (left: float, right: float) xmargin: unknown xscale: [ 'linear' | 'log' | 'symlog' | 'logit' | ... ] xticklabels: list of string labels xticks: list of tick locations. ybound: (lower: float, upper: float) ylabel: unknown ylim: (bottom: float, top: float) ymargin: unknown yscale: [ 'linear' | 'log' | 'symlog' | 'logit' | ... ] yticklabels: list of string labels yticks: list of tick locations. zorder: float borderpad : float, optional Padding between inset axes and the bbox_to_anchor. Defaults to 0.5. Returns ------- inset_axes : `axes_class` Inset axes object created. File: ****/lib/python3.6/site-packages/mpl_toolkits/axes_grid1/inset_locator.py Type: function
In [5]:
zoomed_inset_axes??
Signature: zoomed_inset_axes(parent_axes, zoom, loc=1, bbox_to_anchor=None, bbox_transform=None, axes_class=None, axes_kwargs=None, borderpad=0.5) Source: @docstring.dedent_interpd def zoomed_inset_axes(parent_axes, zoom, loc=1, bbox_to_anchor=None, bbox_transform=None, axes_class=None, axes_kwargs=None, borderpad=0.5): """ Create an anchored inset axes by scaling a parent axes. Parameters ---------- parent_axes : `matplotlib.axes.Axes` Axes to place the inset axes. zoom : float Scaling factor of the data axes. *zoom* > 1 will enlargen the coordinates (i.e., "zoomed in"), while *zoom* < 1 will shrink the coordinates (i.e., "zoomed out"). loc : int or string, optional, default to 1 Location to place the inset axes. The valid locations are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 bbox_to_anchor : tuple or `matplotlib.transforms.BboxBase`, optional Bbox that the inset axes will be anchored. Can be a tuple of [left, bottom, width, height], or a tuple of [left, bottom]. bbox_transform : `matplotlib.transforms.Transform`, optional Transformation for the bbox. if None, `parent_axes.transAxes` is used. axes_class : `matplotlib.axes.Axes` type, optional If specified, the inset axes created with be created with this class's constructor. axes_kwargs : dict, optional Keyworded arguments to pass to the constructor of the inset axes. Valid arguments include: %(Axes)s borderpad : float, optional Padding between inset axes and the bbox_to_anchor. Defaults to 0.5. Returns ------- inset_axes : `axes_class` Inset axes object created. """ if axes_class is None: axes_class = HostAxes if axes_kwargs is None: inset_axes = axes_class(parent_axes.figure, parent_axes.get_position()) else: inset_axes = axes_class(parent_axes.figure, parent_axes.get_position(), **axes_kwargs) axes_locator = AnchoredZoomLocator(parent_axes, zoom=zoom, loc=loc, bbox_to_anchor=bbox_to_anchor, bbox_transform=bbox_transform, borderpad=borderpad) inset_axes.set_axes_locator(axes_locator) _add_inset_axes(parent_axes, inset_axes) return inset_axes File: ****/lib/python3.6/site-packages/mpl_toolkits/axes_grid1/inset_locator.py Type: function
plot with zoomed inset axes
In [6]:
fig, ax = plt.subplots(figsize=[5, 4])
ax.scatter(x, y, s=1)
ax.set_xlim(-3, 6)
ax.set_ylim(-3, 6)
# generate inset axes
axins = zoomed_inset_axes(ax, 1.5, loc='upper right') # zoom = 1.5
# plot in the inset axes
axins.scatter(x, y, s=1)
# fix the x, y limit of the inset axes
axins.set_xlim(-1, 1)
axins.set_ylim(-1, 1)
# fix the number of ticks on the inset axes
axins.yaxis.get_major_locator().set_params(nbins=3)
axins.xaxis.get_major_locator().set_params(nbins=3)
plt.savefig('with_zoomed_inset_axes.png',dpi=200)
plt.show()