Skip to content

Add 'band' mode for errorbars#581

Merged
nvaytet merged 12 commits into
mainfrom
band-errorbars
Jun 25, 2026
Merged

Add 'band' mode for errorbars#581
nvaytet merged 12 commits into
mainfrom
band-errorbars

Conversation

@nvaytet

@nvaytet nvaytet commented Jun 24, 2026

Copy link
Copy Markdown
Member

We expand the scope of the errorbars argument from being just True/False to also accepting a string to control the mode for representing errorbars on 1d plots. This can now be a 'band' which plots as:
Figure 1 (80)

Example:

import plopp as pp

da = pp.data.data1d(variances=True)
da.plot(errorbars='band')

Fixes #580

@nvaytet nvaytet marked this pull request as ready for review June 24, 2026 15:07
else:
raise ValueError(f"Invalid errorbar mode: {mode}")

def update(self, x: np.ndarray, y: np.ndarray, e: np.ndarray, hist: bool) -> None:

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.

There is some repeated code in this function and __init__. Can you extract it?

Comment thread src/plopp/backends/matplotlib/line.py Outdated
self._artist.remove()
self._artist = self._ax.fill_between(
x, yme, ype, color=color, alpha=alpha, zorder=zorder, **extra_arg
)

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 guess you checked and this artist cannot be modified in place?

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.

You can but it gets annoying when you have to handle both the line and step case. In the line case, the vertices are made of len(x)*2 + 3 points (don't ask why +3) but in the step case it's len(x)*4 + 3.
To replace the vertices you have to use a mix of ordered points and points in reverse order, and with the step you have to insert some np.repeat in there as well.

I decided for the sake of simplicity to just remove and re-create. Performance seems fine on what I tested (although I could have tested on larger data; i will do that now just to make sure).

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.

Turns out it is noticeably slower with larger data 😞. I will try to update in-place.

Comment thread src/plopp/backends/common.py Outdated
if data.variances is not None:
error = {
'x': np.asarray(sc.midpoints(x).values) if hist else xvalues,
'x': xvalues, # np.asarray(sc.midpoints(x).values) if hist else xvalues,

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.

Should this commented code be removed?

xverts = np.concatenate([x[0:1], x2, x2[::-1][1:], x[0:1]])
a = np.repeat(yme, 2)
b = np.repeat(ype, 2)[::-1]
yverts = np.concatenate([[b[-1]], a[0:1], a, b[0:1], b, b[-2:]])

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.

This does indeed look complicated. But it seems to make sense.

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 hope the interface/number of vertices doesn't randomly change when they release a new matplotlib...

@nvaytet nvaytet merged commit da72a8f into main Jun 25, 2026
5 checks passed
@nvaytet nvaytet deleted the band-errorbars branch June 25, 2026 12:40
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.

Add a 'band' mode for displaying errorbars?

2 participants