By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Minor, but annoying legend problem in matplotlib

P: n/a

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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
>>>>> "Jorl" == Jorl Shefner <v4***@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/ex...legend_demo.py

JDH
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.