Drawing inset plots with fig.add_axes()

It is not a must for subplots to align side by side. In some occasions, such as when zooming in or out, we can also embed subplots on top of the parent plot layer. This can be done by fig.add_axes(). To add a subplot, here is the basic usage: 

fig = plt.figure() # or fig = plt.gcf()
fig.add_axes([left, bottom, width, height])

The left, bottom, width, and height parameters are specified relative to the parent figure in terms of float. Note that fig.add_axes() returns an axes object, so you may store it as a variable such as ax = fig.add_axes([left, bottom, width, height]) for further adjustments.

The following is a complete example where we try to plot the overview in a smaller embedded subplot:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
np.random.seed(100)
# Prepare data
x = np.random.binomial(1000,0.6,1000)
y = np.random.binomial(1000,0.6,1000)
c = np.random.rand(1000)

# Draw the parent plot
ax = plt.scatter(x,y,s=1,c=c)
plt.xlim(580,650)
plt.ylim(580,650)

# Draw the inset subplot
ax_new = fig.add_axes([0.6, 0.6, 0.2, 0.2])
plt.scatter(x,y,s=1,c=c)
plt.show()

Let's check out the result in the figure:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.198.239