On Monday 18 August 2008 10:45:39 am Darren Dale wrote: > On Monday 18 August 2008 09:48:58 am you wrote: > > Hi, > > > > On Mon, Aug 18, 2008 at 2:25 PM, Darren Dale <[EMAIL PROTECTED]> wrote: > > > Could you post a *simple* script that reproduces the problem? And in > > > the meantime, to get a figure that looks better for your publication, > > > can you save your figure as an svg, import it into inkscape, tweak the > > > bad placement, and then save a pdf? > > > > Thanks for the inkscape suggestion - I usually just use eps and pdf > > which I find harder to edit. > > > > Below is the function I use to create the plot. Actually I thought it > > happened with all legends, but I think I have just been focussing on > > these plots for too long, because when I check now it only seems to > > happen when I plot with this function. > > plot_stacked(arange(1,6),arange(1,6)) should reproduce the problem. > > Perhaps it is something to do with setting the labels to '' to delete > > the duplicate copies I don't want? > > No, because you can comment out that part of the code and still get a > problem with the "Higher Order" label. > > The problem is that the text placement for the "Higher Order" text label is > being calculated with reference to the descent of "g" rather than its > baseline (see http://en.wikipedia.org/wiki/Typeface#Font_metrics). I'm > looking into it, I think it can be improved.
It turns out that the information needed to do a better job of placing either the text or the Rectangle is calculated by the renderer, long after the text and Rectangle positions have been chosen based on an approximation of the text height (among other parameters). This is not my area of expertise, I'm just trying to fill in while the others are busy at the Scipy conference. There must be good reasons for organizing Legend the way it is, but maybe it tries to do too much too early and then never makes any refinements. Here is a patch that I submit only for the sake of discussion, it is an ugly workaround to the problem of not using the text's baseline as the common point of reference. You can perhaps temporarily modify your legend.py to make a better figure, although maybe this patch doesnt even do what you want it to. For now, I think it would be better to go with inkscape to make improvements, if you can. Index: lib/matplotlib/legend.py =================================================================== --- lib/matplotlib/legend.py (revision 6035) +++ lib/matplotlib/legend.py (working copy) @@ -203,16 +203,31 @@ if not len(self.legendHandles) and not len(self.texts): return - for h in self.legendHandles: - if h is not None: - h.draw(renderer) - if hasattr(h, '_legmarker'): - h._legmarker.draw(renderer) - if 0: bbox_artist(h, renderer) - for t in self.texts: - if 0: bbox_artist(t, renderer) - t.draw(renderer) + for l, t in safezip(self.legendHandles, self.texts): + if t is not None: + if 0: bbox_artist(t, renderer) + t.draw(renderer) + s = t.get_text() + f = t.get_font_properties() + ismath = t.is_math_text(s) + w, h, d = renderer.get_text_width_height_descent(s, f, ismath) + if l is not None: + if isinstance(l, Rectangle): + try: + old_h = l.get_height() + new_h = old_h * (h-d) / h + old_y = l.get_y() + new_y = old_y + (old_h - new_h) + l.set_height(new_h) + l.set_y(new_y) + except NameError: + pass + l.draw(renderer) + if hasattr(l, '_legmarker'): + l._legmarker.draw(renderer) + if 0: bbox_artist(l, renderer) + renderer.close_group('legend') #draw_bbox(self.save, renderer, 'g') #draw_bbox(self.ibox, renderer, 'r', self.get_transform()) ------------------------------------------------------------------------- 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 https://lists.sourceforge.net/lists/listinfo/matplotlib-users