def impose_legend_limit ( limit = 30 , axes = "gca" , * * kwargs ) : if axes == "gca" : axes = _pylab . gca ( ) # make these axes current _pylab . axes ( axes ) # loop over all the lines_pylab. for n in range ( 0 , len ( axes . lines ) ) : if n > limit - 1 and not n == len ( axes . lines ) - 1 : axes . lines [ n ] . set_label ( "_nolegend_" ) if n == limit - 1 and not n == len ( axes . lines ) - 1 : axes . lines [ n ] . set_label ( "..." ) _pylab . legend ( * * kwargs )
mannually set legend python
This will erase all but say 30 of the legend entries and remake the legend . You ll probably have to move it back into your favorite position at this point .
def impose_legend_limit(limit=30, axes="gca", **kwargs):
    """
    This will erase all but, say, 30 of the legend entries and remake the legend.
    You'll probably have to move it back into your favorite position at this point.
    """
    if axes=="gca": axes = _pylab.gca()

    # make these axes current
    _pylab.axes(axes)

    # loop over all the lines_pylab.
    for n in range(0,len(axes.lines)):
        if n >  limit-1 and not n==len(axes.lines)-1: axes.lines[n].set_label("_nolegend_")
        if n == limit-1 and not n==len(axes.lines)-1: axes.lines[n].set_label("...")

    _pylab.legend(**kwargs)