Open In App
Related Articles

Matplotlib.axes.Axes.inset_axes() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.inset_axes() Function

The Axes.inset_axes() function in axes module of matplotlib library is also used to add a child inset axes to this existing axes.
Syntax: Axes.inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs) Parameters: This method accept the following parameters that are described below:
  • bounds: This parameter is the Lower-left corner of inset axes, and its width and height.[x0, y0, width, height]
  • transform: This parameter is the units of rect are in axes-relative coordinates.
  • zorder: This parameter contains the number and its default value is 5.
Returns: This method returns the ax which is the created Axes instance.
Note: This function works in Matplotlib version >= 3.0 Below examples illustrate the matplotlib.axes.Axes.inset_axes() function in matplotlib.axes: Example 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
   
fig, ax = plt.subplots()
ax.plot(range(10))
axin1 = ax.inset_axes([0.8, 0.1
                       0.15, 0.15])
axin2 = ax.inset_axes(
        [5, 7, 2.3, 2.3], transform = ax.transData)
  
ax.set_title('matplotlib.axes.Axes.inset_axes() Example',
              fontsize = 14, fontweight ='bold')
plt.show()

                    
Output: Example 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
  
def geeks():
    from matplotlib.cbook import get_sample_data
    import numpy as np
    f = get_sample_data("axes_grid / bivariate_normal.npy"
                        asfileobj = False)
    z = np.load(f)
    return z, (-3, 4, -4, 3)
  
fig, ax = plt.subplots()
  
X, extent = geeks()
Z2 = np.zeros([150, 150], dtype ="g")
ny, nx = X.shape
Z2[30:30 + ny, 30:30 + nx] = X
  
ax.imshow(Z2, extent = extent, 
          interpolation ="nearest",
          origin ="lower", cmap ="Greens")
  
axins = ax.inset_axes([0.5, 0.5, 0.47, 0.47])
  
axins.imshow(Z2, extent = extent,
            interpolation ="nearest",
            origin ="lower", cmap ="Greens")
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
  
ax.indicate_inset_zoom(axins)
  
ax.set_title('matplotlib.axes.Axes.inset_axes() Example',
             fontsize = 14, fontweight ='bold')
plt.show()

                    
Output:

Last Updated : 19 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads