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

Allow plotting categorical data #5464

Merged
merged 18 commits into from
Jun 21, 2021

Conversation

Illviljan
Copy link
Contributor

@Illviljan Illviljan commented Jun 12, 2021

a = xr.DataArray(
    [0, 1, 2],
    dims=("dim_1"),
    coords=dict(dim_1=(["dim_1"], ["u", "v", "w"])),
)
a.plot()

image

a = xr.DataArray(
    [[0, 1, 2], [3, 4, 5]],
    dims=("dim_0", "dim_1"),
    coords=dict(dim_0=(["dim_0"], [0, 1]), dim_1=(["dim_1"], ["u", "v", "w"])),
)
a.plot()

image

@Illviljan
Copy link
Contributor Author

Are there any related tests for this that can be extended?

Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

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

LGTM! This needs a whats-new note though. Thanks @Illviljan

return x[0] - xstep, x[-1] + xstep

# Try to center the pixels:
left, right = _maybe_center_pixels(x)
Copy link
Contributor

Choose a reason for hiding this comment

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

doesn't this contradict the error checking above?

Copy link
Contributor Author

@Illviljan Illviljan Jun 14, 2021

Choose a reason for hiding this comment

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

This makes sure that xarray isn't the one throwing errors before getting to ax.imshow, if the np.issubdtype(v.dtype, str) check hadn't been there.
But it's true it's not really necessary as ax.imshow doesn't seem to support categoricals.

But maybe we prefer matplotlib raising the error?

Copy link
Contributor

Choose a reason for hiding this comment

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

np.issubdtype(v.dtype, str) check hadn't been there.

but it is there :) It's redundant, is it not? Shall we just remove the str check from _maybe_center_pixels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other alternative is to remove the other check and let it crash within matplotlib. But I'm not sure they even intend to support categoricals for these functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think i like that. If matplotlib starts supporting things, then it will just work automatically in xarray. I also liked your refactoring to _maybe_center_pixels

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, then I just need to figure out how to catch the errors in the tests.

@Illviljan
Copy link
Contributor Author

Turns out ax.imshow has some support for strings in the sense that it convert it to integers. So I had to some formatting to get the ticklabels back to strings.

a = xr.DataArray(
    [[0, 1, 2], [3, 4, 5]],
    dims=("dim_0", "dim_1"),
    coords=dict(dim_0=(["dim_0"], [0, 1]), dim_1=(["dim_1"], ["u", "v", "w"])),
)
a.plot.imshow()

image

xarray/tests/test_plot.py Outdated Show resolved Hide resolved
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
@dcherian dcherian added the plan to merge Final call for comments label Jun 17, 2021
@dcherian
Copy link
Contributor

Thanks @Illviljan this is great work!

@dcherian dcherian merged commit b52b29d into pydata:master Jun 21, 2021
dcherian added a commit to dcherian/xarray that referenced this pull request Jun 23, 2021
* main:
  Improve error message for guess engine (pydata#5455)
  Refactor dataset groupby tests (pydata#5506)
  DOC: zarr note on encoding (pydata#5427)
  Allow plotting categorical data (pydata#5464)
@TomNicholas TomNicholas mentioned this pull request Jul 8, 2021
8 tasks
@Illviljan Illviljan deleted the Illviljan-plot_categorical_data branch July 18, 2021 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use matplotlib's categorical axis features
3 participants