From baf1591fbe5427667f42cd7dc95598bde74a3399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 10:15:54 -0500 Subject: [PATCH 1/9] Update super style --- gwhat/gwrecharge/gwrecharge_plot_results.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index d8305cc7..b0374e87 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -970,7 +970,7 @@ def __init__(self, setp={}): self._xticklabels_yt = 2 def plot(self, glue_df): - super(FigWaterBudgetGLUE, self).plot() + super().plot() ax = self.ax0 glue_yrly = glue_df['hydrol yearly budget'] @@ -1189,7 +1189,7 @@ def __init__(self, setp={}): self.setp['xticks size'] = 12 def plot(self, glue_data): - super(FigYearlyRechgGLUE, self).plot() + super().plot() ax0 = self.ax0 self.ax0.set_axisbelow(True) @@ -1392,7 +1392,7 @@ def __init__(self, setp={}): self.setp['ylabel size'] = 16 def plot(self, glue_df): - super(FigAvgYearlyBudget, self).plot() + super().plot() avg_yrly = { 'evapo': np.nanmean(glue_df['yearly budget']['evapo']), @@ -1518,7 +1518,7 @@ def __init__(self, setp={}): def plot(self, glue_df): """Plot the results.""" - super(FigAvgMonthlyBudget, self).plot() + super().plot() avg_mly = { 'evapo': np.nanmean( glue_df['monthly budget']['evapo'][:, :, 2], axis=0), From b105e5b6646533a987bd98519858b09c0ff6d202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 10:43:00 -0500 Subject: [PATCH 2/9] Update super calls --- gwhat/gwrecharge/gwrecharge_plot_results.py | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index b0374e87..e7496b0b 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -54,7 +54,7 @@ class FigureStackManager(QWidget): def __init__(self, parent=None): - super(FigureStackManager, self).__init__(parent) + super().__init__(parent) self.setMinimumSize(1250, 650) self.setWindowTitle('Recharge Results') self.setWindowFlags(Qt.Window) @@ -147,7 +147,7 @@ def show(self): self.setWindowState(self.windowState() & ~Qt.WindowMinimized) else: self.resize(self.size()) - super(FigureStackManager, self).show() + super().show() self.plot_results() @@ -160,7 +160,7 @@ class FigSetpPanelManager(QWidget): """ def __init__(self, figcanvas, parent=None): - super(FigSetpPanelManager, self).__init__(parent) + super().__init__(parent) self.figsetp_panels = [] self.set_figcanvas(figcanvas) self.setup() @@ -201,7 +201,7 @@ def add_figsetp_panel(self, figsetp_panel): class SetpPanelBase(QWidget): def __init__(self, parent=None): - super(SetpPanelBase, self).__init__(parent) + super().__init__(parent) layout = QGridLayout(self) layout.setContentsMargins(0, 0, 0, 0) @@ -212,7 +212,7 @@ def register_figcanvas(self, figcanvas): class MarginSizePanel(SetpPanelBase): def __init__(self, parent=None): - super(MarginSizePanel, self).__init__(parent) + super().__init__(parent) self.setup() def setup(self): @@ -275,7 +275,7 @@ def update_from_setp(self, setp): class TextOptPanel(SetpPanelBase): def __init__(self, **kwargs): - super(TextOptPanel, self).__init__(parent=None) + super().__init__(parent=None) self.setup(**kwargs) def setup(self, xlabelsize=True, xticksize=True, ylabelsize=True, @@ -412,7 +412,7 @@ def _notes_changed(self): class FigSizePanel(SetpPanelBase): def __init__(self, parent=None): - super(FigSizePanel, self).__init__(parent) + super().__init__(parent) self.setup() def setup(self): @@ -484,7 +484,7 @@ def _fig_size_changed(self): class YearLimitsPanel(SetpPanelBase): def __init__(self, parent=None): - super(YearLimitsPanel, self).__init__(parent) + super().__init__(parent) self.setup() def setup(self): @@ -540,7 +540,7 @@ def _xaxis_changed(self): class YAxisOptPanel(SetpPanelBase): def __init__(self, parent=None): - super(YAxisOptPanel, self).__init__(parent) + super().__init__(parent) self.setup() def setup(self): @@ -626,7 +626,7 @@ class FigManagerBase(QWidget): Abstract manager to show the results from GLUE. """ def __init__(self, figure_canvas, setp_panels=[], parent=None): - super(FigManagerBase, self).__init__(parent) + super().__init__(parent) self.savefig_dir = os.getcwd() self.figcanvas = figure_canvas(setp={}) @@ -647,7 +647,7 @@ def __init__(self, figure_canvas, setp_panels=[], parent=None): layout.setRowStretch(1, 100) def resizeEvent(self, event): - super(FigManagerBase, self).resizeEvent(event) + super().resizeEvent(event) self.figsetp_manager.setMinimumWidth( self.figsetp_manager.view.minimumSizeHint().width() + 2*self.figsetp_manager.scrollarea.frameWidth() + @@ -754,7 +754,7 @@ class FigCanvasBase(FigureCanvasQTAgg): MARGINS = [1, 0.15, 0.15, 0.65] # left, top, right, bottom def __init__(self, setp={}): - super(FigCanvasBase, self).__init__(MplFigure()) + super().__init__(MplFigure()) self.xticklabels = [] self.notes = [] self._xticklabels_yt = 0 @@ -966,7 +966,7 @@ class FigWaterBudgetGLUE(FigCanvasBase): MARGINS = [1, 0.15, 0.15, 1.1] def __init__(self, setp={}): - super(FigWaterBudgetGLUE, self).__init__(setp) + super().__init__(setp) self._xticklabels_yt = 2 def plot(self, glue_df): @@ -1128,7 +1128,7 @@ def set_xlimits(self, xmin, xmax): def setup_ylimits(self): """Setup the limits of the yaxis.""" - super(FigWaterBudgetGLUE, self).setup_ylimits() + super().setup_ylimits() self.setup_xticklabels() def setup_language(self): @@ -1183,7 +1183,7 @@ class FigYearlyRechgGLUE(FigCanvasBase): FIGNAME = "gw_rechg_glue" def __init__(self, setp={}): - super(FigYearlyRechgGLUE, self).__init__(setp) + super().__init__(setp) self._xticklabels_yt = 4 self.setp['legend size'] = 12 self.setp['xticks size'] = 12 @@ -1310,7 +1310,7 @@ def set_xlimits(self, xmin, xmax): def setup_ylimits(self): """Setup the limits of the yaxis""" - super(FigYearlyRechgGLUE, self).setup_ylimits() + super().setup_ylimits() self.setup_xticklabels() def setup_axes_labels(self): @@ -1383,7 +1383,7 @@ class FigAvgYearlyBudget(FigCanvasBase): MARGINS = [1, 0.15, 0.15, 0.35] def __init__(self, setp={}): - super(FigAvgYearlyBudget, self).__init__(setp) + super().__init__(setp) self._xticklabels_yt = 5 self.bar_handles = [] self.setp['xticks size'] = 12 @@ -1507,7 +1507,7 @@ class FigAvgMonthlyBudget(FigCanvasBase): MARGINS = [1, 0.35, 0.15, 0.35] def __init__(self, setp={}): - super(FigAvgMonthlyBudget, self).__init__(setp) + super().__init__(setp) self._xticklabels_yt = 5 self.lg_handles = [] self.setp['xticks size'] = 14 From adbbeca50ee348bebe8e6de0ce8b8a9794cd275a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 10:43:39 -0500 Subject: [PATCH 3/9] FigCanvasBase: rework the plot interface --- gwhat/gwrecharge/gwrecharge_plot_results.py | 24 ++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index e7496b0b..84ff5584 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -813,11 +813,20 @@ def clear_figure(self, silent=True): if not silent: self.sig_fig_changed.emit(self.figure) - def plot(self): + def plot(self, glue_data): """Plot the data.""" if self.ax0 is None: self.setup_ax() self.clear_ax() + self.__plot__(glue_data) + self.refresh_margins() + + def __plot__(self, glue_data): + """ + This method needs to be reimplemented in all figure canvas that + inherit this base class. + """ + raise NotImplementedError def set_language(self, language): """Set the language of the text shown in the figure.""" @@ -969,8 +978,7 @@ def __init__(self, setp={}): super().__init__(setp) self._xticklabels_yt = 2 - def plot(self, glue_df): - super().plot() + def __plot__(self, glue_df): ax = self.ax0 glue_yrly = glue_df['hydrol yearly budget'] @@ -1188,8 +1196,7 @@ def __init__(self, setp={}): self.setp['legend size'] = 12 self.setp['xticks size'] = 12 - def plot(self, glue_data): - super().plot() + def __plot__(self, glue_data): ax0 = self.ax0 self.ax0.set_axisbelow(True) @@ -1391,9 +1398,7 @@ def __init__(self, setp={}): self.setp['notes size'] = 12 self.setp['ylabel size'] = 16 - def plot(self, glue_df): - super().plot() - + def __plot__(self, glue_df): avg_yrly = { 'evapo': np.nanmean(glue_df['yearly budget']['evapo']), 'runoff': np.nanmean(glue_df['yearly budget']['runoff']), @@ -1516,9 +1521,8 @@ def __init__(self, setp={}): self.setp['ylabel size'] = 16 self.setp['legend size'] = 12 - def plot(self, glue_df): + def __plot__(self, glue_df): """Plot the results.""" - super().plot() avg_mly = { 'evapo': np.nanmean( glue_df['monthly budget']['evapo'][:, :, 2], axis=0), From ea60773c2cea3d4a9a1934b2584f922ba8aba26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 10:46:39 -0500 Subject: [PATCH 4/9] Fix pep8 issues --- gwhat/gwrecharge/gwrecharge_plot_results.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 84ff5584..091980dd 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -720,7 +720,7 @@ def _select_savefig_path(self): ffmat = "*.pdf;;*.svg;;*.png" fname, ftype = QFileDialog.getSaveFileName( - self, "Save Figure", figname, ffmat) + self, "Save Figure", figname, ffmat) if fname: ftype = ftype.replace('*', '') fname = fname if fname.endswith(ftype) else fname + ftype From 660824894cef6206ae6393c866294d7cb5f60c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 12:09:25 -0500 Subject: [PATCH 5/9] FigCanvasBase: auto setup margins width --- gwhat/gwrecharge/gwrecharge_plot_results.py | 69 +++++++++++++++++++-- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 091980dd..ad639a1e 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -813,13 +813,20 @@ def clear_figure(self, silent=True): if not silent: self.sig_fig_changed.emit(self.figure) + def draw(self): + """ + Extend matplotlib canvas class draw method to automatically + adjust the figure margins width. + """ + super().draw() + self.refresh_margins(silent=True) + def plot(self, glue_data): """Plot the data.""" if self.ax0 is None: self.setup_ax() self.clear_ax() self.__plot__(glue_data) - self.refresh_margins() def __plot__(self, glue_data): """ @@ -870,12 +877,62 @@ def set_axes_margins_inches(self, margins): def refresh_margins(self, silent=False): """Refresh the axes marings using the values defined in setp.""" - left = self.setp['left margin']/self.setp['fwidth'] - top = self.setp['top margin']/self.setp['fheight'] - right = self.setp['right margin']/self.setp['fwidth'] - bottom = self.setp['bottom margin']/self.setp['fheight'] + if self.ax0 is None: + return + + figheight = self.figure.get_figheight() + figwidth = self.figure.get_figwidth() + figborderpad = 0.15 + + figbbox = self.figure.bbox + ax = self.ax0 + axbbox = self.ax0.bbox + + renderer = self.get_renderer() + bbox_xaxis_bottom, bbox_xaxis_top = ( + ax.xaxis.get_ticklabel_extents(renderer)) + bbox_yaxis_left, bbox_yaxis_right = ( + ax.yaxis.get_ticklabel_extents(renderer)) + + bbox_yaxis_label = ax.yaxis.label.get_window_extent(renderer) + bbox_xaxis_label = ax.xaxis.label.get_window_extent(renderer) + + # Calculate left margin width. + left_margin = ( + figborderpad / figwidth + + (axbbox.x0 - bbox_yaxis_label.x0) / figbbox.width) + + # Calculate right margin width. + right_margin = ( + figborderpad / figwidth + + max((bbox_xaxis_bottom.x1 - axbbox.x1) / figbbox.width, + (bbox_xaxis_top.x1 - axbbox.x1) / figbbox.width, + 0)) + + # Calculate top margin height. + top_margin = ( + figborderpad / figheight + + max((bbox_yaxis_left.y1 - axbbox.y1) / figbbox.height, + (bbox_yaxis_right.y1 - axbbox.y1) / figbbox.height, + 0)) + + # Calculate bottom margin height. + xticklabels_max_y0 = max( + [xticklabel.get_window_extent(renderer).y0 for + xticklabel in self.xticklabels] + [0]) + bottom_margin = ( + figborderpad / figheight + + max((axbbox.y0 - bbox_xaxis_label.y0) / figbbox.height, + (axbbox.y0 - bbox_xaxis_bottom.y0) / figbbox.height, + (axbbox.y0 - xticklabels_max_y0) / figbbox.height, + 0)) + + # Setup axe position. for ax in self.figure.axes: - ax.set_position([left, bottom, 1-left-right, 1-top-bottom]) + ax.set_position([ + left_margin, bottom_margin, + 1 - left_margin - right_margin, + 1 - top_margin - bottom_margin]) if not silent: self.sig_fig_changed.emit(self.figure) From 5182e3e877a2560f8e3210428d27e40c25a5b246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 12:10:38 -0500 Subject: [PATCH 6/9] Remove MarginSizePanel --- gwhat/gwrecharge/gwrecharge_plot_results.py | 69 +-------------------- 1 file changed, 1 insertion(+), 68 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index ad639a1e..4d4f554e 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -73,27 +73,23 @@ def setup_stack(self): fig_rechg_glue = FigManagerBase( FigYearlyRechgGLUE, setp_panels=[FigSizePanel(), - MarginSizePanel(), YAxisOptPanel(), YearLimitsPanel(), TextOptPanel()]) fig_watbudg_glue = FigManagerBase( FigWaterBudgetGLUE, setp_panels=[FigSizePanel(), - MarginSizePanel(), YAxisOptPanel(), YearLimitsPanel(), TextOptPanel()]) fig_avg_yearly_budg = FigManagerBase( FigAvgYearlyBudget, setp_panels=[FigSizePanel(), - MarginSizePanel(), YAxisOptPanel(), TextOptPanel(xlabelsize=False, legendsize=False)]) fig_avg_monthly_budg = FigManagerBase( FigAvgMonthlyBudget, setp_panels=[FigSizePanel(), - MarginSizePanel(), YAxisOptPanel(), TextOptPanel(xlabelsize=False, notesize=False)]) @@ -210,69 +206,6 @@ def register_figcanvas(self, figcanvas): figcanvas.sig_newfig_plotted.connect(self.update_from_setp) -class MarginSizePanel(SetpPanelBase): - def __init__(self, parent=None): - super().__init__(parent) - self.setup() - - def setup(self): - """Setup the gui of the panel.""" - self.layout().addWidget(self._setup_margins_grpbox()) - self.layout().setRowStretch(self.layout().rowCount(), 100) - - @property - def fig_margins(self): - return [self._spb_margins[loc].value() for loc in LOCS] - - def _setup_margins_grpbox(self): - """ - Setup a group box with spin boxes that allows to set the figure - margins size in inches. - """ - grpbox = QGroupBox("Margins Size :") - layout = QGridLayout(grpbox) - - self._spb_margins = {} - for row, loc in enumerate(LOCS): - self._spb_margins[loc] = QDoubleSpinBox() - self._spb_margins[loc].setSingleStep(0.05) - self._spb_margins[loc].setMinimum(0) - self._spb_margins[loc].setSuffix(' in') - self._spb_margins[loc].setAlignment(Qt.AlignCenter) - self._spb_margins[loc].setKeyboardTracking(False) - self._spb_margins[loc].valueChanged.connect(self._margins_changed) - - layout.addWidget(QLabel("%s :" % loc), row, 0) - layout.addWidget(self._spb_margins[loc], row, 2) - layout.setColumnStretch(1, 100) - layout.setContentsMargins(10, 10, 10, 10) # (L, T, R, B) - - return grpbox - - @QSlot() - def _margins_changed(self): - """Handle when one of the margin size is changed by the user.""" - self.figcanvas.set_axes_margins_inches(self.fig_margins) - - @QSlot(dict) - def update_from_setp(self, setp): - self._spb_margins['left'].blockSignals(True) - self._spb_margins['left'].setValue(setp['left margin']) - self._spb_margins['left'].blockSignals(False) - - self._spb_margins['right'].blockSignals(True) - self._spb_margins['right'].setValue(setp['right margin']) - self._spb_margins['right'].blockSignals(False) - - self._spb_margins['top'].blockSignals(True) - self._spb_margins['top'].setValue(setp['top margin']) - self._spb_margins['top'].blockSignals(False) - - self._spb_margins['bottom'].blockSignals(True) - self._spb_margins['bottom'].setValue(setp['bottom margin']) - self._spb_margins['bottom'].blockSignals(False) - - class TextOptPanel(SetpPanelBase): def __init__(self, **kwargs): super().__init__(parent=None) @@ -620,11 +553,11 @@ def _yaxis_changed(self): # ---- Figure managers - class FigManagerBase(QWidget): """ Abstract manager to show the results from GLUE. """ + def __init__(self, figure_canvas, setp_panels=[], parent=None): super().__init__(parent) self.savefig_dir = os.getcwd() From c0f21d2eabd0a4b20ff647f1a3f08333e10e3332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 12:15:17 -0500 Subject: [PATCH 7/9] FigCanvasBase: rework margins setp --- gwhat/gwrecharge/gwrecharge_plot_results.py | 65 +++++++++++---------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 4d4f554e..255cbb3c 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -684,7 +684,6 @@ class FigCanvasBase(FigureCanvasQTAgg): 'light grey': '0.85'} FWIDTH, FHEIGHT = 8.5, 5 - MARGINS = [1, 0.15, 0.15, 0.65] # left, top, right, bottom def __init__(self, setp={}): super().__init__(MplFigure()) @@ -706,13 +705,13 @@ def set_figure_setp(self, setp): if 'fheight' not in self.setp.keys(): self.setp['fheight'] = self.FHEIGHT if 'left margin' not in self.setp.keys(): - self.setp['left margin'] = self.MARGINS[0] + self.setp['left margin'] = None if 'top margin' not in self.setp.keys(): - self.setp['top margin'] = self.MARGINS[1] + self.setp['top margin'] = None if 'right margin' not in self.setp.keys(): - self.setp['right margin'] = self.MARGINS[2] + self.setp['right margin'] = None if 'bottom margin' not in self.setp.keys(): - self.setp['bottom margin'] = self.MARGINS[3] + self.setp['bottom margin'] = None if 'xlabel size' not in self.setp.keys(): self.setp['xlabel size'] = 16 if 'xticks size' not in self.setp.keys(): @@ -831,34 +830,42 @@ def refresh_margins(self, silent=False): bbox_xaxis_label = ax.xaxis.label.get_window_extent(renderer) # Calculate left margin width. - left_margin = ( - figborderpad / figwidth + - (axbbox.x0 - bbox_yaxis_label.x0) / figbbox.width) + left_margin = self.setp['left margin'] + if left_margin is None: + left_margin = ( + figborderpad / figwidth + + (axbbox.x0 - bbox_yaxis_label.x0) / figbbox.width) # Calculate right margin width. - right_margin = ( - figborderpad / figwidth + - max((bbox_xaxis_bottom.x1 - axbbox.x1) / figbbox.width, - (bbox_xaxis_top.x1 - axbbox.x1) / figbbox.width, - 0)) + right_margin = self.setp['right margin'] + if right_margin is None: + right_margin = ( + figborderpad / figwidth + + max((bbox_xaxis_bottom.x1 - axbbox.x1) / figbbox.width, + (bbox_xaxis_top.x1 - axbbox.x1) / figbbox.width, + 0)) # Calculate top margin height. - top_margin = ( - figborderpad / figheight + - max((bbox_yaxis_left.y1 - axbbox.y1) / figbbox.height, - (bbox_yaxis_right.y1 - axbbox.y1) / figbbox.height, - 0)) + top_margin = self.setp['top margin'] + if top_margin is None: + top_margin = ( + figborderpad / figheight + + max((bbox_yaxis_left.y1 - axbbox.y1) / figbbox.height, + (bbox_yaxis_right.y1 - axbbox.y1) / figbbox.height, + 0)) # Calculate bottom margin height. - xticklabels_max_y0 = max( - [xticklabel.get_window_extent(renderer).y0 for - xticklabel in self.xticklabels] + [0]) - bottom_margin = ( - figborderpad / figheight + - max((axbbox.y0 - bbox_xaxis_label.y0) / figbbox.height, - (axbbox.y0 - bbox_xaxis_bottom.y0) / figbbox.height, - (axbbox.y0 - xticklabels_max_y0) / figbbox.height, - 0)) + bottom_margin = self.setp['bottom margin'] + if bottom_margin is None: + xticklabels_max_y0 = max( + [xticklabel.get_window_extent(renderer).y0 for + xticklabel in self.xticklabels] + [0]) + bottom_margin = ( + figborderpad / figheight + + max((axbbox.y0 - bbox_xaxis_label.y0) / figbbox.height, + (axbbox.y0 - bbox_xaxis_bottom.y0) / figbbox.height, + (axbbox.y0 - xticklabels_max_y0) / figbbox.height, + 0)) # Setup axe position. for ax in self.figure.axes: @@ -962,7 +969,6 @@ def _get_xlabel_xt(self, fontsize, rotation): class FigWaterBudgetGLUE(FigCanvasBase): FIGNAME = "water_budget_glue" FWIDTH, FHEIGHT = 15, 7 - MARGINS = [1, 0.15, 0.15, 1.1] def __init__(self, setp={}): super().__init__(setp) @@ -1177,7 +1183,6 @@ class FigYearlyRechgGLUE(FigCanvasBase): uncertainty. """ - MARGINS = [1, 0.15, 0.15, 0.9] # left, top, right, bottom FIGNAME = "gw_rechg_glue" def __init__(self, setp={}): @@ -1377,7 +1382,6 @@ class FigAvgYearlyBudget(FigCanvasBase): """ FIGNAME = "avg_yearly_water_budget" FWIDTH, FHEIGHT = 8, 4.5 - MARGINS = [1, 0.15, 0.15, 0.35] def __init__(self, setp={}): super().__init__(setp) @@ -1499,7 +1503,6 @@ class FigAvgMonthlyBudget(FigCanvasBase): """ FIGNAME = "avg_monthly_water_budget" FWIDTH, FHEIGHT = 8, 4.5 - MARGINS = [1, 0.35, 0.15, 0.35] def __init__(self, setp={}): super().__init__(setp) From 3de91393eefd87e7a4e27e184c5cb4133d3b9b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 12:21:40 -0500 Subject: [PATCH 8/9] Take into account legend when setting top margin --- gwhat/gwrecharge/gwrecharge_plot_results.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 255cbb3c..0c08e9d9 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -811,7 +811,7 @@ def refresh_margins(self, silent=False): """Refresh the axes marings using the values defined in setp.""" if self.ax0 is None: return - + figheight = self.figure.get_figheight() figwidth = self.figure.get_figwidth() figborderpad = 0.15 @@ -829,6 +829,11 @@ def refresh_margins(self, silent=False): bbox_yaxis_label = ax.yaxis.label.get_window_extent(renderer) bbox_xaxis_label = ax.xaxis.label.get_window_extent(renderer) + if self.ax0.get_legend() is not None: + legend_bbox = self.ax0.get_legend().get_window_extent(renderer) + else: + legend_bbox = self.ax0.bbox + # Calculate left margin width. left_margin = self.setp['left margin'] if left_margin is None: @@ -848,11 +853,12 @@ def refresh_margins(self, silent=False): # Calculate top margin height. top_margin = self.setp['top margin'] if top_margin is None: - top_margin = ( - figborderpad / figheight + - max((bbox_yaxis_left.y1 - axbbox.y1) / figbbox.height, - (bbox_yaxis_right.y1 - axbbox.y1) / figbbox.height, - 0)) + top_margin = figborderpad / figheight + top_margin += max( + (bbox_yaxis_left.y1 - axbbox.y1) / figbbox.height, + (bbox_yaxis_right.y1 - axbbox.y1) / figbbox.height, + (legend_bbox.y1 - axbbox.y1) / figbbox.height, + 0) # Calculate bottom margin height. bottom_margin = self.setp['bottom margin'] From 904b2c2397b9b24fcca09069a8797e3ced4b2e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Fri, 26 Feb 2021 12:42:35 -0500 Subject: [PATCH 9/9] Minor codestyle changes --- gwhat/gwrecharge/gwrecharge_plot_results.py | 33 ++++++++++----------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 0c08e9d9..bb4eac86 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -811,7 +811,7 @@ def refresh_margins(self, silent=False): """Refresh the axes marings using the values defined in setp.""" if self.ax0 is None: return - + figheight = self.figure.get_figheight() figwidth = self.figure.get_figwidth() figborderpad = 0.15 @@ -837,28 +837,26 @@ def refresh_margins(self, silent=False): # Calculate left margin width. left_margin = self.setp['left margin'] if left_margin is None: - left_margin = ( - figborderpad / figwidth + - (axbbox.x0 - bbox_yaxis_label.x0) / figbbox.width) + left_margin = max( + (axbbox.x0 - bbox_yaxis_label.x0) / figbbox.width, + 0) + figborderpad / figwidth # Calculate right margin width. right_margin = self.setp['right margin'] if right_margin is None: - right_margin = ( - figborderpad / figwidth + - max((bbox_xaxis_bottom.x1 - axbbox.x1) / figbbox.width, - (bbox_xaxis_top.x1 - axbbox.x1) / figbbox.width, - 0)) + right_margin = max( + (bbox_xaxis_bottom.x1 - axbbox.x1) / figbbox.width, + (bbox_xaxis_top.x1 - axbbox.x1) / figbbox.width, + 0) + figborderpad / figwidth # Calculate top margin height. top_margin = self.setp['top margin'] if top_margin is None: - top_margin = figborderpad / figheight - top_margin += max( + top_margin = max( (bbox_yaxis_left.y1 - axbbox.y1) / figbbox.height, (bbox_yaxis_right.y1 - axbbox.y1) / figbbox.height, (legend_bbox.y1 - axbbox.y1) / figbbox.height, - 0) + 0) + figborderpad / figheight # Calculate bottom margin height. bottom_margin = self.setp['bottom margin'] @@ -866,12 +864,11 @@ def refresh_margins(self, silent=False): xticklabels_max_y0 = max( [xticklabel.get_window_extent(renderer).y0 for xticklabel in self.xticklabels] + [0]) - bottom_margin = ( - figborderpad / figheight + - max((axbbox.y0 - bbox_xaxis_label.y0) / figbbox.height, - (axbbox.y0 - bbox_xaxis_bottom.y0) / figbbox.height, - (axbbox.y0 - xticklabels_max_y0) / figbbox.height, - 0)) + bottom_margin = max( + (axbbox.y0 - bbox_xaxis_label.y0) / figbbox.height, + (axbbox.y0 - bbox_xaxis_bottom.y0) / figbbox.height, + (axbbox.y0 - xticklabels_max_y0) / figbbox.height, + 0) + figborderpad / figheight # Setup axe position. for ax in self.figure.axes: