I have a figure with some inset axes at variable positions. Each of these inset axes has the same lines and thus the same legend.
To avoid cluttering the figure, I would like to only show the legend of one of these insets and position it (for example) in the top right corner of the main ax to indicate that this is a general legend.
This is a simplified version of my code:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np
fig, ax = plt.subplots(1, 1)
ax.plot(range(10), range(10))
inset_ax1 = inset_axes(ax,
width=1.5,
height=1.5,
loc='lower right')
inset_ax2 = inset_axes(ax,
width=2,
height=2,
loc='upper left')
inset_ax1.plot(range(10), np.random.rand(10,3))
inset_ax2.plot(range(10), np.random.rand(10,3))
inset_ax1.legend(['a', 'b', 'c'], loc='upper right')
plt.show()
I tried using ax.add_artist(inset_ax1.legend())
but this gives the following warning: ValueError: Can not reset the axes. You are probably trying to re-use an artist in more than one Axes which is not supported
. How can i resolve this?
To give some context: I’m making a figure similar to this but I would like to show a single legend for all the windroses. The position and number of insets is variable.