movelegend#

movelegend(ax1, ax2=None, invisible=True, **kwargs)[source]#

Move the legend from one axes to another, preserving properties.

Note 1: does not require the first legend to actually exist, just that handles exist (e.g. by plotting with the “label” argument); see example below.

Note 2: see seaborn.move_legend() for a similar function.

Parameters:
  • ax1 (plt.Axes) – the axes to move the legend from

  • ax2 (plt.Axes) – the axes to move the legend to (if None, use current axes)

  • invisible (bool) – if True, set the axis to be invisible (just showing the legend) if no artists are in the destination axes

  • kwargs (dict) – passed to plt.legend()

Example:

import numpy as np
import sciris as sc

fig, axs = sc.getrowscols(3, make=True)

for i,ax in enumerate(fig.axes):
    for j in range(4):
        ax.plot(np.random.rand(50)*(1+j), 'o', label=f'Scale = {j}')

ax4 = fig.add_subplot(2,2,4)
sc.movelegend(ax, ax4) # Can be any of the axes since they have the same legend
New in version 3.2.2.