From: <as...@us...> - 2009-12-15 02:57:56
|
Revision: 8034 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8034&view=rev Author: astraw Date: 2009-12-15 02:57:46 +0000 (Tue, 15 Dec 2009) Log Message: ----------- Add patch_artist kwarg to boxplot Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/examples/pylab_examples/boxplot_demo2.py trunk/matplotlib/lib/matplotlib/axes.py trunk/matplotlib/lib/matplotlib/pyplot.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2009-12-15 02:57:32 UTC (rev 8033) +++ trunk/matplotlib/CHANGELOG 2009-12-15 02:57:46 UTC (rev 8034) @@ -1,3 +1,6 @@ +2009-12-14 Add patch_artist kwarg to boxplot, but keep old default. + Convert boxplot_demo2.py to use the new patch_artist. - ADS + 2009-12-06 axes_grid: reimplemented AxisArtist with FloatingAxes support. Added new examples. - JJL Modified: trunk/matplotlib/examples/pylab_examples/boxplot_demo2.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/boxplot_demo2.py 2009-12-15 02:57:32 UTC (rev 8033) +++ trunk/matplotlib/examples/pylab_examples/boxplot_demo2.py 2009-12-15 02:57:46 UTC (rev 8034) @@ -42,8 +42,8 @@ ax1 = fig.add_subplot(111) plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25) -bp = plt.boxplot(data, notch=0, sym='+', vert=1, whis=1.5) -plt.setp(bp['boxes'], color='black') +bp = plt.boxplot(data, notch=0, sym='+', vert=1, whis=1.5, patch_artist=True) +plt.setp(bp['boxes'], edgecolor='black') plt.setp(bp['whiskers'], color='black') plt.setp(bp['fliers'], color='red', marker='+') @@ -64,25 +64,12 @@ medians = range(numBoxes) for i in range(numBoxes): box = bp['boxes'][i] - boxX = [] - boxY = [] - for j in range(5): - boxX.append(box.get_xdata()[j]) - boxY.append(box.get_ydata()[j]) - boxCoords = zip(boxX,boxY) - # Alternate between Dark Khaki and Royal Blue k = i % 2 - boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) - ax1.add_patch(boxPolygon) - # Now draw the median lines back over what we just filled in + # Set the box colors + box.set_facecolor(boxColors[k]) + # Now get the medians med = bp['medians'][i] - medianX = [] - medianY = [] - for j in range(2): - medianX.append(med.get_xdata()[j]) - medianY.append(med.get_ydata()[j]) - plt.plot(medianX, medianY, 'k') - medians[i] = medianY[0] + medians[i] = med.get_ydata()[0] # Finally, overplot the sample averages, with horixzontal alignment # in the center of each box plt.plot([np.average(med.get_xdata())], [np.average(data[i])], Modified: trunk/matplotlib/lib/matplotlib/axes.py =================================================================== --- trunk/matplotlib/lib/matplotlib/axes.py 2009-12-15 02:57:32 UTC (rev 8033) +++ trunk/matplotlib/lib/matplotlib/axes.py 2009-12-15 02:57:46 UTC (rev 8034) @@ -21,6 +21,7 @@ import matplotlib.legend as mlegend import matplotlib.lines as mlines import matplotlib.mlab as mlab +import matplotlib.path as mpath import matplotlib.patches as mpatches import matplotlib.spines as mspines import matplotlib.quiver as mquiver @@ -4892,12 +4893,12 @@ return (l0, caplines, barcols) def boxplot(self, x, notch=0, sym='b+', vert=1, whis=1.5, - positions=None, widths=None): + positions=None, widths=None, patch_artist=False): """ call signature:: boxplot(x, notch=0, sym='+', vert=1, whis=1.5, - positions=None, widths=None) + positions=None, widths=None, patch_artist=False) Make a box and whisker plot for each column of *x* or each vector in sequence *x*. The box extends from the lower to @@ -4905,6 +4906,8 @@ The whiskers extend from the box to show the range of the data. Flier points are those past the end of the whiskers. + *x* is an array or a sequence of vectors. + - *notch* = 0 (default) produces a rectangular box plot. - *notch* = 1 will produce a notched box plot @@ -4927,7 +4930,8 @@ each box. The default is 0.5, or ``0.15*(distance between extreme positions)`` if that is smaller. - *x* is an array or a sequence of vectors. + - *patch_artist* = False (default) produces boxes with the Line2D artist + - *patch_artist* = True produces boxes with the Patch artist Returns a dictionary mapping each component of the boxplot to a list of the :class:`matplotlib.lines.Line2D` @@ -5045,23 +5049,55 @@ med_x = [cap_x_min, cap_x_max] med_y = [med, med] + def to_vc(xs,ys): + # convert arguments to verts and codes + verts = [] + #codes = [] + for xi,yi in zip(xs,ys): + verts.append( (xi,yi) ) + verts.append( (0,0) ) # ignored + codes = [mpath.Path.MOVETO] + \ + [mpath.Path.LINETO]*(len(verts)-2) + \ + [mpath.Path.CLOSEPOLY] + return verts,codes + + def patch_list(xs,ys): + verts,codes = to_vc(xs,ys) + path = mpath.Path( verts, codes ) + patch = mpatches.PathPatch(path) + self.add_artist(patch) + return [patch] + # vertical or horizontal plot? if vert: def doplot(*args): return self.plot(*args) + def dopatch(xs,ys): + return patch_list(xs,ys) else: def doplot(*args): shuffled = [] for i in xrange(0, len(args), 3): shuffled.extend([args[i+1], args[i], args[i+2]]) return self.plot(*shuffled) + def dopatch(xs,ys): + xs,ys = ys,xs # flip X, Y + return patch_list(xs,ys) + if patch_artist: + median_color = 'k' + else: + median_color = 'r' + whiskers.extend(doplot(wisk_x, [q1, wisk_lo], 'b--', wisk_x, [q3, wisk_hi], 'b--')) caps.extend(doplot(cap_x, [wisk_hi, wisk_hi], 'k-', cap_x, [wisk_lo, wisk_lo], 'k-')) - boxes.extend(doplot(box_x, box_y, 'b-')) - medians.extend(doplot(med_x, med_y, 'r-')) + if patch_artist: + boxes.extend(dopatch(box_x, box_y)) + else: + boxes.extend(doplot(box_x, box_y, 'b-')) + medians.extend(doplot(med_x, med_y, median_color+'-')) fliers.extend(doplot(flier_hi_x, flier_hi, sym, flier_lo_x, flier_lo, sym)) Modified: trunk/matplotlib/lib/matplotlib/pyplot.py =================================================================== --- trunk/matplotlib/lib/matplotlib/pyplot.py 2009-12-15 02:57:32 UTC (rev 8033) +++ trunk/matplotlib/lib/matplotlib/pyplot.py 2009-12-15 02:57:46 UTC (rev 8034) @@ -1767,7 +1767,8 @@ # This function was autogenerated by boilerplate.py. Do not edit as # changes will be lost @autogen_docstring(Axes.boxplot) -def boxplot(x, notch=0, sym='b+', vert=1, whis=1.5, positions=None, widths=None, hold=None): +def boxplot(x, notch=0, sym='b+', vert=1, whis=1.5, positions=None, widths=None, + hold=None, patch_artist=False): ax = gca() # allow callers to override the hold state by passing hold=True|False washold = ax.ishold() @@ -1775,7 +1776,8 @@ if hold is not None: ax.hold(hold) try: - ret = ax.boxplot(x, notch, sym, vert, whis, positions, widths) + ret = ax.boxplot(x, notch, sym, vert, whis, positions, widths, + patch_artist=patch_artist) draw_if_interactive() finally: ax.hold(washold) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |