Skip to content

make cbar labelloc possible for all direction #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions ultraplot/axes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,11 +1249,40 @@
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)

Check warning on line 1258 in ultraplot/axes/base.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/axes/base.py#L1258

Added line #L1258 was not covered by tests
else:
raise ValueError("Could not determined position")

Check warning on line 1260 in ultraplot/axes/base.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/axes/base.py#L1260

Added line #L1260 was not covered by tests
case "left" | "right":
if labelloc in (None, "left", "right"):
obj.set_label(label)
elif labelloc in ("top", "bottom"):
obj.ax.set_xlabel(label)

Check warning on line 1265 in ultraplot/axes/base.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/axes/base.py#L1265

Added line #L1265 was not covered by tests
else:
raise ValueError("Could not determined position")

Check warning on line 1267 in ultraplot/axes/base.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/axes/base.py#L1267

Added line #L1267 was not covered by tests
# 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()

Check warning on line 1276 in ultraplot/axes/base.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/axes/base.py#L1276

Added line #L1276 was not covered by tests
case "left" | "right":
if labelloc in ("top", "bottom"):
axis = obj._short_axis()
case _:
raise ValueError("Location not understood.")

Check warning on line 1281 in ultraplot/axes/base.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/axes/base.py#L1279-L1281

Added lines #L1279 - L1281 were not covered by tests
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:
Expand Down
13 changes: 13 additions & 0 deletions ultraplot/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,16 @@ def test_draw_edges():
axi.colorbar(h, drawedges=drawedges)
axi.set_title(f"{drawedges=}")
return fig


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)
locs = "top bottom left right".split()
for loc, labelloc in zip(locs, locs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want all combinations here, not just a zip?

ax.colorbar(h, loc=loc, labelloc=labelloc)