Minor, but annoying legend problem in matplotlib

Jorl Shefner v451v at yahoo.com
Fri Feb 25 13:35:09 EST 2005


   I have a problem that I run into a lot with the 'legend' command's 
default behavior.  I've found a work-around but I wonder if there's a 
better way.

   For a simple example, take the following:
________________________________

    x= [1,2,3,4,5,6,7,8]
    a= [5,3,2,4,6,5,8,7]
    b= [4,1,3,5,2,8,3,6]
    c= [8,4,9,6,7,3,9,4]
    
    DataSets= [a,b,c]
      
    Symb= ['k-o','k--s','k-.^']
              
    for index,d in enumerate(DataSets):
              
        plot(x,DataSets[index],Symb[index])
              
    legend(["a","b","c"])
_______________________________

   This behaves just as I would want it to.  Normally, though I want 
'open' markers, which (AFAIK) require me to set the color to 'w' 
(white), and so the (white-on-white) lines won't show up in this case 
(the markers still have black outlines).  I tried using two colors in 
one marker definition, 'k-wo', but that didn't work.

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

    Symb= ['wo','ws','w^']
    LineType= ['k-','k--','k-.']
    
    for index,d in enumerate(DataSets):
        
        plot(x,DataSets[index],LineType[index])
        plot(x,DataSets[index],Symb[index])
        
    legend(["a","b","c"])    
_______________________________

   This produces the correct plot, but the legend here alternates 
between symbol and marker in its what uses for designating each dataset 
(a uses 'marker a', b uses 'line b', c uses 'marker c').
   Is there some rationale for this being the default behavior?  

   The workaround I've found has been to use two separate loops for the 
symbol and line plotting:

_______________________________


    Symb= ['wo','ws','w^']
    LineType= ['k-','k--','k-.']
    
    #Loop 1
    for index,d in enumerate(DataSets):
        
        plot(x,DataSets[index],LineType[index])

    # Loop 2
    for index,d in enumerate(DataSets):
        
        plot(x,DataSets[index],Symb[index])
        
    legend(["a","b","c"]) 
_______________________________ 

   This works and will give me a legend that uses only marker symbols in 
it, as desired.   It's not an ideal solution though as I often have some 
moderate amount of processing within the loop that I'd rather not have 
to repeat or write out to some temporary variable just in order to have 
it available for the second loop.
   I've gotten around this before for somewhat similar cases using 
suggestions from this group of explicitly defining the values the legend 
will use:

L1= plot(x,y,...

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

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

TIA,

J.S.

-- 
Actual e-mail:  <Firstname> 'dot' <Lastname> @comcast.net



More information about the Python-list mailing list