usetex=True

[I'm bringing this back to the list. Hope you don't mind.]

I've been doing a lot of non-matplotlib work lately and this fell off my radar. Thanks for the reminder.

The patch looks good to me. I'm not concerned about doing all this in _get_handles as opposed to Legend.__init__. I actually think it's better in _get_handles because then it's more connected to the creation of the handles themselves. (And of course _get_handles is only ever called from __init__ anyway, so it really is just an organizational extension of __init__). I moved all references to _xdata to _get_handles to be consistent.

The one concern I had is that _xdata is being stored as a member variable, which implicitly assumes that it will be the same for all handles. After your patch, it can be different for each handle -- for instance a line made of markers vs. dashing. So I've just made it a local variable to _get_handles and recreate it separately for each handle. There's no reason to try to share something so small, and it just feels safer (fewer potential side effects) that way.

I've committed this in r4871. Please check it out and kick the tires with all the test cases you came up with along the way and let me know how it goes.

Cheers,
Mike

Paul Novak wrote:

···

Mike,

Thank you for handling the PS backend issues. Below is an email I sent earlier about an updated patch for fixing legends with numpoints=1; I didn't get any response probably because of my delay in responding. If I should restart this thread on the mailing list, I can.

Included below is an updated patch fixing the legend with numpoints=1. The patch has been made against SVN and works for Line2D, LineCollection, Patch, and RegularPolyCollection. The patch could be merged.

I also think the patch could be improved. Currently, handle._marker is examined to determine if the legend should contain lines or symbols, but this is done in the _get_handles function. It would be better if that could be moved into the Legend class definition, however, I do not know how to examine handle._marker in the Legend class definition.

Again, I would appreciate any comments or improvements.

Thanks,

Paul

diff -u a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py
--- a/lib/matplotlib/legend.py 2008-01-09 13:11:00.000000000 -0600
+++ b/lib/matplotlib/legend.py 2008-01-09 13:08:36.000000000 -0600
@@ -175,9 +175,7 @@
         # make a trial box in the middle of the axes. relocate it
         # based on it's bbox
         left, top = 0.5, 0.5
- if self.numpoints == 1:
- self._xdata = npy.array([left + self.handlelen*0.5])
- else:
+ if self.numpoints > 1:
             self._xdata = npy.linspace(left, left + self.handlelen, self.numpoints)
         textleft = left+ self.handlelen+self.handletextsep
         self.texts = self._get_texts(labels, textleft, top)
@@ -236,6 +234,7 @@

     def _get_handles(self, handles, texts):
         HEIGHT = self._approx_text_height()
+ left = 0.5

         ret = [] # the returned legend lines

@@ -243,6 +242,10 @@
             x, y = label.get_position()
             x -= self.handlelen + self.handletextsep
             if isinstance(handle, Line2D):
+ if self.numpoints == 1 and handle._marker == 'None':
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
+ elif self.numpoints == 1:
+ self._xdata = npy.array([left + self.handlelen*0.5])
                 ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
                 legline = Line2D(self._xdata, ydata)
                 legline.update_from(handle)
@@ -253,7 +256,8 @@

                 ret.append(legline)
             elif isinstance(handle, Patch):
-
+ if self.numpoints == 1:
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
                 p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )
@@ -263,6 +267,8 @@
                 p.set_clip_path(None)
                 ret.append(p)
             elif isinstance(handle, LineCollection):
+ if self.numpoints == 1:
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
                 ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
                 legline = Line2D(self._xdata, ydata)
                 self._set_artist_props(legline)
@@ -277,6 +283,8 @@
                 ret.append(legline)

             elif isinstance(handle, RegularPolyCollection):
+ if self.numpoints == 1:
+ self._xdata = npy.array([left])
                 p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )

Michael Droettboom wrote:

Thanks for pointing that out -- I didn't even notice.

There was an ordering problem in how colors were being set. This should be fixed in r4870.

Cheers,
Mike

Paul Novak wrote:

I have another problem after updating to SVN r4869. The ticks take the color of the plotted line without regard to the default. For example,

matplotlib.rc('xtick', color='black')
matplotlib.rc('ytick', color='black')
plot(x, y, 'red')

will give red ticks.

Paul

Michael Droettboom wrote:

It's a bug. It's now possible for rgba colors to make their way all the way down to that level, so the code needs to truncate that to rgb (Ps can't handle alpha anyway).

This is committed in SVN r4869.

Cheers,
Mike

Paul Novak wrote:

Hello,

I am trying to use the PS backend to make some simple line plots, using the following script, but I get the error messages included below when I try to plot with usetex=True. I have also included the output from --verbose=helpful. I am using the current SVN, and I don't recall having this problem when using 0.91.1.

Thanks,
Paul

---
#!/usr/bin/env python

import matplotlib
matplotlib.use('PS')
matplotlib.rc('text', usetex = 'True')
from pylab import *

x = arange(0,5)
y = 2 * x
figure(1)
plot(x, y)
savefig('psfig')
show()

---
$HOME=/home/pnovak
CONFIGDIR=/home/pnovak/.matplotlib
matplotlib data path /usr/lib/python2.5/site-packages/matplotlib/mpl-data
loaded rc file /home/pnovak/.matplotlib/matplotlibrc
matplotlib version 0.98pre
verbose.level helpful
interactive is False
units is False
platform is linux2
numerix numpy 1.0.3.1
Using fontManager instance from /home/pnovak/.matplotlib/fontManager.cache
backend PS version Level II
Found dvipng version 1.5

Traceback (most recent call last):
   File "junk.py", line 15, in <module>
     savefig('psfig')
   File "/usr/lib/python2.5/site-packages/matplotlib/pyplot.py", line 271, in savefig
     return fig.savefig(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 854, in savefig
     self.canvas.print_figure(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 1085, in print_figure
     **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 837, in print_ps
     return self._print_ps(outfile, 'ps', *args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 863, in _print_ps
     orientation, isLandscape, papertype)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 1051, in _print_figure_tex
     self.figure.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
     for a in self.axes: a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
     a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 680, in draw
     tick.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 179, in draw
     self.label1.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/text.py", line 761, in draw
     Text.draw(self, renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/text.py", line 309, in draw
     self._fontproperties, angle)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 544, in draw_tex
     color = '%1.3f,%1.3f,%1.3f'% gc.get_rgb()
TypeError: not all arguments converted during string formatting

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

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

The patch for legends works except for LineCollection. For any call to legend(), I get the errors below,

Paul

Traceback (most recent call last):
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 331, in expose_event
     self._render_figure(self._pixmap, w, h)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
     FigureCanvasAgg.draw(self)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py", line 257, in draw
     self.figure.draw(self.renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
     for a in self.axes: a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
     a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/legend.py", line 206, in draw
     h.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 480, in draw
     lineFunc(renderer, gc, tpath, affine.frozen())
   File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 740, in _draw_dashed
     renderer.draw_path(gc, path, trans)
ValueError: Dash sequence must be an even length sequence; found 1
Traceback (most recent call last):
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 331, in expose_event
     self._render_figure(self._pixmap, w, h)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
     FigureCanvasAgg.draw(self)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py", line 257, in draw
     self.figure.draw(self.renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
     for a in self.axes: a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
     a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/legend.py", line 206, in draw
     h.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 480, in draw
     lineFunc(renderer, gc, tpath, affine.frozen())
   File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 740, in _draw_dashed
     renderer.draw_path(gc, path, trans)
ValueError: Dash sequence must be an even length sequence; found 1
Traceback (most recent call last):
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 331, in expose_event
     self._render_figure(self._pixmap, w, h)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
     FigureCanvasAgg.draw(self)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py", line 257, in draw
     self.figure.draw(self.renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
     for a in self.axes: a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
     a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/legend.py", line 206, in draw
     h.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 480, in draw
     lineFunc(renderer, gc, tpath, affine.frozen())
   File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 740, in _draw_dashed
     renderer.draw_path(gc, path, trans)
ValueError: Dash sequence must be an even length sequence; found 1

Michael Droettboom wrote:

···

[I'm bringing this back to the list. Hope you don't mind.]

I've been doing a lot of non-matplotlib work lately and this fell off my radar. Thanks for the reminder.

The patch looks good to me. I'm not concerned about doing all this in _get_handles as opposed to Legend.__init__. I actually think it's better in _get_handles because then it's more connected to the creation of the handles themselves. (And of course _get_handles is only ever called from __init__ anyway, so it really is just an organizational extension of __init__). I moved all references to _xdata to _get_handles to be consistent.

The one concern I had is that _xdata is being stored as a member variable, which implicitly assumes that it will be the same for all handles. After your patch, it can be different for each handle -- for instance a line made of markers vs. dashing. So I've just made it a local variable to _get_handles and recreate it separately for each handle. There's no reason to try to share something so small, and it just feels safer (fewer potential side effects) that way.

I've committed this in r4871. Please check it out and kick the tires with all the test cases you came up with along the way and let me know how it goes.

Cheers,
Mike

Paul Novak wrote:

Mike,

Thank you for handling the PS backend issues. Below is an email I sent earlier about an updated patch for fixing legends with numpoints=1; I didn't get any response probably because of my delay in responding. If I should restart this thread on the mailing list, I can.

Included below is an updated patch fixing the legend with numpoints=1. The patch has been made against SVN and works for Line2D, LineCollection, Patch, and RegularPolyCollection. The patch could be merged.

I also think the patch could be improved. Currently, handle._marker is examined to determine if the legend should contain lines or symbols, but this is done in the _get_handles function. It would be better if that could be moved into the Legend class definition, however, I do not know how to examine handle._marker in the Legend class definition.

Again, I would appreciate any comments or improvements.

Thanks,

Paul

diff -u a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py
--- a/lib/matplotlib/legend.py 2008-01-09 13:11:00.000000000 -0600
+++ b/lib/matplotlib/legend.py 2008-01-09 13:08:36.000000000 -0600
@@ -175,9 +175,7 @@
         # make a trial box in the middle of the axes. relocate it
         # based on it's bbox
         left, top = 0.5, 0.5
- if self.numpoints == 1:
- self._xdata = npy.array([left + self.handlelen*0.5])
- else:
+ if self.numpoints > 1:
             self._xdata = npy.linspace(left, left + self.handlelen, self.numpoints)
         textleft = left+ self.handlelen+self.handletextsep
         self.texts = self._get_texts(labels, textleft, top)
@@ -236,6 +234,7 @@

     def _get_handles(self, handles, texts):
         HEIGHT = self._approx_text_height()
+ left = 0.5

         ret = [] # the returned legend lines

@@ -243,6 +242,10 @@
             x, y = label.get_position()
             x -= self.handlelen + self.handletextsep
             if isinstance(handle, Line2D):
+ if self.numpoints == 1 and handle._marker == 'None':
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
+ elif self.numpoints == 1:
+ self._xdata = npy.array([left + self.handlelen*0.5])
                 ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
                 legline = Line2D(self._xdata, ydata)
                 legline.update_from(handle)
@@ -253,7 +256,8 @@

                 ret.append(legline)
             elif isinstance(handle, Patch):
-
+ if self.numpoints == 1:
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
                 p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )
@@ -263,6 +267,8 @@
                 p.set_clip_path(None)
                 ret.append(p)
             elif isinstance(handle, LineCollection):
+ if self.numpoints == 1:
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
                 ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
                 legline = Line2D(self._xdata, ydata)
                 self._set_artist_props(legline)
@@ -277,6 +283,8 @@
                 ret.append(legline)

             elif isinstance(handle, RegularPolyCollection):
+ if self.numpoints == 1:
+ self._xdata = npy.array([left])
                 p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )

Michael Droettboom wrote:

Thanks for pointing that out -- I didn't even notice.

There was an ordering problem in how colors were being set. This should be fixed in r4870.

Cheers,
Mike

Paul Novak wrote:

I have another problem after updating to SVN r4869. The ticks take the color of the plotted line without regard to the default. For example,

matplotlib.rc('xtick', color='black')
matplotlib.rc('ytick', color='black')
plot(x, y, 'red')

will give red ticks.

Paul

Michael Droettboom wrote:

It's a bug. It's now possible for rgba colors to make their way all the way down to that level, so the code needs to truncate that to rgb (Ps can't handle alpha anyway).

This is committed in SVN r4869.

Cheers,
Mike

Paul Novak wrote:

Hello,

I am trying to use the PS backend to make some simple line plots, using the following script, but I get the error messages included below when I try to plot with usetex=True. I have also included the output from --verbose=helpful. I am using the current SVN, and I don't recall having this problem when using 0.91.1.

Thanks,
Paul

---
#!/usr/bin/env python

import matplotlib
matplotlib.use('PS')
matplotlib.rc('text', usetex = 'True')
from pylab import *

x = arange(0,5)
y = 2 * x
figure(1)
plot(x, y)
savefig('psfig')
show()

---
$HOME=/home/pnovak
CONFIGDIR=/home/pnovak/.matplotlib
matplotlib data path /usr/lib/python2.5/site-packages/matplotlib/mpl-data
loaded rc file /home/pnovak/.matplotlib/matplotlibrc
matplotlib version 0.98pre
verbose.level helpful
interactive is False
units is False
platform is linux2
numerix numpy 1.0.3.1
Using fontManager instance from /home/pnovak/.matplotlib/fontManager.cache
backend PS version Level II
Found dvipng version 1.5

Traceback (most recent call last):
   File "junk.py", line 15, in <module>
     savefig('psfig')
   File "/usr/lib/python2.5/site-packages/matplotlib/pyplot.py", line 271, in savefig
     return fig.savefig(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 854, in savefig
     self.canvas.print_figure(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 1085, in print_figure
     **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 837, in print_ps
     return self._print_ps(outfile, 'ps', *args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 863, in _print_ps
     orientation, isLandscape, papertype)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 1051, in _print_figure_tex
     self.figure.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
     for a in self.axes: a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
     a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 680, in draw
     tick.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 179, in draw
     self.label1.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/text.py", line 761, in draw
     Text.draw(self, renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/text.py", line 309, in draw
     self._fontproperties, angle)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 544, in draw_tex
     color = '%1.3f,%1.3f,%1.3f'% gc.get_rgb()
TypeError: not all arguments converted during string formatting

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

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks. I think that's actually unrelated to your patch -- but is new since 0.91.2. Fixed in SVN r4872.

Thanks for your help,
Mike

Paul Novak wrote:

···

The patch for legends works except for LineCollection. For any call to legend(), I get the errors below,

Paul

Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 331, in expose_event
    self._render_figure(self._pixmap, w, h)
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py", line 257, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
    for a in self.axes: a.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
    a.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/legend.py", line 206, in draw
    h.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 480, in draw
    lineFunc(renderer, gc, tpath, affine.frozen())
  File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 740, in _draw_dashed
    renderer.draw_path(gc, path, trans)
ValueError: Dash sequence must be an even length sequence; found 1
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 331, in expose_event
    self._render_figure(self._pixmap, w, h)
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py", line 257, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
    for a in self.axes: a.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
    a.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/legend.py", line 206, in draw
    h.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 480, in draw
    lineFunc(renderer, gc, tpath, affine.frozen())
  File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 740, in _draw_dashed
    renderer.draw_path(gc, path, trans)
ValueError: Dash sequence must be an even length sequence; found 1
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 331, in expose_event
    self._render_figure(self._pixmap, w, h)
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_agg.py", line 257, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
    for a in self.axes: a.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
    a.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/legend.py", line 206, in draw
    h.draw(renderer)
  File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 480, in draw
    lineFunc(renderer, gc, tpath, affine.frozen())
  File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line 740, in _draw_dashed
    renderer.draw_path(gc, path, trans)
ValueError: Dash sequence must be an even length sequence; found 1

Michael Droettboom wrote:

[I'm bringing this back to the list. Hope you don't mind.]

I've been doing a lot of non-matplotlib work lately and this fell off my radar. Thanks for the reminder.

The patch looks good to me. I'm not concerned about doing all this in _get_handles as opposed to Legend.__init__. I actually think it's better in _get_handles because then it's more connected to the creation of the handles themselves. (And of course _get_handles is only ever called from __init__ anyway, so it really is just an organizational extension of __init__). I moved all references to _xdata to _get_handles to be consistent.

The one concern I had is that _xdata is being stored as a member variable, which implicitly assumes that it will be the same for all handles. After your patch, it can be different for each handle -- for instance a line made of markers vs. dashing. So I've just made it a local variable to _get_handles and recreate it separately for each handle. There's no reason to try to share something so small, and it just feels safer (fewer potential side effects) that way.

I've committed this in r4871. Please check it out and kick the tires with all the test cases you came up with along the way and let me know how it goes.

Cheers,
Mike

Paul Novak wrote:

Mike,

Thank you for handling the PS backend issues. Below is an email I sent earlier about an updated patch for fixing legends with numpoints=1; I didn't get any response probably because of my delay in responding. If I should restart this thread on the mailing list, I can.

Included below is an updated patch fixing the legend with numpoints=1. The patch has been made against SVN and works for Line2D, LineCollection, Patch, and RegularPolyCollection. The patch could be merged.

I also think the patch could be improved. Currently, handle._marker is examined to determine if the legend should contain lines or symbols, but this is done in the _get_handles function. It would be better if that could be moved into the Legend class definition, however, I do not know how to examine handle._marker in the Legend class definition.

Again, I would appreciate any comments or improvements.

Thanks,

Paul

diff -u a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py
--- a/lib/matplotlib/legend.py 2008-01-09 13:11:00.000000000 -0600
+++ b/lib/matplotlib/legend.py 2008-01-09 13:08:36.000000000 -0600
@@ -175,9 +175,7 @@
         # make a trial box in the middle of the axes. relocate it
         # based on it's bbox
         left, top = 0.5, 0.5
- if self.numpoints == 1:
- self._xdata = npy.array([left + self.handlelen*0.5])
- else:
+ if self.numpoints > 1:
             self._xdata = npy.linspace(left, left + self.handlelen, self.numpoints)
         textleft = left+ self.handlelen+self.handletextsep
         self.texts = self._get_texts(labels, textleft, top)
@@ -236,6 +234,7 @@

     def _get_handles(self, handles, texts):
         HEIGHT = self._approx_text_height()
+ left = 0.5

         ret = [] # the returned legend lines

@@ -243,6 +242,10 @@
             x, y = label.get_position()
             x -= self.handlelen + self.handletextsep
             if isinstance(handle, Line2D):
+ if self.numpoints == 1 and handle._marker == 'None':
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
+ elif self.numpoints == 1:
+ self._xdata = npy.array([left + self.handlelen*0.5])
                 ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
                 legline = Line2D(self._xdata, ydata)
                 legline.update_from(handle)
@@ -253,7 +256,8 @@

                 ret.append(legline)
             elif isinstance(handle, Patch):
-
+ if self.numpoints == 1:
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
                 p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )
@@ -263,6 +267,8 @@
                 p.set_clip_path(None)
                 ret.append(p)
             elif isinstance(handle, LineCollection):
+ if self.numpoints == 1:
+ self._xdata = npy.linspace(left, left + self.handlelen, 2)
                 ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
                 legline = Line2D(self._xdata, ydata)
                 self._set_artist_props(legline)
@@ -277,6 +283,8 @@
                 ret.append(legline)

             elif isinstance(handle, RegularPolyCollection):
+ if self.numpoints == 1:
+ self._xdata = npy.array([left])
                 p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )

Michael Droettboom wrote:

Thanks for pointing that out -- I didn't even notice.

There was an ordering problem in how colors were being set. This should be fixed in r4870.

Cheers,
Mike

Paul Novak wrote:

I have another problem after updating to SVN r4869. The ticks take the color of the plotted line without regard to the default. For example,

matplotlib.rc('xtick', color='black')
matplotlib.rc('ytick', color='black')
plot(x, y, 'red')

will give red ticks.

Paul

Michael Droettboom wrote:

It's a bug. It's now possible for rgba colors to make their way all the way down to that level, so the code needs to truncate that to rgb (Ps can't handle alpha anyway).

This is committed in SVN r4869.

Cheers,
Mike

Paul Novak wrote:

Hello,

I am trying to use the PS backend to make some simple line plots, using the following script, but I get the error messages included below when I try to plot with usetex=True. I have also included the output from --verbose=helpful. I am using the current SVN, and I don't recall having this problem when using 0.91.1.

Thanks,
Paul

---
#!/usr/bin/env python

import matplotlib
matplotlib.use('PS')
matplotlib.rc('text', usetex = 'True')
from pylab import *

x = arange(0,5)
y = 2 * x
figure(1)
plot(x, y)
savefig('psfig')
show()

---
$HOME=/home/pnovak
CONFIGDIR=/home/pnovak/.matplotlib
matplotlib data path /usr/lib/python2.5/site-packages/matplotlib/mpl-data
loaded rc file /home/pnovak/.matplotlib/matplotlibrc
matplotlib version 0.98pre
verbose.level helpful
interactive is False
units is False
platform is linux2
numerix numpy 1.0.3.1
Using fontManager instance from /home/pnovak/.matplotlib/fontManager.cache
backend PS version Level II
Found dvipng version 1.5

Traceback (most recent call last):
   File "junk.py", line 15, in <module>
     savefig('psfig')
   File "/usr/lib/python2.5/site-packages/matplotlib/pyplot.py", line 271, in savefig
     return fig.savefig(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 854, in savefig
     self.canvas.print_figure(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 1085, in print_figure
     **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 837, in print_ps
     return self._print_ps(outfile, 'ps', *args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 863, in _print_ps
     orientation, isLandscape, papertype)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 1051, in _print_figure_tex
     self.figure.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/figure.py", line 698, in draw
     for a in self.axes: a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 1397, in draw
     a.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 680, in draw
     tick.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 179, in draw
     self.label1.draw(renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/text.py", line 761, in draw
     Text.draw(self, renderer)
   File "/usr/lib/python2.5/site-packages/matplotlib/text.py", line 309, in draw
     self._fontproperties, angle)
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_ps.py", line 544, in draw_tex
     color = '%1.3f,%1.3f,%1.3f'% gc.get_rgb()
TypeError: not all arguments converted during string formatting

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

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA