I am trying to plot a number of subplots in matplotlib, each subplot should have an inset axes. I can get the code examples to work for a single axis with a inset axes added using
mpl_toolkits.axes_grid.inset_locator.inset_axes(), and I can plot subplots fine without the inset axes, but when trying to do the same for subplots in a loop, I get
TypeError: 'AxesHostAxes' object is not callable on the second subplot. This seems a bit strange that it should work when
number_of_plots is ==1, but not >1. How should I be doing it, or is it a bug? (
matplotlib.__version__ is ‘1.5.1’)
from matplotlib import pyplot as plt from mpl_toolkits.axes_grid.inset_locator import inset_axes import numpy as np x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) n_row, n_col = 4, 4 fig = plt.figure(1,(10,10)) #number_of_plots = 1 Works! number_of_plots = n_row * n_col # Does not work! for idx in range(number_of_plots): ax = fig.add_subplot(n_row, n_col, idx + 1) ax.plot(x, y) inset_axes = inset_axes(ax, width="30%", # width = 30% of parent_bbox height="30%", # height : 1 inch )
For future readers: This post shows methods to create insets in matplotlib.
Concrete answer to the question here: It’s not a bug. You are redefining
Before the line
inset_axes = inset_axes(...),
inset_axes is a function from
mpl_toolkits.axes_grid.inset_locator. After that,
inset_axes is the return of that function, which is an
The general advice is of course: Never call a variable by the same name as a function you import or use in your code.
The concrete solution:
ax_ins = inset_axes(ax, width="30%", height="30%")
Good looking out big dawg, that helped me with a similar issue.