Skip to content

Add marginals_plot - #618

Merged
jl-wynen merged 8 commits into
mainfrom
add-marginals-plot
Nov 12, 2026
Merged

jl-wynen merged 8 commits into
mainfrom
add-marginals-plot

Conversation

@jl-wynen

Copy link
Copy Markdown
Member

This is scipp/ess#859 but cleaned up a little and with support for interactive figures.

@jl-wynen
jl-wynen requested a review from nvaytet November 10, 2026 13:57
Comment thread src/plopp/plotting/_marginals.py Outdated
from ..graphics import imagefigure, linefigure


def marginals_plot(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need the _plot suffix? This being a plotting library, maybe it's clear enough with plopp.marginals?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Works for me

Comment thread docs/plotting/marginals-plot.ipynb Outdated
"outputs": [],
"source": [
"da = pp.data.data2d()\n",
"pp.marginals_plot(da)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tried it with bin edges (da = pp.data.data2d(binedges=True)) and it fails with

DimensionError: Cannot add coord 'y' of dims (y: 40) to DataArray with dims (y: 41)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Now that I think of it, bin edges might be an issue for the side plot... 😬

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't see a good solution to this except implementing support for orientation='vertical' in Plopp. I could try this. Or we just raise an error, saying that bin edges are not supported along the vertical dim.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just raise for now, I can try to fix later. Can you make an issue so I don't forget?

Comment thread docs/plotting/index.md

:::{grid-item-card} Marginals plot
:link: marginals-plot.ipynb
:img-bottom: ../_static/plotting/marginals-plot.png

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you also need to add it to the top-level index (currently does not show up there).

Comment thread docs/plotting/marginals.ipynb Resolved
Comment thread src/plopp/plotting/_marginals.py Outdated
cax = divider.append_axes("right", "4%", pad="5%")

main_data_node = data if isinstance(data, Node) else Node(data)
fig = imagefigure(main_data_node, ax=ax_2d, cax=cax, cbar=True, **plot_kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I pass the cbar=False arg if I want to hide it, it clashes with the hard-coded one here.

Comment on lines +35 to +38
np.testing.assert_allclose(top_line.get_xdata(), da.coords[xdim].values)
np.testing.assert_allclose(top_line.get_ydata(), da.sum(ydim).values)
np.testing.assert_allclose(side_line.get_xdata(), da.sum(xdim).values)
np.testing.assert_allclose(side_line.get_ydata(), da.coords[ydim].values)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ideally, we should avoid having matplotlib specifics in these tests (such as get_xdata()).
Instead, we should grab the Plopp artists and verify the values they hold agree with what we expect

However, I don't see a nice way of doing this, as we only return the 2d figure, which is probably the right thing to do. So we can keep it as it is (I know you've said before that maybe we should just commit fully to matplotlib...)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TBH, the tests were generated by Claude. So I rely on you to tell me if they should be changed.

That said, I don't think marginals can work with anything except matplotlib because it uses make_axes_locatable directly.

cax = divider.append_axes("right", "4%", pad="5%") if cbar else None

main_data_node = data if isinstance(data, Node) else Node(data)
fig = imagefigure(main_data_node, ax=ax_2d, cax=cax, cbar=cbar, **plot_kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you're tired of trying to get this working for everything, I can take over.

I just realised that the plot won't work for inputs that are missing coords (and probably other things).
We can either just use plot instead of imagefigure and linefigure as they also accept nodes as inputs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I worked around one issue when building the side data. But plot fails because the coord is unsorted. How would you deal with that? (Feel free to push a change if that is easier)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I pushed an update with a flag to suppress the unsorted coord warning.
Now it also works with raw numpy arrays as inputs.

@nvaytet nvaytet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me now. @jl-wynen Did you want to have one last look? Make sure it still works on your Bifrost data?

@jl-wynen

Copy link
Copy Markdown
Member Author

Looks good to me now. @jl-wynen Did you want to have one last look? Make sure it still works on your Bifrost data?

It works and looks good to me.

@jl-wynen
jl-wynen merged commit 4989e12 into main Nov 12, 2026
5 checks passed
@jl-wynen
jl-wynen deleted the add-marginals-plot branch November 12, 2026 10:56
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