From 580f98142e9c83a98cfe9d7e291bb0b31afdd0ba Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Fri, 18 Apr 2025 13:30:41 +0200 Subject: [PATCH 1/2] make cbar labelloc possible for all direction --- ultraplot/axes/base.py | 33 ++++++++++++++++++++++++++++++-- ultraplot/tests/test_colorbar.py | 8 ++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index 70d7c4f90..41c33bab3 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -1249,11 +1249,40 @@ def _add_colorbar( width=tickwidth * tickwidthratio, ) # noqa: E501 if label is not None: - obj.set_label(label) + # Note for some reason axis.set_label does not work here. We need to use set_x/ylabel explicitly + match loc: + case "top" | "bottom": + if labelloc in (None, "top", "bottom"): + obj.set_label(label) + elif labelloc in ("left", "right"): + obj.ax.set_ylabel(label) + else: + raise ValueError("Could not determined position") + case "left" | "right": + if labelloc in (None, "left", "right"): + obj.set_label(label) + elif labelloc in ("top", "bottom"): + obj.ax.set_xlabel(label) + else: + raise ValueError("Could not determined position") + # Default to setting label on long axis + case _: + obj.set_label(label) if labelloc is not None: + # Temporarily modify the axis to set the label and its properties + match loc: + case "top" | "bottom": + if labelloc in ("left", "right"): + axis = obj._short_axis() + case "left" | "right": + if labelloc in ("top", "bottom"): + axis = obj._short_axis() + case _: + raise ValueError("Location not understood.") axis.set_label_position(labelloc) axis.label.update(kw_label) - for label in axis.get_ticklabels(): + # Assume ticks are set on the long axis(!) + for label in obj._long_axis().get_ticklabels(): label.update(kw_ticklabels) kw_outline = {"edgecolor": color, "linewidth": linewidth} if obj.outline is not None: diff --git a/ultraplot/tests/test_colorbar.py b/ultraplot/tests/test_colorbar.py index 28866c71e..552e8ef4f 100644 --- a/ultraplot/tests/test_colorbar.py +++ b/ultraplot/tests/test_colorbar.py @@ -275,3 +275,11 @@ def test_draw_edges(): axi.colorbar(h, drawedges=drawedges) axi.set_title(f"{drawedges=}") return fig + + +def test_label_placement_colorbar(): + data = np.random.rand(10, 10) + fig, ax = uplt.subplots() + h = ax.imshow(data) + for labelloc in "top bottom left right".split(): + ax.colorbar(h, loc="right", labelloc=labelloc) From 4493b299e33f96cc27c76fc8f4d2347da8f6eb36 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Fri, 18 Apr 2025 13:51:48 +0200 Subject: [PATCH 2/2] update unittests --- ultraplot/tests/test_colorbar.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ultraplot/tests/test_colorbar.py b/ultraplot/tests/test_colorbar.py index 552e8ef4f..98a2ec112 100644 --- a/ultraplot/tests/test_colorbar.py +++ b/ultraplot/tests/test_colorbar.py @@ -278,8 +278,13 @@ def test_draw_edges(): def test_label_placement_colorbar(): + """ + Ensure that all potential combinations of colorbar + label placement is possible. + """ data = np.random.rand(10, 10) fig, ax = uplt.subplots() h = ax.imshow(data) - for labelloc in "top bottom left right".split(): - ax.colorbar(h, loc="right", labelloc=labelloc) + locs = "top bottom left right".split() + for loc, labelloc in zip(locs, locs): + ax.colorbar(h, loc=loc, labelloc=labelloc)