mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-03-15 06:40:24 +01:00
feat: Added wrapper for errorbar matplotlib method for more convenient
plotting.
This commit is contained in:
parent
88fd37b241
commit
6ed357b51d
2 changed files with 52 additions and 0 deletions
|
@ -1,8 +1,43 @@
|
|||
import pickle
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from .obs import Obs
|
||||
|
||||
|
||||
def errorbar(x, y, axes=plt, *args, **kwargs):
|
||||
"""pyerrors wrapper for the errorbars method fo matplotlib
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : list
|
||||
A list of x-values. It can be a list of Obs objects or int/float.
|
||||
y : list
|
||||
A list of y-values. It should be a list of Obs objects.
|
||||
axes : (matplotlib.pyplot.axes)
|
||||
The axes to plot on. default is plt.
|
||||
"""
|
||||
if not all(isinstance(o, Obs) for o in y):
|
||||
raise Exception("All entries of 'y' have to be Obs")
|
||||
|
||||
if all(isinstance(o, Obs) for o in x):
|
||||
if not all(hasattr(o, 'e_dvalue') for o in x):
|
||||
[o.gamma_method() for o in x]
|
||||
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):
|
||||
[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):
|
||||
"""Dump object into pickle file.
|
||||
|
||||
|
|
17
tests/misc_test.py
Normal file
17
tests/misc_test.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pyerrors as pe
|
||||
import pytest
|
||||
|
||||
|
||||
def test_obs_errorbar():
|
||||
x_float = np.arange(5)
|
||||
x_obs = []
|
||||
y_obs = []
|
||||
for x in x_float:
|
||||
x_obs.append(pe.pseudo_Obs(x, 0.1, "test"))
|
||||
y_obs.append(pe.pseudo_Obs(x ** 2, 0.1, "test"))
|
||||
|
||||
pe.errorbar(x_float, y_obs, marker="x", ms=2)
|
||||
pe.errorbar(x_obs, y_obs, marker="x", ms=2)
|
||||
|
Loading…
Add table
Reference in a new issue