From 0009f1bb8df20c0653c543adb69ba214704f9398 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Tue, 22 Mar 2011 17:09:51 -0700 Subject: [PATCH 1/6] always use figure(1) --- lib/matplotlib/tests/test_axes.py | 46 ++++++++++----------- lib/matplotlib/tests/test_backend_svg.py | 2 +- lib/matplotlib/tests/test_dates.py | 18 ++++---- lib/matplotlib/tests/test_image.py | 10 ++--- lib/matplotlib/tests/test_simplification.py | 20 ++++----- lib/matplotlib/tests/test_spines.py | 2 +- lib/matplotlib/tests/test_text.py | 4 +- 7 files changed, 51 insertions(+), 51 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cceb110b5eaf..47b67213a922 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -21,7 +21,7 @@ def test_formatter_ticker(): ydata1 = [ (1.5*y - 0.5)*units.km for y in range(10) ] ydata2 = [ (1.75*y - 1.0)*units.km for y in range(10) ] - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = plt.subplot( 111 ) ax.set_xlabel( "x-label 001" ) fig.savefig( 'formatter_ticker_001' ) @@ -50,7 +50,7 @@ def test_basic_annotate(): # Offset Points - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot( 111, autoscale_on=False, xlim=(-1,5), ylim=(-3,5) ) line, = ax.plot( t, s, lw=3, color='purple' ) @@ -75,7 +75,7 @@ def test_polar_annotations(): r = np.arange(0.0, 1.0, 0.001 ) theta = 2.0 * 2.0 * np.pi * r - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot( 111, polar=True ) line, = ax.plot( theta, r, color='#ee8d18', lw=3 ) @@ -103,7 +103,7 @@ def test_polar_coord_annotations(): from matplotlib.patches import Ellipse el = Ellipse((0,0), 10, 20, facecolor='r', alpha=0.5) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot( 111, aspect='equal' ) ax.add_artist( el ) @@ -135,7 +135,7 @@ def test_fill_units(): value = 10.0 * units.deg day = units.Duration( "ET", 24.0 * 60.0 * 60.0 ) - fig = plt.figure() + fig = plt.figure(1);plt.clf() # Top-Left ax1 = fig.add_subplot( 221 ) @@ -167,7 +167,7 @@ def test_fill_units(): @image_comparison(baseline_images=['single_point']) def test_single_point(): - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.subplot( 211 ) plt.plot( [0], [0], 'o' ) @@ -181,7 +181,7 @@ def test_single_date(): time1=[ 721964.0 ] data1=[ -65.54 ] - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.subplot( 211 ) plt.plot_date( time1, data1, 'o', color='r' ) @@ -219,7 +219,7 @@ def test_shaped_data(): y2 = np.arange( 10 ) y2.shape = 10, 1 - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.subplot( 411 ) plt.plot( y1 ) plt.subplot( 412 ) @@ -236,7 +236,7 @@ def test_shaped_data(): @image_comparison(baseline_images=['const_xy']) def test_const_xy(): - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.subplot( 311 ) plt.plot( np.arange(10), np.ones( (10,) ) ) @@ -255,7 +255,7 @@ def test_const_xy(): def test_polar_wrap(): D2R = np.pi / 180.0 - fig = plt.figure() + fig = plt.figure(1);plt.clf() #NOTE: resolution=1 really should be the default plt.subplot( 111, polar=True, resolution=1 ) @@ -265,7 +265,7 @@ def test_polar_wrap(): fig.savefig( 'polar_wrap_180' ) - fig = plt.figure() + fig = plt.figure(1);plt.clf() #NOTE: resolution=1 really should be the default plt.subplot( 111, polar=True, resolution=1 ) @@ -292,7 +292,7 @@ def test_polar_units(): y1 = [ 1.0, 2.0, 3.0, 4.0] y2 = [ 4.0, 3.0, 2.0, 1.0 ] - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.polar( x2, y1, color = "blue" ) @@ -312,7 +312,7 @@ def test_polar_rmin(): r = np.arange(0, 3.0, 0.01) theta = 2*np.pi*r - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) ax.plot(theta, r) ax.set_rmax(2.0) @@ -332,7 +332,7 @@ def test_axvspan_epoch(): dt = units.Duration( "ET", units.day.convert( "sec" ) ) - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.axvspan( t0, tf, facecolor="blue", alpha=0.25 ) @@ -353,7 +353,7 @@ def test_axhspan_epoch(): dt = units.Duration( "ET", units.day.convert( "sec" ) ) - fig = plt.figure() + fig = plt.figure(1);plt.clf() plt.axhspan( t0, tf, facecolor="blue", alpha=0.25 ) @@ -366,7 +366,7 @@ def test_axhspan_epoch(): @image_comparison(baseline_images=['hexbin_extent']) def test_hexbin_extent(): # this test exposes sf bug 2856228 - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) data = np.arange(2000.)/2000. @@ -381,7 +381,7 @@ def test_nonfinite_limits(): x = np.arange(0., np.e, 0.01) y = np.log(x) x[len(x)/2] = np.nan - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(x, y) fig.savefig('nonfinite_limits') @@ -396,7 +396,7 @@ def test_imshow(): r = np.sqrt(x**2+y**2-x*y) #Create a contour plot at N/4 and extract both the clip path and transform - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.imshow(r) @@ -414,7 +414,7 @@ def test_imshow_clip(): r = np.sqrt(x**2+y**2-x*y) #Create a contour plot at N/4 and extract both the clip path and transform - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) c = ax.contour(r,[N/4]) @@ -435,7 +435,7 @@ def test_polycollection_joinstyle(): from matplotlib import collections as mcoll - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) verts = np.array([[1,1], [1,2], [2,2], [2,1]]) c = mcoll.PolyCollection([verts], linewidths = 40) @@ -453,7 +453,7 @@ def test_fill_between_interpolate(): y1 = np.sin(2*np.pi*x) y2 = 1.2*np.sin(4*np.pi*x) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(211) ax.plot(x, y1, x, y2, color='black') ax.fill_between(x, y1, y2, where=y2>=y1, facecolor='green', interpolate=True) @@ -473,7 +473,7 @@ def test_symlog(): x = np.array([0,1,2,4,6,9,12,24]) y = np.array([1000000, 500000, 100000, 100, 5, 0, 0, 0]) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(x, y) ax.set_yscale('symlog') @@ -497,7 +497,7 @@ def test_pcolormesh(): # The color array can include masked values: Zm = ma.masked_where(np.fabs(Qz) < 0.5*np.amax(Qz), Z) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(121) ax.pcolormesh(Qx,Qz,Z, lw=0.5, edgecolors='k') ax.set_title('lw=0.5') diff --git a/lib/matplotlib/tests/test_backend_svg.py b/lib/matplotlib/tests/test_backend_svg.py index c99c480981da..a882c6eb9f83 100644 --- a/lib/matplotlib/tests/test_backend_svg.py +++ b/lib/matplotlib/tests/test_backend_svg.py @@ -7,7 +7,7 @@ def test_visibility(): # This is SF 2856495. See # https://sourceforge.net/tracker/?func=detail&aid=2856495&group_id=80706&atid=560720 - fig=plt.figure() + fig=plt.figure(1);plt.clf() ax=fig.add_subplot(1,1,1) x = np.linspace(0,4*np.pi,50) diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 04f491c597fa..fbde3320bcb5 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -9,7 +9,7 @@ def test_date_empty(): # make sure mpl does the right thing when told to plot dates even # if no date data has been presented, cf # http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720 - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(1,1,1) ax.xaxis_date() fig.savefig('date_empty') @@ -19,7 +19,7 @@ def test_date_axhspan(): # test ax hspan with date inputs t0 = datetime.datetime(2009, 1, 20) tf = datetime.datetime(2009, 1, 21) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(1,1,1) ax.axhspan( t0, tf, facecolor="blue", alpha=0.25 ) ax.set_ylim(t0-datetime.timedelta(days=5), @@ -32,7 +32,7 @@ def test_date_axvspan(): # test ax hspan with date inputs t0 = datetime.datetime(2000, 1, 20) tf = datetime.datetime(2010, 1, 21) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(1,1,1) ax.axvspan( t0, tf, facecolor="blue", alpha=0.25 ) ax.set_xlim(t0-datetime.timedelta(days=720), @@ -46,7 +46,7 @@ def test_date_axhline(): # test ax hline with date inputs t0 = datetime.datetime(2009, 1, 20) tf = datetime.datetime(2009, 1, 31) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(1,1,1) ax.axhline( t0, color="blue", lw=3) ax.set_ylim(t0-datetime.timedelta(days=5), @@ -59,7 +59,7 @@ def test_date_axvline(): # test ax hline with date inputs t0 = datetime.datetime(2000, 1, 20) tf = datetime.datetime(2000, 1, 21) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(1,1,1) ax.axvline( t0, color="red", lw=3) ax.set_xlim(t0-datetime.timedelta(days=5), @@ -75,7 +75,7 @@ def test_too_many_date_ticks(): # DayLocator. This should trigger a Locator.MAXTICKS RuntimeError t0 = datetime.datetime(2000, 1, 20) tf = datetime.datetime(2000, 1, 20) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(1,1,1) ax.set_xlim((t0,tf), auto=True) ax.plot([],[]) @@ -98,7 +98,7 @@ def test_RRuleLocator(): t0 = datetime( 1000, 1, 1 ) tf = datetime( 6000, 1, 1 ) - fig = pylab.figure() + fig = pylab.figure(1);plt.clf() ax = pylab.subplot( 111 ) ax.set_autoscale_on( True ) ax.plot( [t0, tf], [0.0, 1.0], marker='o' ) @@ -126,7 +126,7 @@ def test_DateFormatter(): t0 = datetime( 2001, 1, 1, 0, 0, 0 ) tf = datetime( 2001, 1, 1, 0, 0, 1 ) - fig = pylab.figure() + fig = pylab.figure(1);plt.clf() ax = pylab.subplot( 111 ) ax.set_autoscale_on( True ) ax.plot( [t0, tf], [0.0, 1.0], marker='o' ) @@ -151,7 +151,7 @@ def test_empty_date_with_year_formatter(): import matplotlib.dates as dates - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) yearFmt = dates.DateFormatter('%Y') diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 23faf6c6b28f..743aeedbb9f8 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -14,7 +14,7 @@ def test_image_interps(): X = np.arange(100) X = X.reshape(5, 20) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax1 = fig.add_subplot(311) ax1.imshow(X, interpolation='nearest') ax1.set_title('three interpolations') @@ -50,7 +50,7 @@ def test_figimage(): fig.savefig('figimage-%d' % int(suppressComposite), dpi=100) def test_image_python_io(): - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot([1,2,3]) buffer = cStringIO.StringIO() @@ -59,7 +59,7 @@ def test_image_python_io(): plt.imread(buffer) # def test_image_unicode_io(): -# fig = plt.figure() +# fig = plt.figure(1);plt.clf() # ax = fig.add_subplot(111) # ax.plot([1,2,3]) # fname = u"\u0a3a\u0a3a.png" @@ -100,7 +100,7 @@ def test_imsave(): def test_image_clip(): from math import pi - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111, projection='hammer') d = [[1,2],[3,4]] @@ -114,7 +114,7 @@ def test_imshow(): import numpy as np import matplotlib.pyplot as plt - fig = plt.figure() + fig = plt.figure(1);plt.clf() arr = np.arange(100).reshape((10, 10)) ax = fig.add_subplot(111) ax.imshow(arr, interpolation="bilinear", extent=(1,2,1,2)) diff --git a/lib/matplotlib/tests/test_simplification.py b/lib/matplotlib/tests/test_simplification.py index b38ad48673db..3cb7c9411a46 100644 --- a/lib/matplotlib/tests/test_simplification.py +++ b/lib/matplotlib/tests/test_simplification.py @@ -21,7 +21,7 @@ def test_clipping(): t = np.arange(0.0, 2.0, 0.01) s = np.sin(2*pi*t) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(t, s, linewidth=1.0) ax.set_ylim((-0.20, -0.28)) @@ -34,7 +34,7 @@ def test_overflow(): x = np.array([1.0,2.0,3.0,2.0e5]) y = np.arange(len(x)) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(x,y) ax.set_xlim(xmin=2,xmax=6) @@ -48,7 +48,7 @@ def test_diamond(): x = np.array([0.0, 1.0, 0.0, -1.0, 0.0]) y = np.array([1.0, 0.0, -1.0, 0.0, 1.0]) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(x, y) ax.set_xlim(xmin=-0.6, xmax=0.6) @@ -62,7 +62,7 @@ def test_noise(): np.random.seed(0) x = np.random.uniform(size=(5000,)) * 50 - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0) ax.set_xticks([]) @@ -81,7 +81,7 @@ def test_sine_plus_noise(): np.random.seed(0) x = np.sin(np.linspace(0, np.pi * 2.0, 1000)) + np.random.uniform(size=(1000,)) * 0.01 - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0) ax.set_xticks([]) @@ -103,7 +103,7 @@ def test_simplify_curve(): [Path.MOVETO, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CLOSEPOLY]), fc="none") - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.add_patch(pp1) ax.set_xticks([]) @@ -115,7 +115,7 @@ def test_simplify_curve(): @image_comparison(baseline_images=['hatch_simplify']) def test_hatch(): - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.add_patch(Rectangle((0, 0), 1, 1, fill=False, hatch="/")) ax.set_xlim((0.45, 0.55)) @@ -125,7 +125,7 @@ def test_hatch(): @image_comparison(baseline_images=['fft_peaks']) def test_fft_peaks(): - fig = plt.figure() + fig = plt.figure(1);plt.clf() t = arange(65536) ax = fig.add_subplot(111) p1 = ax.plot(abs(fft(sin(2*pi*.01*t)*blackman(len(t))))) @@ -182,7 +182,7 @@ def test_throw_rendering_complexity_exceeded(): xx = np.arange(200000) yy = np.random.rand(200000) yy[1000] = np.nan - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(xx, yy) try: @@ -215,7 +215,7 @@ def test_para_equal_perp(): x = np.array([0, 1, 2, 1, 0, -1, 0, 1] + [1] * 128) y = np.array([1, 1, 2, 1, 0, -1, 0, 0] + [0] * 128) - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(x + 1, y + 1) ax.plot(x + 1, y + 1, 'ro') diff --git a/lib/matplotlib/tests/test_spines.py b/lib/matplotlib/tests/test_spines.py index adebc44a4b84..e45388b32601 100644 --- a/lib/matplotlib/tests/test_spines.py +++ b/lib/matplotlib/tests/test_spines.py @@ -6,7 +6,7 @@ @image_comparison(baseline_images=['spines_axes_positions']) def test_spines_axes_positions(): # SF bug 2852168 - fig = plt.figure() + fig = plt.figure(1);plt.clf() x = np.linspace(0,2*np.pi,100) y = 2*np.sin(x) ax = fig.add_subplot(1,1,1) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 9ef688e1015e..4c3b9b314384 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -8,7 +8,7 @@ def test_font_styles(): from matplotlib.font_manager import FontProperties - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = plt.subplot( 1, 1, 1 ) normalFont = FontProperties( family = "sans-serif", @@ -66,7 +66,7 @@ def test_font_styles(): @image_comparison(baseline_images=['multiline']) def test_multiline(): - fig = plt.figure() + fig = plt.figure(1);plt.clf() ax = plt.subplot( 1, 1, 1 ) ax.set_title("multiline\ntext alignment") From 3494bbac25bfe2d3baa8d3ef25db87c9b98b6acb Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Tue, 22 Mar 2011 19:44:45 -0700 Subject: [PATCH 2/6] added 'scrub' keyword to fig.clf() --- lib/matplotlib/figure.py | 13 ++++++++++++- lib/matplotlib/rcsetup.py | 1 + lib/matplotlib/tests/test_simplification.py | 13 ++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index b96423ec95e3..ff147d18e7aa 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -196,6 +196,11 @@ def _update_this(self, s, val): val = rcParams[key] setattr(self, s, val) + def __repr__(self): + tmpdict = self.__dict__.copy() + if 'validate' in tmpdict: + tmpdict.pop('validate') + return repr(tmpdict) class Figure(Artist): @@ -769,7 +774,7 @@ def add_subplot(self, *args, **kwargs): self.sca(a) return a - def clf(self, keep_observers=False): + def clf(self, keep_observers=False, scrub=None): """ Clear the figure. @@ -796,6 +801,12 @@ def clf(self, keep_observers=False): if not keep_observers: self._axobservers = [] + if scrub is None and rcParams['figure.scrub'] or scrub == True: + sp = self.subplotpars + sp.left = sp.right = sp.top = sp.bottom = None + sp.update() + + def clear(self): """ Clear the figure -- synonym for fig.clf diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index f3198d862c7f..c78581be305d 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -502,6 +502,7 @@ def __call__(self, s): 'figure.facecolor' : [ '0.75', validate_color], # facecolor; scalar gray 'figure.edgecolor' : [ 'w', validate_color], # edgecolor; white 'figure.autolayout' : [ False, validate_autolayout], + 'figure.scrub' : [ True , validate_bool], # scrub subplotparams on clf 'figure.subplot.left' : [0.125, ValidateInterval(0, 1, closedmin=True, closedmax=True)], 'figure.subplot.right' : [0.9, ValidateInterval(0, 1, closedmin=True, closedmax=True)], diff --git a/lib/matplotlib/tests/test_simplification.py b/lib/matplotlib/tests/test_simplification.py index 3cb7c9411a46..3f7cd207e42b 100644 --- a/lib/matplotlib/tests/test_simplification.py +++ b/lib/matplotlib/tests/test_simplification.py @@ -185,17 +185,21 @@ def test_throw_rendering_complexity_exceeded(): fig = plt.figure(1);plt.clf() ax = fig.add_subplot(111) ax.plot(xx, yy) + rcParams['path.simplify'] = True try: fig.savefig(cStringIO.StringIO()) except e: raise e - else: - rcParams['path.simplify'] = True + + # here we close the figure so that subsequent tests create a new one using + # the rcParam defaults, instead of the figsize specified above + plt.close() @image_comparison(baseline_images=['clipper_edge']) def test_clipper(): dat = (0, 1, 0, 2, 0, 3, 0, 4, 0, 5) - fig = plt.figure(figsize=(2, 1)) + #plt.close() + fig = plt.figure(1,figsize=(2, 1)); fig.subplots_adjust(left = 0, bottom = 0, wspace = 0, hspace = 0) ax = fig.add_axes((0, 0, 1.0, 1.0), ylim = (0, 5), autoscale_on = False) @@ -209,6 +213,9 @@ def test_clipper(): ax.set_xlim(5, 9) fig.savefig('clipper_edge') + # here we close the figure so that subsequent tests create a new one using + # the rcParam defaults, instead of the figsize specified above + plt.close() @image_comparison(baseline_images=['para_equal_perp']) def test_para_equal_perp(): From 1532b7e606fc941d4d4a1ed46820b2bb8b974c9c Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Wed, 23 Mar 2011 03:14:08 -0700 Subject: [PATCH 3/6] more updates to the scrub, fixed some tests --- lib/matplotlib/figure.py | 17 +++++++++++++---- lib/matplotlib/pyplot.py | 8 +++----- lib/matplotlib/tests/test_image.py | 4 +++- lib/matplotlib/tests/test_mathtext.py | 9 ++++++++- lib/matplotlib/tests/test_simplification.py | 15 ++++++++++----- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index ff147d18e7aa..88095bbe7ab0 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -780,6 +780,12 @@ def clf(self, keep_observers=False, scrub=None): Set *keep_observers* to True if, for example, a gui widget is tracking the axes in the figure. + + Set *scrub* to True if you want the figure to reset the + subplotparameters to the defaults. + + If *scrub* is None, the behavior it will be set to + rcParams['figure.scrub']. """ self.suppressComposite = None self.callbacks = cbook.CallbackRegistry(('dpi_changed', )) @@ -801,17 +807,20 @@ def clf(self, keep_observers=False, scrub=None): if not keep_observers: self._axobservers = [] - if scrub is None and rcParams['figure.scrub'] or scrub == True: + if scrub is None: + scrub = rcParams['figure.scrub'] + if scrub == True: sp = self.subplotpars - sp.left = sp.right = sp.top = sp.bottom = None + sp.left = sp.right = sp.top = sp.bottom = sp.validate = None sp.update() + self._cachedRenderer = None - def clear(self): + def clear(self, keep_observers=False, scrub=None): """ Clear the figure -- synonym for fig.clf """ - self.clf() + self.clf(keep_observers,scrub) @allow_rasterization def draw(self, renderer): diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 6dd4f5293a97..7c93afa56410 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -346,11 +346,9 @@ def close(*args): raise TypeError('close takes 0 or 1 arguments') -def clf(): - """ - Clear the current figure - """ - gcf().clf() +@docstring.copy_dedent(Figure.clf) +def clf(*args,**kwargs): + gcf().clf(*args,**kwargs) draw_if_interactive() def draw(): diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 743aeedbb9f8..00756b48e1d1 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -33,9 +33,10 @@ def test_image_interps(): @image_comparison(baseline_images=['figimage-0', 'figimage-1'], extensions=['png'], tol=1.5e-3) def test_figimage(): 'test the figimage method' + plt.close('all') for suppressComposite in False, True: - fig = plt.figure(figsize=(2,2), dpi=100) + fig = plt.figure(1,figsize=(2,2), dpi=100);fig.clf() fig.suppressComposite = suppressComposite x,y = np.ix_(np.arange(100.0)/100.0, np.arange(100.0)/100.0) z = np.sin(x**2 + y**2 - x*y) @@ -48,6 +49,7 @@ def test_figimage(): fig.figimage(img[::-1,::-1], xo=100, yo=100, origin='lower') fig.savefig('figimage-%d' % int(suppressComposite), dpi=100) + plt.close() def test_image_python_io(): fig = plt.figure(1);plt.clf() diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 2c95581cf8d1..e7dff300ab4d 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -134,7 +134,8 @@ font_tests.append(wrapper % set) def _run_all_tests(tests): - fig = plt.figure(figsize=(5, len(tests) / 2.0)) + plt.close('all') + fig = plt.figure(1,figsize=(5, len(tests) / 2.0)) for i, test in enumerate(tests): fig.text(0, float(len(tests) - i - 1) / len(tests), test) return fig @@ -143,6 +144,7 @@ def _run_all_tests(tests): def test_mathtext(): fig = _run_all_tests(math_tests) fig.savefig('mathtext') + plt.close() @image_comparison(baseline_images=['mathtext_stix']) def test_mathtext_stix(): @@ -150,6 +152,7 @@ def test_mathtext_stix(): fig = _run_all_tests(math_tests) fig.savefig('mathtext_stix') + plt.close() matplotlib.rcParams['mathtext.fontset'] = 'cm' @@ -159,6 +162,7 @@ def test_mathtext_stixsans(): fig = _run_all_tests(math_tests) fig.savefig('mathtext_stixsans') + plt.close() matplotlib.rcParams['mathtext.fontset'] = 'cm' @@ -166,6 +170,7 @@ def test_mathtext_stixsans(): def test_mathtext_font(): fig = _run_all_tests(font_tests) fig.savefig('mathtext_font') + plt.close() @image_comparison(baseline_images=['mathtext_font_stix']) def test_mathtext_font_stix(): @@ -173,6 +178,7 @@ def test_mathtext_font_stix(): fig = _run_all_tests(font_tests) fig.savefig('mathtext_font_stix') + plt.close() matplotlib.rcParams['mathtext.fontset'] = 'cm' @@ -182,5 +188,6 @@ def test_mathtext_font_stixsans(): fig = _run_all_tests(font_tests) fig.savefig('mathtext_font_stixsans') + plt.close() matplotlib.rcParams['mathtext.fontset'] = 'cm' diff --git a/lib/matplotlib/tests/test_simplification.py b/lib/matplotlib/tests/test_simplification.py index 3f7cd207e42b..304481b330b5 100644 --- a/lib/matplotlib/tests/test_simplification.py +++ b/lib/matplotlib/tests/test_simplification.py @@ -7,7 +7,7 @@ import numpy as np from matplotlib import patches, path, transforms -from nose.tools import raises +from nose.tools import raises, with_setup import cStringIO nan = np.nan @@ -190,15 +190,20 @@ def test_throw_rendering_complexity_exceeded(): fig.savefig(cStringIO.StringIO()) except e: raise e + finally: + plt.close() + del(xx);del(yy) + import gc + gc.collect() - # here we close the figure so that subsequent tests create a new one using - # the rcParam defaults, instead of the figsize specified above - plt.close() @image_comparison(baseline_images=['clipper_edge']) def test_clipper(): dat = (0, 1, 0, 2, 0, 3, 0, 4, 0, 5) - #plt.close() + # here we close the figure because the figsize parameter is ignored for + # figures which are already created + # the rcParam defaults, instead of the figsize specified above + plt.close() fig = plt.figure(1,figsize=(2, 1)); fig.subplots_adjust(left = 0, bottom = 0, wspace = 0, hspace = 0) From aecc1e235f2aa7ef4328deee3306f8d952379972 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Thu, 24 Mar 2011 00:39:54 -0700 Subject: [PATCH 4/6] documented new scrub kwarg for clf --- CHANGELOG | 7 +++++++ doc/api/api_changes.rst | 11 +++++++++++ lib/matplotlib/figure.py | 5 ++--- lib/matplotlib/rcsetup.py | 2 +- matplotlibrc.template | 1 + 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 980b6dac3a32..43821202873a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +2011-03-23 New scrub kwarg for clf, which allows for the resetting + of the subplot parameters of a figure, and is backed back + a new rcParam which can restore the old default behavior. + Also fixed a huge memory leak in the test suite where + none of the generated figures were being closed or + cleared. - PI + 2011-03-10 Update pytz version to 2011c, thanks to Simon Cross. - JKS 2011-03-06 Add standalone tests.py test runner script. - JKS diff --git a/doc/api/api_changes.rst b/doc/api/api_changes.rst index e7a95b23bdd4..16aa33268b11 100644 --- a/doc/api/api_changes.rst +++ b/doc/api/api_changes.rst @@ -7,6 +7,17 @@ This chapter is a log of changes to matplotlib that affect the outward-facing API. If updating matplotlib breaks your scripts, this list may help describe what changes may be necessary in your code. +Changes beyond 1.0.1 +==================== + +* The :meth:`matplotlib.figure.Figure.clf` and + :meth:`matplotlib.pyplot.clf` methods now accept a *scrub* kwarg. + Passing ``scrub=True`` will reset the figure's subplot parameters, + ``scrub=False`` will clear the figure without touching the subplot + parameters(old behavior) and the default value of ``scrub=None`` will + set ``scrub`` to the value of ``rcParams['figure.autoscrub']`` + (default). + Changes beyond 0.99.x ===================== diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 88095bbe7ab0..57e7965e731a 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -785,7 +785,7 @@ def clf(self, keep_observers=False, scrub=None): subplotparameters to the defaults. If *scrub* is None, the behavior it will be set to - rcParams['figure.scrub']. + rcParams['figure.autoscrub']. """ self.suppressComposite = None self.callbacks = cbook.CallbackRegistry(('dpi_changed', )) @@ -808,12 +808,11 @@ def clf(self, keep_observers=False, scrub=None): self._axobservers = [] if scrub is None: - scrub = rcParams['figure.scrub'] + scrub = rcParams['figure.autoscrub'] if scrub == True: sp = self.subplotpars sp.left = sp.right = sp.top = sp.bottom = sp.validate = None sp.update() - self._cachedRenderer = None def clear(self, keep_observers=False, scrub=None): diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index c78581be305d..6023dbbbf22f 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -502,7 +502,7 @@ def __call__(self, s): 'figure.facecolor' : [ '0.75', validate_color], # facecolor; scalar gray 'figure.edgecolor' : [ 'w', validate_color], # edgecolor; white 'figure.autolayout' : [ False, validate_autolayout], - 'figure.scrub' : [ True , validate_bool], # scrub subplotparams on clf + 'figure.autoscrub' : [ True , validate_bool], # default behavior of scrub for clf() 'figure.subplot.left' : [0.125, ValidateInterval(0, 1, closedmin=True, closedmax=True)], 'figure.subplot.right' : [0.9, ValidateInterval(0, 1, closedmin=True, closedmax=True)], diff --git a/matplotlibrc.template b/matplotlibrc.template index 67933f94849b..b63a7da71484 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -256,6 +256,7 @@ backend : %(backend)s #figure.dpi : 80 # figure dots per inch #figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray #figure.edgecolor : white # figure edgecolor +#figure.autoscrub : True # default figure.clf() behaviour # The figure subplot parameters. All dimensions are fraction of the # figure width or height From 5805de20b03f7ac1c7c3720ee2893f8a122218b0 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Thu, 24 Mar 2011 12:55:14 -0700 Subject: [PATCH 5/6] added a test for scrub. --- lib/matplotlib/tests/test_axes.py | 23 ++++++++++++++++++++- lib/matplotlib/tests/test_simplification.py | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 47b67213a922..fa7e12c2c3a7 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -515,10 +515,31 @@ def test_pcolormesh(): @image_comparison(baseline_images=['canonical']) def test_canonical(): - fig, ax = plt.subplots() + plt.close('all') + fig, ax = plt.subplots(num=1) ax.plot([1,2,3]) fig.savefig('canonical') +def test_scrub(): + from matplotlib import rcParams + fig = plt.figure(1); fig.clf() + s = fig.subplotpars + fig.subplots_adjust(left=.3, bottom=.1, right=.4, top=.15) + # with scrub=False, subplotpars should not be reset to the rcParam defaults + fig.clf(scrub=False) + assert s.left == .3 + assert s.bottom == .1 + assert s.right == .4 + assert s.top == .15 + + fig.subplots_adjust(left=.3, bottom=.1, right=.4, top=.15) + # with scrub=True, subplotpars should be reset to the rcParam defaults + fig.clf(scrub=True) + assert s.left == rcParams['figure.subplot.left'] + assert s.bottom == rcParams['figure.subplot.bottom'] + assert s.right == rcParams['figure.subplot.right'] + assert s.top == rcParams['figure.subplot.top'] + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False) diff --git a/lib/matplotlib/tests/test_simplification.py b/lib/matplotlib/tests/test_simplification.py index 304481b330b5..92a49bb77d41 100644 --- a/lib/matplotlib/tests/test_simplification.py +++ b/lib/matplotlib/tests/test_simplification.py @@ -7,7 +7,7 @@ import numpy as np from matplotlib import patches, path, transforms -from nose.tools import raises, with_setup +from nose.tools import raises import cStringIO nan = np.nan From 500a96e9b5c4178e23f976193c82f254a14f6697 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Fri, 25 Mar 2011 11:27:28 -0700 Subject: [PATCH 6/6] also scrub hspace and wspace --- lib/matplotlib/figure.py | 1 + lib/matplotlib/tests/test_axes.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 57e7965e731a..66bfdd16a67b 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -812,6 +812,7 @@ def clf(self, keep_observers=False, scrub=None): if scrub == True: sp = self.subplotpars sp.left = sp.right = sp.top = sp.bottom = sp.validate = None + sp.wspace = sp.hspace = None sp.update() diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index fa7e12c2c3a7..3f8a796227e1 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -524,21 +524,27 @@ def test_scrub(): from matplotlib import rcParams fig = plt.figure(1); fig.clf() s = fig.subplotpars - fig.subplots_adjust(left=.3, bottom=.1, right=.4, top=.15) + fig.subplots_adjust(left=.3, bottom=.1, right=.4, + top=.15,hspace=.9,wspace=.26) # with scrub=False, subplotpars should not be reset to the rcParam defaults fig.clf(scrub=False) assert s.left == .3 assert s.bottom == .1 assert s.right == .4 assert s.top == .15 + assert s.hspace == .9 + assert s.wspace == .26 - fig.subplots_adjust(left=.3, bottom=.1, right=.4, top=.15) + fig.subplots_adjust(left=.3, bottom=.1, right=.4, + top=.15,hspace=.9,wspace=.26) # with scrub=True, subplotpars should be reset to the rcParam defaults fig.clf(scrub=True) assert s.left == rcParams['figure.subplot.left'] assert s.bottom == rcParams['figure.subplot.bottom'] assert s.right == rcParams['figure.subplot.right'] assert s.top == rcParams['figure.subplot.top'] + assert s.hspace == rcParams['figure.subplot.hspace'] + assert s.wspace == rcParams['figure.subplot.wspace'] if __name__=='__main__': import nose