orderlegend#

orderlegend(order=None, ax=None, handles=None, labels=None, reverse=None, **kwargs)[source]#

Create a legend with a specified order, or change the order of an existing legend. Can either specify an order, or use the reverse argument to simply reverse the order. Note: you do not need to create the legend before calling this function; if you do, you will need to pass any additional keyword arguments to this function since it will override existing settings.

Parameters:
  • order (list or array) – the new order of the legend, as from e.g. np.argsort()

  • ax (axes) – the axes object; if omitted, defaults to current axes

  • handles (list) – the legend handles; can be used instead of ax

  • labels (list) – the legend labels; can be used instead of ax

  • reverse (bool) – if supplied, simply reverse the legend order

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

Examples:

plt.plot([1,4,3], label='A')
plt.plot([5,7,8], label='B')
plt.plot([2,5,2], label='C')
sc.orderlegend(reverse=True) # Legend order C, B, A
sc.orderlegend([1,0,2], frameon=False) # Legend order B, A, C with no frame
plt.legend() # Restore original legend order A, B, C