From: <lee...@us...> - 2008-12-05 00:06:31
|
Revision: 6494 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6494&view=rev Author: leejjoon Date: 2008-12-05 00:06:26 +0000 (Fri, 05 Dec 2008) Log Message: ----------- some minor changes for the legend Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/examples/pylab_examples/legend_demo3.py trunk/matplotlib/lib/matplotlib/legend.py trunk/matplotlib/lib/matplotlib/offsetbox.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2008-12-04 22:13:57 UTC (rev 6493) +++ trunk/matplotlib/CHANGELOG 2008-12-05 00:06:26 UTC (rev 6494) @@ -1,3 +1,7 @@ +2008-12-04 Added fancybox keyword to legend. Also applied some changes + for better look, including baseline adjustment of the + multiline texts so that it is center aligned. -JJL + 2008-12-02 The transmuter classes in the patches.py are reorganized as subclasses of the Style classes. A few more box and arrow styles are added. -JJL Modified: trunk/matplotlib/examples/pylab_examples/legend_demo3.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/legend_demo3.py 2008-12-04 22:13:57 UTC (rev 6493) +++ trunk/matplotlib/examples/pylab_examples/legend_demo3.py 2008-12-05 00:06:26 UTC (rev 6494) @@ -2,6 +2,7 @@ import matplotlib.pyplot as plt import numpy as np +import pylab def myplot(ax): t1 = np.arange(0.0, 1.0, 0.1) @@ -21,7 +22,7 @@ ax3 = plt.subplot(3,1,3) myplot(ax3) -ax3.legend(loc=1, ncol=4, mode="expand", shadow=True) +ax3.legend(loc=1, ncol=4, mode="expand", fancybox=False, shadow=True) #title('Damped oscillation') Modified: trunk/matplotlib/lib/matplotlib/legend.py =================================================================== --- trunk/matplotlib/lib/matplotlib/legend.py 2008-12-04 22:13:57 UTC (rev 6493) +++ trunk/matplotlib/lib/matplotlib/legend.py 2008-12-05 00:06:26 UTC (rev 6494) @@ -113,6 +113,7 @@ ncol=1, # number of columns mode=None, # mode for horizontal distribution of columns. None, "expand" + fancybox=True, shadow = None, ): """ @@ -130,6 +131,7 @@ numpoints the number of points in the legend line prop the font property markerscale the relative size of legend markers vs. original + fancybox if True, draw a frame with a round fancybox. shadow if True, draw a shadow behind legend scatteryoffsets a list of yoffsets for scatter symbols in legend @@ -263,8 +265,11 @@ # The width and height of the legendPatch will be set (in the # draw()) to the length that includes the padding. Thus we set # pad=0 here. - self.legendPatch.set_boxstyle("round",pad=0, #self.borderpad, - rounding_size=0.2) + if fancybox == True: + self.legendPatch.set_boxstyle("round",pad=0, + rounding_size=0.2) + else: + self.legendPatch.set_boxstyle("square",pad=0) self._set_artist_props(self.legendPatch) @@ -378,7 +383,8 @@ labelboxes = [] for l in labels: - textbox = TextArea(l, textprops=label_prop) + textbox = TextArea(l, textprops=label_prop + multilinebaseline=True, minimumdescent=True) text_list.append(textbox._text) labelboxes.append(textbox) @@ -387,8 +393,8 @@ # The approximate height and descent of text. These values are # only used for plotting the legend handle. - height = self._approx_text_height() * 0.6 - descent = 0. #height/6. + height = self._approx_text_height() * 0.7 + descent = 0. # each handle needs to be drawn inside a box of # (x, y, w, h) = (0, -descent, width, height). @@ -440,9 +446,9 @@ legline._legmarker = legline_marker elif isinstance(handle, Patch): - p = Rectangle(xy=(0, -0.*descent), + p = Rectangle(xy=(0., 0.), width = self.handlelength*self.fontsize, - height=0.*descent+(height-descent)*.9, + height=(height-descent), ) p.update_from(handle) self._set_artist_props(p) @@ -513,12 +519,12 @@ num_smallcol = self._ncol-num_largecol # starting index of each column and number of rows in it. - largecol = zip(range(0, num_largecol*(nrows+1), (nrows+1)), - [nrows+1] * num_largecol) - smallcol = zip(range(num_largecol*(nrows+1), len(handleboxes), nrows), - [nrows] * num_smallcol) + largecol = safezip(range(0, num_largecol*(nrows+1), (nrows+1)), + [nrows+1] * num_largecol) + smallcol = safezip(range(num_largecol*(nrows+1), len(handleboxes), nrows), + [nrows] * num_smallcol) - handle_label = zip(handleboxes, labelboxes) + handle_label = safezip(handleboxes, labelboxes) columnbox = [] for i0, di in largecol+smallcol: # pack handleBox and labelBox into itemBox @@ -526,6 +532,8 @@ sep=self.handletextpad*self.fontsize, children=[h, t], align="baseline") for h, t in handle_label[i0:i0+di]] + # minimumdescent=False for the text of the last row of the column + itemBoxes[-1].get_children()[1].set_minimumdescent(False) # pack columnBox columnbox.append(VPacker(pad=0, Modified: trunk/matplotlib/lib/matplotlib/offsetbox.py =================================================================== --- trunk/matplotlib/lib/matplotlib/offsetbox.py 2008-12-04 22:13:57 UTC (rev 6493) +++ trunk/matplotlib/lib/matplotlib/offsetbox.py 2008-12-05 00:06:26 UTC (rev 6494) @@ -23,9 +23,9 @@ from matplotlib.patches import bbox_artist as mbbox_artist DEBUG=False # for debuging use -def bbox_artist(*kl, **kw): +def bbox_artist(*args, **kwargs): if DEBUG: - mbbox_artist(*kl, **kw) + mbbox_artist(*args, **kwargs) # _get_packed_offsets() and _get_aligned_offsets() are coded assuming @@ -122,9 +122,9 @@ The OffsetBox is a simple container artist. The child artist are meant to be drawn at a relative position to its parent. """ - def __init__(self, *kl, **kw): + def __init__(self, *args, **kwargs): - super(OffsetBox, self).__init__(*kl, **kw) + super(OffsetBox, self).__init__(*args, **kwargs) self._children = [] self._offset = (0, 0) @@ -441,10 +441,18 @@ - def __init__(self, s, textprops=None, **kw): + def __init__(self, s, + textprops=None, + multilinebaseline=None, + minimumdescent=True, + ): """ - *s* : a string to be displayer. - *trnaspose* : transformation matrrix + *s* : a string to be displayed. + *textprops* : property dictionary for the text + *multilinebaseline* : If True, baseline for multiline text is + adjusted so that it is (approximatedly) + center-aligned with singleline text. + *minimumdescent* : If True, the box has a minimum descent of "p". """ if textprops is None: textprops = {} @@ -462,9 +470,48 @@ self.offset_transform = mtransforms.Affine2D() self.offset_transform.clear() self.offset_transform.translate(0, 0) - self._text.set_transform(self.offset_transform) + self._baseline_transform = mtransforms.Affine2D() + self._text.set_transform(self.offset_transform+self._baseline_transform) + self._multilinebaseline = multilinebaseline + self._minimumdescent = minimumdescent + + def set_multilinebaseline(self, t): + """ + Set multilinebaseline . + + If True, baseline for multiline text is + adjusted so that it is (approximatedly) center-aligned with + singleline text. + """ + self._multilinebaseline = t + + + def get_multilinebaseline(self): + """ + get multilinebaseline . + """ + return self._multilinebaseline + + + def set_minimumdescent(self, t): + """ + Set minimumdescent . + + If True, extent of the single line text is adjusted so that + it has minimum descent of "p" + """ + self._minimumdescent = t + + + def get_minimumdescent(self): + """ + get minimumdescent. + """ + return self._minimumdescent + + def set_transform(self, t): """ set_transform is ignored. @@ -507,17 +554,34 @@ bbox, info = self._text._get_layout(renderer) w, h = bbox.width, bbox.height + line = info[0][0] # first line _, hh, dd = renderer.get_text_width_height_descent( line, self._text._fontproperties, ismath=ismath) - d = h-(hh-dd) # the baseline of the first line - # for multiple lines, h or d may greater than h_ or d_. - h_d = max(h_ - d_, h-d) - d = max(d, d_) - h = h_d + d + self._baseline_transform.clear() + if len(info) > 1 and self._multilinebaseline: # multi line + d = h-(hh-dd) # the baseline of the first line + d_new = 0.5 * h - 0.5 * (h_ - d_) + + self._baseline_transform.translate(0, d - d_new) + d = d_new + + else: # single line + + h_d = max(h_ - d_, h-dd) + + if self.get_minimumdescent(): + ## to have a minimum descent, #i.e., "l" and "p" have same + ## descents. + d = max(dd, d_) + else: + d = dd + + h = h_d + d + return w, h, 0., d This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |