Skip to content
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

Fix datetime in colorbar #8831

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,9 @@ def newplotfunc(
if "label" not in cbar_kwargs:
cbar_kwargs["label"] = label_from_attrs(hueplt_norm.data)

if np.issubdtype(hueplt_norm.data.dtype, np.datetime64):
cbar_kwargs["_use_concise_date"] = True

_add_colorbar(
primitive, ax, kwargs.get("cbar_ax", None), cbar_kwargs, cmap_params
)
Expand Down
5 changes: 5 additions & 0 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ def _is_numeric(arr):


def _add_colorbar(primitive, ax, cbar_ax, cbar_kwargs, cmap_params):
_use_concise_data = cbar_kwargs.pop("_use_concise_date", False)

cbar_kwargs.setdefault("extend", cmap_params["extend"])
if cbar_ax is None:
cbar_kwargs.setdefault("ax", ax)
Expand All @@ -723,6 +725,9 @@ def _add_colorbar(primitive, ax, cbar_ax, cbar_kwargs, cmap_params):
fig = ax.get_figure()
cbar = fig.colorbar(primitive, **cbar_kwargs)

if _use_concise_data:
_set_concise_date(ax=cbar.ax, axis="y")

return cbar


Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3407,3 +3407,22 @@
actual = pc.get_offsets().shape[0]

assert expected == actual


@requires_matplotlib
@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
@pytest.mark.parametrize("plotfunc", ["lines", "scatter"])
def test_plot1d_datetime_hue(plotfunc: str) -> None:
time = np.arange(
np.datetime64("2020-01-01"),
np.datetime64("2021-01-01"),
np.timedelta64(1, "D"),
)
data = np.arange(time.size)
darray = xr.DataArray(data=data, dims=("time",), coords={"time": time})

primitive = getattr(darray.plot, plotfunc)(x="time", hue="time")

Check failure on line 3424 in xarray/tests/test_plot.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.11 all-but-dask

test_plot1d_datetime_hue[lines] AttributeError: 'DataArrayPlotAccessor' object has no attribute 'lines'

Check failure on line 3424 in xarray/tests/test_plot.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.9 min-all-deps

test_plot1d_datetime_hue[lines] AttributeError: 'DataArrayPlotAccessor' object has no attribute 'lines'

Check failure on line 3424 in xarray/tests/test_plot.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.12

test_plot1d_datetime_hue[lines] AttributeError: 'DataArrayPlotAccessor' object has no attribute 'lines'. Did you mean: 'line'?

Check failure on line 3424 in xarray/tests/test_plot.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.9

test_plot1d_datetime_hue[lines] AttributeError: 'DataArrayPlotAccessor' object has no attribute 'lines'

Check failure on line 3424 in xarray/tests/test_plot.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12

test_plot1d_datetime_hue[lines] AttributeError: 'DataArrayPlotAccessor' object has no attribute 'lines'. Did you mean: 'line'?

Check failure on line 3424 in xarray/tests/test_plot.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.9

test_plot1d_datetime_hue[lines] AttributeError: 'DataArrayPlotAccessor' object has no attribute 'lines'

# colorbar should view datetime as numerical and use readable concise dates:
number_of_ticks = len(primitive.colorbar.ax.get_ymajorticklabels())
assert number_of_ticks < 30
Loading