From 0e176e5f747fb8fbb2e11382a4c8b26fc8c7864d Mon Sep 17 00:00:00 2001 From: Robotia Date: Sat, 19 Sep 2015 09:28:40 -0400 Subject: [PATCH 1/5] NF - Added set_size_cm and get_size_cm for figures --- lib/matplotlib/figure.py | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index fccf69e6e628..c38c018fc4a1 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -707,7 +707,49 @@ def set_size_inches(self, w, h=None, forward=False): if manager is not None: manager.resize(int(canvasw), int(canvash)) self.stale = True + def set_size_cm(self, w, h=None, forward=False): + """ + set_size_cm(w,h, forward=False) + + Set the figure size in centimeters + + Usage:: + + fig.set_size_cm(w,h) # OR + fig.set_size_cm((w,h) ) + + optional kwarg *forward=True* will cause the canvas size to be + automatically updated; e.g., you can resize the figure window + from the shell + + ACCEPTS: a w,h tuple with w,h in centimeters + + See Also + -------- + + matplotlib.Figure.get_size_cm + """ + + # the width and height have been passed in as a tuple to the first + # argument, so unpack them + in_w = w*0.3937007874015748031496062992126 + in_h = None + if h is None: + in_w, in_h = in_w + else: + in_h = h*0.3937007874015748031496062992126 + + dpival = self.dpi + self.bbox_inches.p1 = in_w, in_h + if forward: + dpival = self.dpi + canvasw = in_w * dpival + canvash = in_h * dpival + manager = getattr(self.canvas, 'manager', None) + if manager is not None: + manager.resize(int(canvasw), int(canvash)) + self.stale = True def get_size_inches(self): """ Returns the current size of the figure in inches (1in == 2.54cm) @@ -724,7 +766,23 @@ def get_size_inches(self): matplotlib.Figure.set_size_inches """ return np.array(self.bbox_inches.p1) + def get_size_cm(self): + """ + Returns the current size of the figure in centimeters + as an numpy array. + + Returns + ------- + size : ndarray + The size of the figure in centimeters + See Also + -------- + + matplotlib.Figure.set_size_cm + """ + return np.array(self.bbox_inches.p1)*2.54 + def get_edgecolor(self): 'Get the edge color of the Figure rectangle' return self.patch.get_edgecolor() From 1972ce78e1db80c0641c2ebfcfdbc54136f58060 Mon Sep 17 00:00:00 2001 From: Robotia Date: Sat, 19 Sep 2015 10:39:19 -0400 Subject: [PATCH 2/5] Stylistic fix by adding space between added methods --- lib/matplotlib/figure.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index c38c018fc4a1..d786570933f7 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -707,6 +707,7 @@ def set_size_inches(self, w, h=None, forward=False): if manager is not None: manager.resize(int(canvasw), int(canvash)) self.stale = True + def set_size_cm(self, w, h=None, forward=False): """ set_size_cm(w,h, forward=False) @@ -750,6 +751,7 @@ def set_size_cm(self, w, h=None, forward=False): if manager is not None: manager.resize(int(canvasw), int(canvash)) self.stale = True + def get_size_inches(self): """ Returns the current size of the figure in inches (1in == 2.54cm) @@ -766,6 +768,7 @@ def get_size_inches(self): matplotlib.Figure.set_size_inches """ return np.array(self.bbox_inches.p1) + def get_size_cm(self): """ Returns the current size of the figure in centimeters @@ -782,7 +785,7 @@ def get_size_cm(self): matplotlib.Figure.set_size_cm """ return np.array(self.bbox_inches.p1)*2.54 - + def get_edgecolor(self): 'Get the edge color of the Figure rectangle' return self.patch.get_edgecolor() From 7ce7f47bfedebcbcf9ed8a443e8b503e0d2d325d Mon Sep 17 00:00:00 2001 From: Robotia Date: Sat, 19 Sep 2015 18:06:22 -0400 Subject: [PATCH 3/5] Removed copying of get/set functions and used an additional kwarg "metric" instead --- lib/matplotlib/figure.py | 91 +++++++++------------------------------- 1 file changed, 20 insertions(+), 71 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d786570933f7..c80cde12b104 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -668,12 +668,12 @@ def figimage(self, X, self.stale = True return im - def set_size_inches(self, w, h=None, forward=False): + def set_size_inches(self, w, h=None, forward=False, metric=False): """ - set_size_inches(w,h, forward=False) + set_size_inches(w,h, forward=False, metric=False) Set the figure size in inches (1in == 2.54cm) - + Usage:: fig.set_size_inches(w,h) # OR @@ -682,8 +682,9 @@ def set_size_inches(self, w, h=None, forward=False): optional kwarg *forward=True* will cause the canvas size to be automatically updated; e.g., you can resize the figure window from the shell - - ACCEPTS: a w,h tuple with w,h in inches + optional kwarg *metric=True* will allow the use of measurement input in centimeters + + ACCEPTS: a w,h tuple with w,h in inches (or cm if metric=True is specified) See Also -------- @@ -695,7 +696,11 @@ def set_size_inches(self, w, h=None, forward=False): # argument, so unpack them if h is None: w, h = w - + + if metric: + w*=(1/2.54) + h*=(1/2.54) + dpival = self.dpi self.bbox_inches.p1 = w, h @@ -708,83 +713,27 @@ def set_size_inches(self, w, h=None, forward=False): manager.resize(int(canvasw), int(canvash)) self.stale = True - def set_size_cm(self, w, h=None, forward=False): - """ - set_size_cm(w,h, forward=False) - - Set the figure size in centimeters - - Usage:: - - fig.set_size_cm(w,h) # OR - fig.set_size_cm((w,h) ) - - optional kwarg *forward=True* will cause the canvas size to be - automatically updated; e.g., you can resize the figure window - from the shell - - ACCEPTS: a w,h tuple with w,h in centimeters - - See Also - -------- - - matplotlib.Figure.get_size_cm - """ - - # the width and height have been passed in as a tuple to the first - # argument, so unpack them - in_w = w*0.3937007874015748031496062992126 - in_h = None - if h is None: - in_w, in_h = in_w - else: - in_h = h*0.3937007874015748031496062992126 - - dpival = self.dpi - self.bbox_inches.p1 = in_w, in_h - - if forward: - dpival = self.dpi - canvasw = in_w * dpival - canvash = in_h * dpival - manager = getattr(self.canvas, 'manager', None) - if manager is not None: - manager.resize(int(canvasw), int(canvash)) - self.stale = True - - def get_size_inches(self): + def get_size_inches(self, metric=False): """ Returns the current size of the figure in inches (1in == 2.54cm) as an numpy array. - + + optional kwarg *metric=True* will return size in centimeters + Returns ------- size : ndarray - The size of the figure in inches + The size of the figure in inches (or cm if metric=True is specified) See Also -------- matplotlib.Figure.set_size_inches """ - return np.array(self.bbox_inches.p1) - - def get_size_cm(self): - """ - Returns the current size of the figure in centimeters - as an numpy array. - - Returns - ------- - size : ndarray - The size of the figure in centimeters - - See Also - -------- - - matplotlib.Figure.set_size_cm - """ - return np.array(self.bbox_inches.p1)*2.54 + if metric: + return np.array(self.bbox_inches.p1)*2.54 + else: + return np.array(self.bbox_inches.p1) def get_edgecolor(self): 'Get the edge color of the Figure rectangle' From 661ec64b2a4d50ad1f22df46fc284dd774dea17a Mon Sep 17 00:00:00 2001 From: Robotia Date: Sat, 19 Sep 2015 18:41:32 -0400 Subject: [PATCH 4/5] Removed some whitespace accidentally left behind by my IDE --- lib/matplotlib/figure.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index c80cde12b104..61495eb43393 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -673,7 +673,7 @@ def set_size_inches(self, w, h=None, forward=False, metric=False): set_size_inches(w,h, forward=False, metric=False) Set the figure size in inches (1in == 2.54cm) - + Usage:: fig.set_size_inches(w,h) # OR @@ -683,7 +683,7 @@ def set_size_inches(self, w, h=None, forward=False, metric=False): automatically updated; e.g., you can resize the figure window from the shell optional kwarg *metric=True* will allow the use of measurement input in centimeters - + ACCEPTS: a w,h tuple with w,h in inches (or cm if metric=True is specified) See Also @@ -696,11 +696,11 @@ def set_size_inches(self, w, h=None, forward=False, metric=False): # argument, so unpack them if h is None: w, h = w - + if metric: w*=(1/2.54) h*=(1/2.54) - + dpival = self.dpi self.bbox_inches.p1 = w, h @@ -717,9 +717,9 @@ def get_size_inches(self, metric=False): """ Returns the current size of the figure in inches (1in == 2.54cm) as an numpy array. - + optional kwarg *metric=True* will return size in centimeters - + Returns ------- size : ndarray From 3232a7419f81c73b5a002347729495a05a8db018 Mon Sep 17 00:00:00 2001 From: Robotia Date: Sat, 19 Sep 2015 19:31:40 -0400 Subject: [PATCH 5/5] Removed additional kwarg and instead used get/set_size_cm functions passed to inch functions --- lib/matplotlib/figure.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 61495eb43393..b15b489a2f6e 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -668,9 +668,9 @@ def figimage(self, X, self.stale = True return im - def set_size_inches(self, w, h=None, forward=False, metric=False): + def set_size_inches(self, w, h=None, forward=False): """ - set_size_inches(w,h, forward=False, metric=False) + set_size_inches(w,h, forward=False) Set the figure size in inches (1in == 2.54cm) @@ -682,9 +682,8 @@ def set_size_inches(self, w, h=None, forward=False, metric=False): optional kwarg *forward=True* will cause the canvas size to be automatically updated; e.g., you can resize the figure window from the shell - optional kwarg *metric=True* will allow the use of measurement input in centimeters - ACCEPTS: a w,h tuple with w,h in inches (or cm if metric=True is specified) + ACCEPTS: a w,h tuple with w,h in inches See Also -------- @@ -697,10 +696,6 @@ def set_size_inches(self, w, h=None, forward=False, metric=False): if h is None: w, h = w - if metric: - w*=(1/2.54) - h*=(1/2.54) - dpival = self.dpi self.bbox_inches.p1 = w, h @@ -713,27 +708,28 @@ def set_size_inches(self, w, h=None, forward=False, metric=False): manager.resize(int(canvasw), int(canvash)) self.stale = True - def get_size_inches(self, metric=False): + def set_size_cm(self, w, h=None, **kwargs): + return self.set_size_inches(w*(1/2.54), h*(1/2.54) if h is not None else None, **kwargs) + + def get_size_inches(self): """ Returns the current size of the figure in inches (1in == 2.54cm) as an numpy array. - optional kwarg *metric=True* will return size in centimeters - Returns ------- size : ndarray - The size of the figure in inches (or cm if metric=True is specified) + The size of the figure in inches See Also -------- matplotlib.Figure.set_size_inches """ - if metric: - return np.array(self.bbox_inches.p1)*2.54 - else: - return np.array(self.bbox_inches.p1) + return np.array(self.bbox_inches.p1) + + def get_size_cm(self): + return np.array(self.bbox_inches.p1)*2.54 def get_edgecolor(self): 'Get the edge color of the Figure rectangle' @@ -1802,4 +1798,4 @@ def figaspect(arg): newsize = np.clip(newsize, figsize_min, figsize_max) return newsize -docstring.interpd.update(Figure=martist.kwdoc(Figure)) +docstring.interpd.update(Figure=martist.kwdoc(Figure)) \ No newline at end of file