FAQ
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

Search Discussions

  • John Hunter at Feb 28, 2005 at 4:40 pm
    "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, markeredgewidth=2) # etc

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

    JDH

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 25, '05 at 6:35p
activeFeb 28, '05 at 4:40p
posts2
users2
websitepython.org

2 users in discussion

John Hunter: 1 post Jorl Shefner: 1 post

People

Translate

site design / logo © 2023 Grokbase