Skip to content

Fix bar tick labels for xarray DataArray with string coordinate#711

Merged
cvanelteren merged 3 commits intoUltraplot:mainfrom
kinyatoride:fix-bar-xarray-string-coord-tick-labels
May 2, 2026
Merged

Fix bar tick labels for xarray DataArray with string coordinate#711
cvanelteren merged 3 commits intoUltraplot:mainfrom
kinyatoride:fix-bar-xarray-string-coord-tick-labels

Conversation

@kinyatoride
Copy link
Copy Markdown
Contributor

Summary

ax.bar(da) on a 1-D xarray.DataArray with a string coordinate renders the full multi-line xarray repr as each x-tick label instead of the underlying string values.

Reproducer

import xarray as xr
import ultraplot as uplt

gm = xr.DataArray(
    [1.0, 2.0, 3.0],
    coords={"ens": ["A", "B", "C"]},
    dims=["ens"],
    name="pr",
    attrs={"long_name": "precipitation", "units": "mm/day"},
)

fig, ax = uplt.subplots(refwidth=4)
ax.bar(gm)
print([t.get_text() for t in ax.get_xticklabels()])

Before: each tick label is "<xarray.DataArray 'ens' ()> Size: 4B\narray('A', dtype='<U1')\nCoordinates:\n ens <U1 4B 'A'" (and similarly for 'B', 'C').

After: ['A', 'B', 'C'].

Cause

In ultraplot/internals/inputs.py::_meta_coords, labels = list(map(str, data)) did not unwrap the xarray.DataArray first. _parse_1d_format passes the coordinate through as a DataArray (not a numpy array); iterating a 1-D DataArray yields scalar DataArrays whose str() is the multi-line repr.

Fix

Wrap data with _to_numpy_array (already defined in the same file) before stringifying, so iteration yields the raw element values for both xarray.DataArray and pandas inputs.

Test

Added test_meta_coords_xarray_string_coord in ultraplot/tests/test_inputs_helpers.py (skipped if xarray is not installed).

Confirmed broken on 2.2.0; fix verified locally with pytest ultraplot/tests/test_inputs_helpers.py and an end-to-end ax.bar(xr.DataArray) smoke check.

Iterating a 1-D xarray.DataArray yields scalar DataArrays whose str()
returns the multi-line repr, which leaked into x-tick labels for
ax.bar(da) when the coordinate was string-valued. Unwrap to a numpy
array via _to_numpy_array before stringifying in _meta_coords.

Adds a regression test in test_inputs_helpers.py.
@cvanelteren
Copy link
Copy Markdown
Collaborator

I will fix the zenodo stuff; it's not related to this PR

…-gh-release major version so the workflow check keeps passing when the action is upgraded. This keeps the assertion focused on the presence of the GitHub release step now that the workflow references v3.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@cvanelteren
Copy link
Copy Markdown
Collaborator

Error will fix itself. Thanks @kinyatoride

@cvanelteren cvanelteren merged commit 27c571d into Ultraplot:main May 2, 2026
15 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants