From ee1a599c38a91a4736a58d36a85d3cbb643a2c54 Mon Sep 17 00:00:00 2001 From: "Jessica B. Hamrick" Date: Sun, 12 Jul 2015 22:33:41 -0500 Subject: [PATCH 1/3] Have set_figheight and set_figwidth call set_size_inches --- lib/matplotlib/figure.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index db49161e4e4f..d891cc3720b0 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -766,23 +766,21 @@ def set_dpi(self, val): self.dpi = val self.stale = True - def set_figwidth(self, val): + def set_figwidth(self, val, **kwargs): """ Set the width of the figure in inches ACCEPTS: float """ - self.bbox_inches.x1 = val - self.stale = True + self.set_size_inches(val, self.get_figheight(), **kwargs) - def set_figheight(self, val): + def set_figheight(self, val, **kwargs): """ Set the height of the figure in inches ACCEPTS: float """ - self.bbox_inches.y1 = val - self.stale = True + self.set_size_inches(self.get_figwidth(), val, **kwargs) def set_frameon(self, b): """ From ddcf618401883969bdaec167f0612eec42928807 Mon Sep 17 00:00:00 2001 From: "Jessica B. Hamrick" Date: Sun, 12 Jul 2015 23:26:29 -0500 Subject: [PATCH 2/3] Add a test for setting the figure width/height --- lib/matplotlib/tests/test_figure.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 2f5fb4d68e2b..587da31b08c0 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -158,6 +158,24 @@ def _as_mpl_axes(self): plt.close(fig) +@cleanup +def test_set_fig_size(): + fig = plt.figure() + + # check figwidth + fig.set_figwidth(5) + assert_equal(fig.get_figwidth(), 5) + + # check figheight + fig.set_figheight(1) + assert_equal(fig.get_figheight(), 1) + + # check using set_size_inches + fig.set_size_inches(2, 4) + assert_equal(fig.get_figwidth(), 2) + assert_equal(fig.get_figheight(), 4) + + if __name__ == "__main__": import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From 85241386c2722b80d2be614140fa04cca9b78b58 Mon Sep 17 00:00:00 2001 From: "Jessica B. Hamrick" Date: Mon, 13 Jul 2015 11:14:44 -0700 Subject: [PATCH 3/3] Remove *args and **kwargs to set_size_inches --- lib/matplotlib/figure.py | 19 +++++++++---------- lib/matplotlib/tests/test_figure.py | 5 +++++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d891cc3720b0..b1acf098f037 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -659,7 +659,7 @@ def figimage(self, X, self.stale = True return im - def set_size_inches(self, *args, **kwargs): + def set_size_inches(self, w, h=None, forward=False): """ set_size_inches(w,h, forward=False) @@ -682,11 +682,10 @@ def set_size_inches(self, *args, **kwargs): matplotlib.Figure.get_size_inches """ - forward = kwargs.get('forward', False) - if len(args) == 1: - w, h = args[0] - else: - w, h = args + # the width and height have been passed in as a tuple to the first + # argument, so unpack them + if h is None: + w, h = w dpival = self.dpi self.bbox_inches.p1 = w, h @@ -766,21 +765,21 @@ def set_dpi(self, val): self.dpi = val self.stale = True - def set_figwidth(self, val, **kwargs): + def set_figwidth(self, val, forward=False): """ Set the width of the figure in inches ACCEPTS: float """ - self.set_size_inches(val, self.get_figheight(), **kwargs) + self.set_size_inches(val, self.get_figheight(), forward=forward) - def set_figheight(self, val, **kwargs): + def set_figheight(self, val, forward=False): """ Set the height of the figure in inches ACCEPTS: float """ - self.set_size_inches(self.get_figwidth(), val, **kwargs) + self.set_size_inches(self.get_figwidth(), val, forward=forward) def set_frameon(self, b): """ diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 587da31b08c0..8ef6f91d59e6 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -175,6 +175,11 @@ def test_set_fig_size(): assert_equal(fig.get_figwidth(), 2) assert_equal(fig.get_figheight(), 4) + # check using tuple to first argument + fig.set_size_inches((1, 3)) + assert_equal(fig.get_figwidth(), 1) + assert_equal(fig.get_figheight(), 3) + if __name__ == "__main__": import nose