From 1cfdb741661c2c9083235f258512c39cecae0da0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 6 Feb 2019 17:30:02 +0100 Subject: [PATCH] Improve Axes selection in Qt figure options. 1. Make sure Axes "labels" in the input dialog are unique (QInputDialog.getItem returns the selected string, but we'll get the wrong index if two axes share e.g. the same title), by including the axes id() if necessary; conversely, don't include the id() in the user-facing UI unless necessary. For example, previously, after fig, axs = plt.subplots(2) axs[0].set(xlabel="foo"); axs[1].set(xlabel="foo") the input dialog would not allow selecting the second axes. 2. If the Axes artist has a "label" attached to it (per `Artist.set_label`), use it in priority for the input dialog. Normally, axes labels are not used (they are used for other artists for the legend, for example). This change allows us to attach a "(colorbar)" label to colorbar axes, which makes it easier to select the right axes. After fig, axs = plt.subplots(2) axs[0].imshow([[0]]); fig.colorbar(axs[0].images[0]) axs[1].imshow([[0]]); fig.colorbar(axs[1].images[0]) previously the input dialog would have the entries but now it has the entries (id: 0x...) (id: 0x...) (colorbar) (id: 0x...) (colorbar) (id: 0x...) --- lib/matplotlib/backends/backend_qt5.py | 35 +++++++++++++------------- lib/matplotlib/colorbar.py | 4 +-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index d6b3e9f9f0ba..f1966b1848c9 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -758,30 +758,31 @@ def sizeHint(self): return size def edit_parameters(self): - allaxes = self.canvas.figure.get_axes() - if not allaxes: + axes = self.canvas.figure.get_axes() + if not axes: QtWidgets.QMessageBox.warning( self.parent, "Error", "There are no axes to edit.") return - elif len(allaxes) == 1: - axes, = allaxes + elif len(axes) == 1: + ax, = axes else: - titles = [] - for axes in allaxes: - name = (axes.get_title() or - " - ".join(filter(None, [axes.get_xlabel(), - axes.get_ylabel()])) or - "".format( - type(axes).__name__, id(axes))) - titles.append(name) + titles = [ + ax.get_label() or + ax.get_title() or + " - ".join(filter(None, [ax.get_xlabel(), ax.get_ylabel()])) or + f"" + for ax in axes] + duplicate_titles = [ + title for title in titles if titles.count(title) > 1] + for i, ax in enumerate(axes): + if titles[i] in duplicate_titles: + titles[i] += f" (id: {id(ax):#x})" # Deduplicate titles. item, ok = QtWidgets.QInputDialog.getItem( self.parent, 'Customize', 'Select axes:', titles, 0, False) - if ok: - axes = allaxes[titles.index(item)] - else: + if not ok: return - - figureoptions.figure_edit(axes, self) + ax = axes[titles.index(item)] + figureoptions.figure_edit(ax, self) def _update_buttons_checked(self): # sync button checkstates to match active mode diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 712b8843ed9e..e6b640e017ba 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1437,7 +1437,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, if parent_anchor is not False: ax.set_anchor(parent_anchor) - cax = fig.add_axes(pbcb) + cax = fig.add_axes(pbcb, label="") # OK, now make a layoutbox for the cb axis. Later, we will use this # to make the colorbar fit nicely. @@ -1551,7 +1551,7 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw): parent.set_anchor(panchor) fig = parent.get_figure() - cax = fig.add_subplot(gs2[1]) + cax = fig.add_subplot(gs2[1], label="") cax.set_aspect(aspect, anchor=anchor, adjustable='box') return cax, kw