|
|
@ -62,12 +62,11 @@ class PlotData(object):
|
|
|
|
>>> h1 = d2()
|
|
|
|
>>> h1 = d2()
|
|
|
|
|
|
|
|
|
|
|
|
# Plot with confidence interval
|
|
|
|
# Plot with confidence interval
|
|
|
|
>>> d3 = PlotData(np.sin(x), x)
|
|
|
|
>>> ci = PlotData(np.vstack([np.sin(x)*0.9, np.sin(x)*1.2]).T, x,
|
|
|
|
>>> d3.children = [PlotData(np.vstack([np.sin(x)*0.9,
|
|
|
|
... plot_args=[':r'])
|
|
|
|
... np.sin(x)*1.2]).T, x)]
|
|
|
|
>>> d3 = PlotData(np.sin(x), x, children=[ci])
|
|
|
|
>>> d3.plot_args_children = [':r']
|
|
|
|
>>> h = d3.plot() # plot data, CI red dotted line
|
|
|
|
|
|
|
|
>>> h = d3.plot(plot_args_children=['b--']) # CI with blue dashed line
|
|
|
|
>>> h = d3.plot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
@ -165,8 +164,9 @@ class PlotData(object):
|
|
|
|
def integrate(self, a=None, b=None, **kwds):
|
|
|
|
def integrate(self, a=None, b=None, **kwds):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
>>> x = np.linspace(0,5,60)
|
|
|
|
>>> x = np.linspace(0,5,60)
|
|
|
|
>>> d = PlotData(np.sin(x), x)
|
|
|
|
>>> y = np.sin(x)
|
|
|
|
>>> d.dataCI = np.vstack((d.data*.9,d.data*1.1)).T
|
|
|
|
>>> ci = PlotData(np.vstack((y*.9, y*1.1)).T, x)
|
|
|
|
|
|
|
|
>>> d = PlotData(y, x, children=[ci])
|
|
|
|
>>> d.integrate(0, np.pi/2, return_ci=True)
|
|
|
|
>>> d.integrate(0, np.pi/2, return_ci=True)
|
|
|
|
array([ 0.99940055, 0.85543644, 1.04553343])
|
|
|
|
array([ 0.99940055, 0.85543644, 1.04553343])
|
|
|
|
>>> np.allclose(d.integrate(0, 5, return_ci=True),
|
|
|
|
>>> np.allclose(d.integrate(0, 5, return_ci=True),
|
|
|
@ -187,12 +187,18 @@ class PlotData(object):
|
|
|
|
b = x[-1]
|
|
|
|
b = x[-1]
|
|
|
|
ix = np.flatnonzero((a < x) & (x < b))
|
|
|
|
ix = np.flatnonzero((a < x) & (x < b))
|
|
|
|
xi = np.hstack((a, x.take(ix), b))
|
|
|
|
xi = np.hstack((a, x.take(ix), b))
|
|
|
|
|
|
|
|
if self.data.ndim > 1:
|
|
|
|
|
|
|
|
fi = np.vstack((self.eval_points(a),
|
|
|
|
|
|
|
|
self.data[ix, :],
|
|
|
|
|
|
|
|
self.eval_points(b))).T
|
|
|
|
|
|
|
|
else:
|
|
|
|
fi = np.hstack((self.eval_points(a), self.data.take(ix),
|
|
|
|
fi = np.hstack((self.eval_points(a), self.data.take(ix),
|
|
|
|
self.eval_points(b)))
|
|
|
|
self.eval_points(b)))
|
|
|
|
res = fun(fi, xi, **kwds)
|
|
|
|
res = fun(fi, xi, **kwds)
|
|
|
|
if return_ci:
|
|
|
|
if return_ci:
|
|
|
|
return np.hstack(
|
|
|
|
res_ci = [child.integrate(a, b, method=method)
|
|
|
|
(res, fun(self.dataCI[ix, :].T, xi[1:-1], **kwds)))
|
|
|
|
for child in self.children]
|
|
|
|
|
|
|
|
return np.hstack((res, res_ci))
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
def plot(self, *args, **kwds):
|
|
|
|
def plot(self, *args, **kwds):
|
|
|
@ -205,15 +211,13 @@ class PlotData(object):
|
|
|
|
if not plotflag and self.children is not None:
|
|
|
|
if not plotflag and self.children is not None:
|
|
|
|
axis.hold('on')
|
|
|
|
axis.hold('on')
|
|
|
|
tmp = []
|
|
|
|
tmp = []
|
|
|
|
child_args = kwds.pop(
|
|
|
|
child_args = kwds.pop('plot_args_children',
|
|
|
|
'plot_args_children',
|
|
|
|
tuple(self.plot_args_children))
|
|
|
|
tuple(
|
|
|
|
|
|
|
|
self.plot_args_children))
|
|
|
|
|
|
|
|
child_kwds = dict(self.plot_kwds_children).copy()
|
|
|
|
child_kwds = dict(self.plot_kwds_children).copy()
|
|
|
|
child_kwds.update(kwds.pop('plot_kwds_children', {}))
|
|
|
|
child_kwds.update(kwds.pop('plot_kwds_children', {}))
|
|
|
|
child_kwds['axis'] = axis
|
|
|
|
child_kwds['axis'] = axis
|
|
|
|
for child in self.children:
|
|
|
|
for child in self.children:
|
|
|
|
tmp1 = child(*child_args, **child_kwds)
|
|
|
|
tmp1 = child.plot(*child_args, **child_kwds)
|
|
|
|
if tmp1 is not None:
|
|
|
|
if tmp1 is not None:
|
|
|
|
tmp.append(tmp1)
|
|
|
|
tmp.append(tmp1)
|
|
|
|
if len(tmp) == 0:
|
|
|
|
if len(tmp) == 0:
|
|
|
@ -267,8 +271,8 @@ class AxisLabels:
|
|
|
|
return self.__str__()
|
|
|
|
return self.__str__()
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
def __str__(self):
|
|
|
|
return '%s\n%s\n%s\n%s\n' % (
|
|
|
|
txt = 'AxisLabels(title={}, xlab={}, ylab={}, zlab={})'
|
|
|
|
self.title, self.xlab, self.ylab, self.zlab)
|
|
|
|
return txt.format(self.title, self.xlab, self.ylab, self.zlab)
|
|
|
|
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
def copy(self):
|
|
|
|
newcopy = empty_copy(self)
|
|
|
|
newcopy = empty_copy(self)
|
|
|
@ -318,11 +322,6 @@ class Plotter_1d(object):
|
|
|
|
if plotmethod is None:
|
|
|
|
if plotmethod is None:
|
|
|
|
plotmethod = 'plot'
|
|
|
|
plotmethod = 'plot'
|
|
|
|
self.plotmethod = plotmethod
|
|
|
|
self.plotmethod = plotmethod
|
|
|
|
# self.plotbackend = plotbackend
|
|
|
|
|
|
|
|
# try:
|
|
|
|
|
|
|
|
# self.plotfun = getattr(plotbackend, plotmethod)
|
|
|
|
|
|
|
|
# except:
|
|
|
|
|
|
|
|
# pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def show(self, *args, **kwds):
|
|
|
|
def show(self, *args, **kwds):
|
|
|
|
plt.show(*args, **kwds)
|
|
|
|
plt.show(*args, **kwds)
|
|
|
@ -350,72 +349,65 @@ class Plotter_1d(object):
|
|
|
|
|
|
|
|
|
|
|
|
def _plot(self, axis, plotflag, wdata, *args, **kwds):
|
|
|
|
def _plot(self, axis, plotflag, wdata, *args, **kwds):
|
|
|
|
x = wdata.args
|
|
|
|
x = wdata.args
|
|
|
|
data = transformdata(x, wdata.data, plotflag)
|
|
|
|
data = transformdata_1d(x, wdata.data, plotflag)
|
|
|
|
dataCI = getattr(wdata, 'dataCI', ())
|
|
|
|
dataCI = getattr(wdata, 'dataCI', ())
|
|
|
|
|
|
|
|
if dataCI:
|
|
|
|
|
|
|
|
dataCI = transformdata_1d(x, dataCI, plotflag)
|
|
|
|
h1 = plot1d(axis, x, data, dataCI, plotflag, *args, **kwds)
|
|
|
|
h1 = plot1d(axis, x, data, dataCI, plotflag, *args, **kwds)
|
|
|
|
return h1
|
|
|
|
return h1
|
|
|
|
__call__ = plot
|
|
|
|
__call__ = plot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plot1d(axis, args, data, dataCI, plotflag, *varargin, **kwds):
|
|
|
|
def set_plot_scale(axis, f_max, plotflag):
|
|
|
|
|
|
|
|
|
|
|
|
plottype = np.mod(plotflag, 10)
|
|
|
|
|
|
|
|
if plottype == 0: # No plotting
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
elif plottype == 1:
|
|
|
|
|
|
|
|
H = axis.plot(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 2:
|
|
|
|
|
|
|
|
H = axis.step(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 3:
|
|
|
|
|
|
|
|
H = axis.stem(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 4:
|
|
|
|
|
|
|
|
H = axis.errorbar(args, data,
|
|
|
|
|
|
|
|
yerr=[dataCI[:, 0] - data,
|
|
|
|
|
|
|
|
dataCI[:, 1] - data],
|
|
|
|
|
|
|
|
*varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 5:
|
|
|
|
|
|
|
|
H = axis.bar(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 6:
|
|
|
|
|
|
|
|
level = 0
|
|
|
|
|
|
|
|
if np.isfinite(level):
|
|
|
|
|
|
|
|
H = axis.fill_between(args, data, level, *varargin, **kwds)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
H = axis.fill_between(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 7:
|
|
|
|
|
|
|
|
H = axis.plot(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
H = axis.fill_between(args, dataCI[:, 0], dataCI[:, 1],
|
|
|
|
|
|
|
|
alpha=0.2, color='r')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scale = plotscale(plotflag)
|
|
|
|
scale = plotscale(plotflag)
|
|
|
|
logXscale = 'x' in scale
|
|
|
|
log_x_scale = 'x' in scale
|
|
|
|
logYscale = 'y' in scale
|
|
|
|
log_y_scale = 'y' in scale
|
|
|
|
logZscale = 'z' in scale
|
|
|
|
log_z_scale = 'z' in scale
|
|
|
|
|
|
|
|
if log_x_scale:
|
|
|
|
if logXscale:
|
|
|
|
|
|
|
|
axis.set(xscale='log')
|
|
|
|
axis.set(xscale='log')
|
|
|
|
if logYscale:
|
|
|
|
if log_y_scale:
|
|
|
|
axis.set(yscale='log')
|
|
|
|
axis.set(yscale='log')
|
|
|
|
if logZscale:
|
|
|
|
if log_z_scale:
|
|
|
|
axis.set(zscale='log')
|
|
|
|
axis.set(zscale='log')
|
|
|
|
|
|
|
|
trans_flag = np.mod(plotflag // 10, 10)
|
|
|
|
transFlag = np.mod(plotflag // 10, 10)
|
|
|
|
log_scale = log_x_scale or log_y_scale or log_z_scale
|
|
|
|
logScale = logXscale or logYscale or logZscale
|
|
|
|
if log_scale or (trans_flag == 5 and not log_scale):
|
|
|
|
if logScale or (transFlag == 5 and not logScale):
|
|
|
|
|
|
|
|
ax = list(axis.axis())
|
|
|
|
ax = list(axis.axis())
|
|
|
|
fmax1 = data.max()
|
|
|
|
if trans_flag == 8 and not log_scale:
|
|
|
|
if transFlag == 5 and not logScale:
|
|
|
|
ax[3] = 11 * np.log10(f_max)
|
|
|
|
ax[3] = 11 * np.log10(fmax1)
|
|
|
|
|
|
|
|
ax[2] = ax[3] - 40
|
|
|
|
ax[2] = ax[3] - 40
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
ax[3] = 1.15 * fmax1
|
|
|
|
ax[3] = 1.15 * f_max
|
|
|
|
ax[2] = ax[3] * 1e-4
|
|
|
|
ax[2] = ax[3] * 1e-4
|
|
|
|
|
|
|
|
|
|
|
|
axis.axis(ax)
|
|
|
|
axis.axis(ax)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plot1d(axis, args, data, dataCI, plotflag, *varargin, **kwds):
|
|
|
|
|
|
|
|
h = []
|
|
|
|
|
|
|
|
plottype = np.mod(plotflag, 10)
|
|
|
|
|
|
|
|
if plottype == 0: # No plotting
|
|
|
|
|
|
|
|
return h
|
|
|
|
|
|
|
|
fun = {1: 'plot', 2: 'step', 3: 'stem', 5: 'bar'}.get(plottype)
|
|
|
|
|
|
|
|
if fun is not None:
|
|
|
|
|
|
|
|
plotfun = getattr(axis, fun)
|
|
|
|
|
|
|
|
h.extend(plotfun(args, data, *varargin, **kwds))
|
|
|
|
if np.any(dataCI) and plottype < 3:
|
|
|
|
if np.any(dataCI) and plottype < 3:
|
|
|
|
axis.hold(True)
|
|
|
|
axis.hold(True)
|
|
|
|
plot1d(axis, args, dataCI, (), plotflag, 'r--')
|
|
|
|
h.extend(plotfun(args, dataCI, 'r--'))
|
|
|
|
return H
|
|
|
|
elif plottype == 4:
|
|
|
|
|
|
|
|
h = axis.errorbar(args, data,
|
|
|
|
|
|
|
|
yerr=[dataCI[:, 0] - data,
|
|
|
|
|
|
|
|
dataCI[:, 1] - data],
|
|
|
|
|
|
|
|
*varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 6:
|
|
|
|
|
|
|
|
h = axis.fill_between(args, data, 0, *varargin, **kwds)
|
|
|
|
|
|
|
|
elif plottype == 7:
|
|
|
|
|
|
|
|
h = axis.plot(args, data, *varargin, **kwds)
|
|
|
|
|
|
|
|
h.extend(axis.fill_between(args, dataCI[:, 0], dataCI[:, 1],
|
|
|
|
|
|
|
|
alpha=0.2, color='r'))
|
|
|
|
|
|
|
|
fmax1 = data.max()
|
|
|
|
|
|
|
|
set_plot_scale(axis, fmax1, plotflag)
|
|
|
|
|
|
|
|
return h
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plotscale(plotflag):
|
|
|
|
def plotscale(plotflag):
|
|
|
@ -462,6 +454,10 @@ def plotscale(plotflag):
|
|
|
|
'ylog'
|
|
|
|
'ylog'
|
|
|
|
>>> plotscale(10000)
|
|
|
|
>>> plotscale(10000)
|
|
|
|
'zlog'
|
|
|
|
'zlog'
|
|
|
|
|
|
|
|
>>> plotscale(1100)
|
|
|
|
|
|
|
|
'xylog'
|
|
|
|
|
|
|
|
>>> plotscale(11100)
|
|
|
|
|
|
|
|
'xyzlog'
|
|
|
|
|
|
|
|
|
|
|
|
See also
|
|
|
|
See also
|
|
|
|
---------
|
|
|
|
---------
|
|
|
@ -480,25 +476,41 @@ def plotscale(plotflag):
|
|
|
|
return scales[scaleId]
|
|
|
|
return scales[scaleId]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def transformdata(x, f, plotflag):
|
|
|
|
def plotflag2plottype_1d(plotflag):
|
|
|
|
transFlag = np.mod(plotflag // 10, 10)
|
|
|
|
plottype = np.mod(plotflag, 10)
|
|
|
|
if transFlag == 0:
|
|
|
|
return ['', 'plot', 'step', 'stem', 'errorbar', 'bar'][plottype]
|
|
|
|
data = f
|
|
|
|
|
|
|
|
elif transFlag == 1:
|
|
|
|
|
|
|
|
data = 1 - f
|
|
|
|
def plotflag2transform_id(plotflag):
|
|
|
|
elif transFlag == 2:
|
|
|
|
transform_id = np.mod(plotflag // 10, 10)
|
|
|
|
data = cumtrapz(f, x)
|
|
|
|
return ['f', '1-f',
|
|
|
|
elif transFlag == 3:
|
|
|
|
'cumtrapz(f)', '1-cumtrapz(f)',
|
|
|
|
data = 1 - cumtrapz(f, x)
|
|
|
|
'log(f)', 'log(1-f)'
|
|
|
|
if transFlag in (4, 5):
|
|
|
|
'log(cumtrapz(f))', 'log(cumtrapz(f))',
|
|
|
|
if transFlag == 4:
|
|
|
|
'log10(f)'][transform_id]
|
|
|
|
data = -np.log1p(-cumtrapz(f, x))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if any(f < 0):
|
|
|
|
def transform_id2plotflag2(transform_id):
|
|
|
|
raise ValueError('Invalid plotflag: Data or dataCI is ' +
|
|
|
|
return {'': 0, 'None': 0, 'f': 0, '1-f': 1,
|
|
|
|
'negative, but must be positive')
|
|
|
|
'cumtrapz(f)': 2, '1-cumtrapz(f)': 3,
|
|
|
|
data = 10 * np.log10(f)
|
|
|
|
'log(f)': 4, 'log(1-f)': 5,
|
|
|
|
return data
|
|
|
|
'log(cumtrapz(f))': 6, 'log(1-cumtrapz(f))': 7,
|
|
|
|
|
|
|
|
'10log10(f)': 8}[transform_id] * 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def transformdata_1d(x, f, plotflag):
|
|
|
|
|
|
|
|
transform_id = np.mod(plotflag // 10, 10)
|
|
|
|
|
|
|
|
transform = [lambda f, x: f,
|
|
|
|
|
|
|
|
lambda f, x: 1 - f,
|
|
|
|
|
|
|
|
lambda f, x: cumtrapz(f, x),
|
|
|
|
|
|
|
|
lambda f, x: 1 - cumtrapz(f, x),
|
|
|
|
|
|
|
|
lambda f, x: np.log(f),
|
|
|
|
|
|
|
|
lambda f, x: np.log1p(-f),
|
|
|
|
|
|
|
|
lambda f, x: np.log(cumtrapz(f, x)),
|
|
|
|
|
|
|
|
lambda f, x: np.log1p(-cumtrapz(f, x)),
|
|
|
|
|
|
|
|
lambda f, x: 10*np.log10(f)
|
|
|
|
|
|
|
|
][transform_id]
|
|
|
|
|
|
|
|
return transform(f, x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Plotter_2d(Plotter_1d):
|
|
|
|
class Plotter_2d(Plotter_1d):
|
|
|
@ -589,14 +601,15 @@ def test_plotdata():
|
|
|
|
x = np.linspace(0, np.pi, 9)
|
|
|
|
x = np.linspace(0, np.pi, 9)
|
|
|
|
xi = np.linspace(0, np.pi, 4*9)
|
|
|
|
xi = np.linspace(0, np.pi, 4*9)
|
|
|
|
|
|
|
|
|
|
|
|
d = PlotData(np.sin(x)/2, x, xlab='x', ylab='sin', title='sinus',
|
|
|
|
d = PlotData(np.sin(x)/2, x, dataCI=[], xlab='x', ylab='sin', title='sinus',
|
|
|
|
plot_args=['r.'])
|
|
|
|
plot_args=['r.'])
|
|
|
|
di = PlotData(d.eval_points(xi, method='cubic'), xi)
|
|
|
|
di = PlotData(d.eval_points(xi, method='cubic'), xi)
|
|
|
|
unused_hi = di.plot()
|
|
|
|
unused_hi = di.plot()
|
|
|
|
unused_h = d.plot()
|
|
|
|
unused_h = d.plot()
|
|
|
|
f = di.to_cdf()
|
|
|
|
f = di.to_cdf()
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(4):
|
|
|
|
for i in range(4):
|
|
|
|
_ = f.plot(plotflag=i)
|
|
|
|
_ = di.plot(plotflag=i)
|
|
|
|
d.show('hold')
|
|
|
|
d.show('hold')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -607,5 +620,5 @@ def test_docstrings():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_docstrings()
|
|
|
|
# test_docstrings()
|
|
|
|
# test_plotdata()
|
|
|
|
test_plotdata()
|
|
|
|