Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cfdb74

Browse files
committed
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 <anonymous AxesSubplot (id: 0x...)> <anonymous AxesSubplot (id: 0x...)> <anonymous AxesSubplot (id: 0x...)> <anonymous AxesSubplot (id: 0x...)> but now it has the entries <anonymous AxesSubplot> (id: 0x...) <anonymous AxesSubplot> (id: 0x...) (colorbar) (id: 0x...) (colorbar) (id: 0x...)
1 parent aab3e8a commit 1cfdb74

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -758,30 +758,31 @@ def sizeHint(self):
758758
return size
759759

760760
def edit_parameters(self):
761-
allaxes = self.canvas.figure.get_axes()
762-
if not allaxes:
761+
axes = self.canvas.figure.get_axes()
762+
if not axes:
763763
QtWidgets.QMessageBox.warning(
764764
self.parent, "Error", "There are no axes to edit.")
765765
return
766-
elif len(allaxes) == 1:
767-
axes, = allaxes
766+
elif len(axes) == 1:
767+
ax, = axes
768768
else:
769-
titles = []
770-
for axes in allaxes:
771-
name = (axes.get_title() or
772-
" - ".join(filter(None, [axes.get_xlabel(),
773-
axes.get_ylabel()])) or
774-
"<anonymous {} (id: {:#x})>".format(
775-
type(axes).__name__, id(axes)))
776-
titles.append(name)
769+
titles = [
770+
ax.get_label() or
771+
ax.get_title() or
772+
" - ".join(filter(None, [ax.get_xlabel(), ax.get_ylabel()])) or
773+
f"<anonymous {type(ax).__name__}>"
774+
for ax in axes]
775+
duplicate_titles = [
776+
title for title in titles if titles.count(title) > 1]
777+
for i, ax in enumerate(axes):
778+
if titles[i] in duplicate_titles:
779+
titles[i] += f" (id: {id(ax):#x})" # Deduplicate titles.
777780
item, ok = QtWidgets.QInputDialog.getItem(
778781
self.parent, 'Customize', 'Select axes:', titles, 0, False)
779-
if ok:
780-
axes = allaxes[titles.index(item)]
781-
else:
782+
if not ok:
782783
return
783-
784-
figureoptions.figure_edit(axes, self)
784+
ax = axes[titles.index(item)]
785+
figureoptions.figure_edit(ax, self)
785786

786787
def _update_buttons_checked(self):
787788
# sync button checkstates to match active mode

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14371437
if parent_anchor is not False:
14381438
ax.set_anchor(parent_anchor)
14391439

1440-
cax = fig.add_axes(pbcb)
1440+
cax = fig.add_axes(pbcb, label="<colorbar>")
14411441

14421442
# OK, now make a layoutbox for the cb axis. Later, we will use this
14431443
# to make the colorbar fit nicely.
@@ -1551,7 +1551,7 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
15511551
parent.set_anchor(panchor)
15521552

15531553
fig = parent.get_figure()
1554-
cax = fig.add_subplot(gs2[1])
1554+
cax = fig.add_subplot(gs2[1], label="<colorbar>")
15551555
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
15561556
return cax, kw
15571557

0 commit comments

Comments
 (0)