If you want to insert a small plot inside a bigger one you can use Axes, like here.
The problem is that I don't know how to do the same inside a subplot.
I have several subplots and I would like to plot a small plot inside each subplot. The example code would be something like this:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
for i in range(4):
ax = fig.add_subplot(2,2,i)
ax.plot(np.arange(11),np.arange(11),'b')
#b = ax.axes([0.7,0.7,0.2,0.2])
#it gives an error, AxesSubplot is not callable
#b = plt.axes([0.7,0.7,0.2,0.2])
#plt.plot(np.arange(3),np.arange(3)+11,'g')
#it plots the small plot in the selected position of the whole figure, not inside the subplot
Any ideas?
Thanks in advance!
source: https://matplotlib.org/examples/pylab_examples/axes_demo.html
From matplotlib 3.0 on, you can use
matplotlib.axes.Axes.inset_axes
:The difference to
mpl_toolkits.axes_grid.inset_locator.inset_axes
mentionned in @jrieke's answer is that this is a lot easier to use (no extra imports etc.), but has the drawback of being slightly less flexible (no argument for padding or corner locations).I wrote a function very similar to plt.axes. You could use it for plotting yours sub-subplots. There is an example...
You can now do this with matplotlibs
inset_axes
method (see docs):Update: As Kuti pointed out, for matplotlib version 2.1 or above, you should change the import statement to:
There is now also a full example showing all different options available.