Skip to content

Wrapper for errorbar matplotlib method #146

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

Merged
merged 3 commits into from
Jan 18, 2023
Merged

Conversation

fjosw
Copy link
Owner

@fjosw fjosw commented Jan 18, 2023

I added a wrapper around plt.errorbar or Axes.errorbar for use with Obs objects which should save a bit of time everytime one tries to plot and Obs valued list or array.

Usage:

import matplotlib.pyplot as plt
import pyerrors as pe

...

pe.errorbar(x, y, marker="o")
plt.show()

or

fig = plt.figure()
ax1 = fig.add_subplot(111)
pe.errorbar(x, y, marker="o", axes=ax1)
plt.show()

@fjosw fjosw requested a review from s-kuberski January 18, 2023 15:33
@s-kuberski
Copy link
Collaborator

Great idea! This is a very useful feature that will definitely save a lot of typing on my side. You could think of making the routine a bit more flexible and don't require all components to be either of type Obs or not. In the past, I ran into problems when trying to plot the content of a Corr that might have values that are None.
One could use

xval = [o.value if isinstance(o, Obs) else o for o in x]
xerr = [o.dvalue if isinstance(o, Obs) else None for o in x]
if all([o is None for o in xerr]):
    xerr = None

yval = [o.value if isinstance(o, Obs) else o for o in x]
yerr = [o.dvalue if isinstance(o, Obs) else None for o in x]
if all([o is None for o in yerr]):
    yerr = None

or even implement and utilise the short routines:

def obsval(o):
    if isinstance(o, Obs):
        return o.value
    else:
        return o

def obsdval(o):
    if isinstance(o, Obs):
        return o.dvalue
    else:
        return None

that could be useful in other cases (I use the first one to be flexible in some cases).

I think a drop-in replacement of plt.errorbar that can cope with Obs but also works in all other cases would be even more valuable. In this case, the user could provide xerr and yerr and only in case there are not provided, they would be generated from the Obs in x and y.

As a last point, one could get the case to work where y is a Corr, i.e., cast the content to an array first. This is sort of done in Corr.show() but I guess it would not hurt to have it here, as well. I tend to be confused by having something like [o[0] for o in c.content] in my code. I guess this is also sort of implemented in Corr.plottable, so one could just use the routine here, if 'y' is a Corr.

@fjosw
Copy link
Owner Author

fjosw commented Jan 18, 2023

Hi @s-kuberski I started looking into this but I think None entries in the x- and y-errors are incompatible

plt.errorbar([2, None, 2], [3, None, 1], yerr=[0.1, None, 0.1])
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

One could replace None entries with zero but that could be considered unexpected behavior.

Making the code work also with xerr and yerr as arguments sounds like a good idea, I will try to implement this.

@s-kuberski
Copy link
Collaborator

You are right, errorbar() seems to be more restrictive than plot(). The latter can cope with None entries. Sorry for not having a look at this myself before suggesting it.
I agree that setting the errors to zero could be considered undefined behavior. So a mixture of Obs and numbers is most likely not a good idea. A filtering of None entries in y, as done in plottable could be nice, but in the end, the user could also just do this manually, before passing the list to the routine.

fjosw and others added 2 commits January 18, 2023 17:01
be passed to overwrite the Obs's errors.

Co-authored-by: Simon Kuberski <simon.kuberski@uni-muenster.de>
@s-kuberski
Copy link
Collaborator

This looks very nice!

@fjosw fjosw merged commit ef23dd2 into develop Jan 18, 2023
@fjosw fjosw deleted the feat/errorbar_wrapper branch January 18, 2023 17:53
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