Skip to content

Add DataArray.epoch.plot() accessor to fix default axes for 2D data when plotting with xarray#107

Merged
JoelLucaAdams merged 5 commits intomainfrom
patch_xarray_2d_plotting_defaults_axes
Mar 18, 2026
Merged

Add DataArray.epoch.plot() accessor to fix default axes for 2D data when plotting with xarray#107
JoelLucaAdams merged 5 commits intomainfrom
patch_xarray_2d_plotting_defaults_axes

Conversation

@JoelLucaAdams
Copy link
Collaborator

@JoelLucaAdams JoelLucaAdams commented Mar 13, 2026

Fed up with xarray plotting the axes in reverse and it's going to be a pain point during the EPOCH workshop so this is my attempt to address the issue by developing our own plotting accessor that inherits the default behaviour of the xarray.plot method while also passing the correct axes for 2D data

@LiamPattinson
Copy link
Collaborator

I'm not sure about modifying default Xarray behaviour, as this could result in unwanted surprises for users. I'd rather implement our own plotting functions under the epoch accessor that correct the axis order.

@LiamPattinson
Copy link
Collaborator

I think this is looking good. Is it ready for review? The PR name should also probably be changed.

@JoelLucaAdams JoelLucaAdams changed the title monkey patch xarray plotting to fix default axes for 2D data Add DataArray.epoch.plot() accessor to fix default axes for 2D data when plotting with xarray Mar 16, 2026
@JoelLucaAdams
Copy link
Collaborator Author

Yep, all ready for review, cheers :)

Copy link
Collaborator

@LiamPattinson LiamPattinson left a comment

Choose a reason for hiding this comment

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

Looks good!

I have some concerns about calling plt.subplots() in the tests and then plt.close at the end, as if multiple tests fail then a lot of matplotlib figures could accumulate in the background. One solution might be to use yield fixtures:

@pytest.fixture
def subplots():
    # You could get fancy and pass rows/cols to the fixture
    # Let's keep it easy here...
    fig, ax = plt.subplots()
    yield (fig, ax)
    plt.close(fig)

def test_something(subplots)
    fix, ax = subplots
    # ... do the test ...

I haven't tested this myself though, so it might take some experimentation.

@JoelLucaAdams
Copy link
Collaborator Author

That's a really good point, and your code worked on the first try. Thanks!

@JoelLucaAdams JoelLucaAdams merged commit c439fb4 into main Mar 18, 2026
16 checks passed
@JoelLucaAdams JoelLucaAdams deleted the patch_xarray_2d_plotting_defaults_axes branch March 18, 2026 12:41
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