Minor, but annoying legend problem in matplotlib

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Feb 28 11:40:52 EST 2005


>>>>> "Jorl" == Jorl Shefner <v451v at yahoo.com> writes:

    Jorl>    The obvious solution is to plot the lines and symbols in
    Jorl> two different commands: _______________________________


You want to explicitly pass the lines you want to legend into the
    legend command, as in 

    Symb= ['wo','ws','w^']
    LineType= ['k-','k--','k-.']

    leglines = []    
    for index,d in enumerate(DataSets):
        
        plot(x,DataSets[index],LineType[index])
        lines = plot(x,DataSets[index],Symb[index])
        leglines.extend(lines)


        
    legend(leglines, ["a","b","c"])    

    Jorl> to have it available for the second loop.  I've gotten
    Jorl> around this before for somewhat similar cases using
    Jorl> suggestions from this group of explicitly defining the
    Jorl> values the legend will use:

    Jorl> L1= plot(x,y,...

    Jorl>  but I can't figure how to do this here because of the
    Jorl> looping over the data sets.

Hope the above example helps here.

    Jorl>    On a related note, is there any way to increase the size
    Jorl> of the markers within the legend?

You can access the lines of the legend instance

leg = legend(lines, labels)
lines = leg.get_lines()
set(lines, markersize=10, markeredgewidth=2)  # etc

See http://matplotlib.sourceforge.net/examples/legend_demo.py

JDH



More information about the Python-list mailing list