feat: misc.errorbar can now also process non Obs data and xerr, yerr can

be passed to overwrite the Obs's errors.

Co-authored-by: Simon Kuberski <simon.kuberski@uni-muenster.de>
This commit is contained in:
Fabian Joswig 2023-01-18 17:02:08 +00:00
parent c03e4669a0
commit c475ca53a5
No known key found for this signature in database

View file

@ -4,38 +4,35 @@ import matplotlib.pyplot as plt
from .obs import Obs from .obs import Obs
def errorbar(x, y, axes=plt, *args, **kwargs): def errorbar(x, y, axes=plt, **kwargs):
"""pyerrors wrapper for the errorbars method fo matplotlib """pyerrors wrapper for the errorbars method of matplotlib
Parameters Parameters
---------- ----------
x : list x : list
A list of x-values. It can be a list of Obs objects or int/float. A list of x-values which can be Obs.
y : list y : list
A list of y-values. It should be a list of Obs objects. A list of y-values which can be Obs.
axes : (matplotlib.pyplot.axes) axes : (matplotlib.pyplot.axes)
The axes to plot on. default is plt. The axes to plot on. default is plt.
""" """
if not all(isinstance(o, Obs) for o in y): val = {}
raise Exception("All entries of 'y' have to be Obs") err = {}
for name, comp in zip(["x", "y"], [x, y]):
if all(isinstance(o, Obs) for o in comp):
if not all(hasattr(o, 'e_dvalue') for o in comp):
[o.gamma_method() for o in comp]
val[name] = [o.value for o in comp]
err[name] = [o.dvalue for o in comp]
else:
val[name] = comp
err[name] = None
if all(isinstance(o, Obs) for o in x): if f"{name}err" in kwargs:
if not all(hasattr(o, 'e_dvalue') for o in x): err[name] = kwargs.get(f"{name}err")
[o.gamma_method() for o in x] kwargs.pop(f"{name}err", None)
xval = [o.value for o in x]
xerr = [o.dvalue for o in x]
elif all(isinstance(o, (int, float, np.integer)) for o in x):
xval = x
xerr = None
else:
raise Exception("All entries of 'x' have to be of the same type (int, float or Obs)")
if not all(hasattr(o, 'e_dvalue') for o in y): axes.errorbar(val["x"], val["y"], xerr=err["x"], yerr=err["y"], **kwargs)
[o.gamma_method() for o in y]
yval = [o.value for o in y]
yerr = [o.dvalue for o in y]
axes.errorbar(xval, yval, *args, xerr=xerr, yerr=yerr, **kwargs)
def dump_object(obj, name, **kwargs): def dump_object(obj, name, **kwargs):