Minor, but annoying legend problem in matplotlib

J

Jorl Shefner

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.
 
J

John Hunter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top