plot legend behavior & question

Hi there -

I’m using the latest stable pylab/matplotlib (0.98.3) via OS X 10.5.4. I am plotting a simple array of data, and I’m getting some strange behavior when I try to add a legend to the plot.

First, the plot without the legend:

f=figure(1)
title(“Equilibrium Concentration vs. Equivalence Ratio”)
line1 = plot(phi,xeq[1])
show()

This works fine. However, when I try to add a legend, I get an error. I add this:

f=figure(1)

title(“Equilibrium Concentration vs. Equivalence Ratio”)

line1 = plot(phi,xeq[1])

f.legend(line1,(‘CO2’),loc=(0.8,0.8))
show()

And it gives me an error because of the second argument to f.legend:

···

ValueError Traceback (most recent call last)

/Users/charles/Documents/school/combustion/problem-2-52/adiabatic.py in ()
111 title(“Equilibrium Concentrations vs. Equivalence Ratio”)

112 line1=plot(phi,xeq[1])

–> 113 f.legend(line1,(‘CO2’),loc=(0.8,0.8))
114 show()
115

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/figure.pyc in legend(self, handles, labels, *args, **kwargs)

841         """
842         handles = flatten(handles)

–> 843 l = Legend(self, handles, labels, *args, **kwargs)
844 self.legends.append(l)
845 return l

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/legend.pyc in init(self, parent, handles, labels, loc, numpoints, prop, pad, markerscale, labelsep, handlelen, handletextsep, axespad, shadow)

180         textleft = left+ self.handlelen+self.handletextsep
181         self.texts = self._get_texts(labels, textleft, top)

–> 182 self.legendHandles = self._get_handles(handles, self.texts)

183
184         self._drawFrame = True

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/legend.pyc in _get_handles(self, handles, texts)
246 # centered marker proxy

247

–> 248 for handle, label in safezip(handles, texts):
249 if self.numpoints > 1:
250 xdata = np.linspace(left, left + self.handlelen, self.numpoints)

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/cbook.pyc in safezip(*args)
899 for i, arg in enumerate(args[1:]):
900 if len(arg) != Nx:
–> 901 raise ValueError(_safezip_msg % (Nx, i+1, len(arg)))

902     return zip(*args)
903

ValueError: In safezip, len(args[0])=1 but len(args[1])=3
WARNING: Failure executing file: <adiabatic.py>

I don’t understand why this doesn’t work, because I should be able to give a label of arbitrary length. It doesn’t work when I replace single quotes with double quotes, either. But I change the length to 1, like it wants:

f=figure(1)
title(“Equilibrium Concentrations vs. Equivalence Ratio”)
line1=plot(phi,xeq[1])
f.legend(line1,(‘C’),loc=(0.8,0.8))
show()

Which creates a plot, with a legend, but when I move the mouse over the plot, it has the “thinking” cursor (hourglass). I can’t close the window using the buttons in the upper left hand corner, I can’t close it using close(1), and I can’t close it using close(‘all’). I end up having to run ‘killall Python’ from a command line.

I can get the plot with the legend to work, albeit with only ONE character for the name, by running (from Python)

f=figure(1)

title(“Equilibrium Concentrations vs. Equivalence Ratio”)

line1=plot(phi,xeq[1])

f.legend(line1,(‘C’),loc=(0.8,0.8))

(without the show() command) in the script, then running show() when the script is done, and I don’t get the “thinking” cursor.

Note: I have also used the function figlegend() with the exact same results/problems.

  1. How can I give a legend name with a length of more than 1 character? What am I doing wrong?
  2. What is causing the behavior with the thinking cursor?

Also, ultimately I would like to make a plot with 30+ species, instead of just CO2. Is there a way to do this other than doing the following?

line1 line2 line3 … = plot(phi,xeq[1],phi,xeq[2],phi,xeq[3],…)
f.legend((line1,line2,…),(name[0],name[1],name[2],…),loc=(0.8,0.8))

Thanks ahead of time for any suggestions or hints.

Charles

Charles,

  The legend functions needs a sequence-type object (list,etc) not a tuple as the second argument. The following should work:
                f.legend(line1,['CO2'],loc=(0.8,0.8))
  Not sure about the cursor issue, see if the above solves the problem. You should be able to add a number of plots more effectively using a for loop:

lines =
names = ["CO2","H2O","etc"]
for y in xeq:
    lines.append( plot(phi,y) )
f.legend( lines,names,loc=(0.8,0.8) )

Cheers,

    -Jonathan Helmus
  
charles reid wrote:

···

Hi there -

I'm using the latest stable pylab/matplotlib (0.98.3) via OS X 10.5.4. <http://10.5.4.> I am plotting a simple array of data, and I'm getting some strange behavior when I try to add a legend to the plot.
First, the plot without the legend:

f=figure(1)
title("Equilibrium Concentration vs. Equivalence Ratio")
line1 = plot(phi,xeq[1])
show()

This works fine. However, when I try to add a legend, I get an error. I add this:

f=figure(1)
title("Equilibrium Concentration vs. Equivalence Ratio")
line1 = plot(phi,xeq[1])
f.legend(line1,('CO2'),loc=(0.8,0.8))
show()

And it gives me an error because of the second argument to f.legend:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/Users/charles/Documents/school/combustion/problem-2-52/adiabatic.py in <module>()
    111 title("Equilibrium Concentrations vs. Equivalence Ratio")
    112 line1=plot(phi,xeq[1])
--> 113 f.legend(line1,('CO2'),loc=(0.8,0.8))
    114 show()
    115

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/figure.pyc in legend(self, handles, labels, *args, **kwargs)
    841 """
    842 handles = flatten(handles)
--> 843 l = Legend(self, handles, labels, *args, **kwargs)
    844 self.legends.append(l)
    845 return l

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/legend.pyc in __init__(self, parent, handles, labels, loc, numpoints, prop, pad, markerscale, labelsep, handlelen, handletextsep, axespad, shadow)
    180 textleft = left+ self.handlelen+self.handletextsep
    181 self.texts = self._get_texts(labels, textleft, top)
--> 182 self.legendHandles = self._get_handles(handles, self.texts)
    183
    184 self._drawFrame = True

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/legend.pyc in _get_handles(self, handles, texts)
    246 # centered marker proxy
    247
--> 248 for handle, label in safezip(handles, texts):
    249 if self.numpoints > 1:
    250 xdata = np.linspace(left, left + self.handlelen, self.numpoints)

/Library/Python/2.5/site-packages/matplotlib-0.98.3-py2.5-macosx-10.3-i386.egg/matplotlib/cbook.pyc in safezip(*args)
    899 for i, arg in enumerate(args[1:]):
    900 if len(arg) != Nx:
--> 901 raise ValueError(_safezip_msg % (Nx, i+1, len(arg)))
    902 return zip(*args)
    903

ValueError: In safezip, len(args[0])=1 but len(args[1])=3
WARNING: Failure executing file: <adiabatic.py>

I don't understand why this doesn't work, because I should be able to give a label of arbitrary length. It doesn't work when I replace single quotes with double quotes, either. But I change the length to 1, like it wants:

f=figure(1)
title("Equilibrium Concentrations vs. Equivalence Ratio")
line1=plot(phi,xeq[1])
f.legend(line1,('C'),loc=(0.8,0.8))
show()

Which creates a plot, with a legend, but when I move the mouse over the plot, it has the "thinking" cursor (hourglass). I can't close the window using the buttons in the upper left hand corner, I can't close it using close(1), and I can't close it using close('all'). I end up having to run 'killall Python' from a command line.

I can get the plot with the legend to work, albeit with only ONE character for the name, by running (from Python)

f=figure(1)
title("Equilibrium Concentrations vs. Equivalence Ratio")
line1=plot(phi,xeq[1])
f.legend(line1,('C'),loc=(0.8,0.8))

(without the show() command) in the script, then running show() when the script is done, and I don't get the "thinking" cursor.

Note: I have also used the function figlegend() with the exact same results/problems.

1. How can I give a legend name with a length of more than 1 character? What am I doing wrong?
2. What is causing the behavior with the thinking cursor?

Also, ultimately I would like to make a plot with 30+ species, instead of just CO2. Is there a way to do this other than doing the following?

line1 line2 line3 ........ = plot(phi,xeq[1],phi,xeq[2],phi,xeq[3],...........)
f.legend((line1,line2,...........),(name[0],name[1],name[2],................),loc=(0.8,0.8))

Thanks ahead of time for any suggestions or hints.

Charles

------------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

legend expects a sequence of strings

  ('CO2') is a string

  ('CO2',) is a length one sequence of strings

  ['CO2'] is also a length 1 sequence of strings

When using parentheses to create a tuple, you must use a comma to
disambiguate the alternative meaning of grouping.

JDH

···

On Tue, Sep 16, 2008 at 4:09 PM, charles reid <charlesreid1@...287...> wrote:

Hi there -

I'm using the latest stable pylab/matplotlib (0.98.3) via OS X 10.5.4. I am
plotting a simple array of data, and I'm getting some strange behavior when
I try to add a legend to the plot.
First, the plot without the legend:

f=figure(1)
title("Equilibrium Concentration vs. Equivalence Ratio")
line1 = plot(phi,xeq[1])
show()

This works fine. However, when I try to add a legend, I get an error. I
add this:

f=figure(1)
title("Equilibrium Concentration vs. Equivalence Ratio")
line1 = plot(phi,xeq[1])
f.legend(line1,('CO2'),loc=(0.8,0.8))
show()

('CO2') is a string not a tuple.
('CO2',) is a tuple.

hth,
Alan Isaac

···

On Tue, 16 Sep 2008, charles reid apparently wrote:

f.legend(line1,('CO2'),loc=(0.8,0.8))