Merge pull request #146 from fjosw/feat/errorbar_wrapper

Wrapper for errorbar matplotlib method
This commit is contained in:
Fabian Joswig 2023-01-18 17:53:23 +00:00 committed by GitHub
commit ef23dd256f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View file

@ -1,8 +1,40 @@
import pickle
import numpy as np
import matplotlib.pyplot as plt
from .obs import Obs
def errorbar(x, y, axes=plt, **kwargs):
"""pyerrors wrapper for the errorbars method of matplotlib
Parameters
----------
x : list
A list of x-values which can be Obs.
y : list
A list of y-values which can be Obs.
axes : (matplotlib.pyplot.axes)
The axes to plot on. default is plt.
"""
val = {}
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 f"{name}err" in kwargs:
err[name] = kwargs.get(f"{name}err")
kwargs.pop(f"{name}err", None)
axes.errorbar(val["x"], val["y"], xerr=err["x"], yerr=err["y"], **kwargs)
def dump_object(obj, name, **kwargs):
"""Dump object into pickle file.