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:

pl.plot([1,4,3], label='A')
pl.plot([5,7,8], label='B')
pl.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
pl.legend() # Restore original legend order A, B, C