# Copyright (C) 2026 Todd Pataky
from . import _style
[docs]
def plot_ci_0d(ci, ax=None, color='b', color_criterion='r', markersize=10, autoset_ylim=True):
'''
Plot a one-sample (or paired-difference) confidence interval for 0D data.
:Parameters:
- *ci* --- 0D one-sample or difference confidence interval
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *color* --- interval (error bar) color
- *color_criterion* --- datum line (*mu*) color
- *markersize* --- marker size
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
:Returns:
- *None*
:Example:
A one-sample confidence interval for the "morley" dataset from R.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> y = jd.R_Morley().y # (100,) array
>>> ci = spm1d.stats.ci_onesample( y, 0.05 )
>>> spm1d.plot.plot_ci_0d( ci ) # equivalent to ci.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import DataPlotter
plotter = DataPlotter(ax)
plotter.plot_errorbar(ci.mean, ci.hc, color=color, x=0)
if ci.mu is not None:
plotter.plot_datum(ci.mu, color=color_criterion, linestyle='--')
plotter.set_ax_prop(xticks=[])
if autoset_ylim:
plotter._set_ylim()
[docs]
def plot_ci_multisample_0d(ci, ax=None, color='b', color_criterion='r', markersize=10, autoset_ylim=True):
'''
Plot a paired- or two-sample 0D confidence interval (``datum='meanA'``).
:Parameters:
- *ci* --- 0D confidence interval with ``datum='meanA'``
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *color* --- first sample interval (error bar) color
- *color_criterion* --- second sample mean color (marker, error bar and
dashed line)
- *markersize* --- second sample mean marker size
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
:Returns:
- *None*
:Example:
A paired-sample confidence interval centered on the first condition's
mean (``datum='meanA'``) for the "sleep" dataset from R.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.R_SleepPairedTest()
>>> y, A = dataset.y, dataset.x # (20,) arrays of responses and condition labels
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> ci = spm1d.stats.ci_pairedsample( y0, y1, 0.05, datum='meanA' )
>>> spm1d.plot.plot_ci_multisample_0d( ci ) # equivalent to ci.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import DataPlotter
plotter = DataPlotter(ax)
if ci._criterion_type == 'meanB':
hbarw = 0.1
x0,x1 = 0, 2.4*hbarw*ci.hc
plotter.plot_errorbar(ci.meanA, ci.hc, color=color, x=x0, hbarw=hbarw)
plotter.plot(x1, ci.meanB, 'o', color=color_criterion, markersize=markersize)
plotter.plot_datum(ci.meanB, color=color_criterion, linestyle='--')
elif ci._criterion_type == 'tailB':
hbarw = 0.1
x0,x1 = 0, 1.2*hbarw*ci.hc
plotter.plot_errorbar(ci.meanA, 0.5*ci.hc, color=color, x=x0, hbarw=hbarw)
plotter.plot_errorbar(ci.meanB, 0.5*ci.hc, color=color_criterion, x=x1, hbarw=hbarw)
y = (ci.meanB - 0.5*ci.hc) if ( ci.meanB > ci.meanA ) else (ci.meanB + 0.5*ci.hc)
plotter.plot_datum(y, color=color_criterion, linestyle='--')
if autoset_ylim:
plotter._set_ylim()
[docs]
def plot_ci(ci, ax=None, x=None, linecolor=None, facecolor=None, edgecolor=None, alpha=0.5, autoset_ylim=True):
'''
Plot a one-sample or difference 1D confidence region.
:Parameters:
- *ci* --- 1D one-sample or difference confidence interval
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *x* --- x positions [default: np.arange(Q)]
- *linecolor* --- mean continuum line color
- *facecolor* --- confidence cloud face color
- *edgecolor* --- confidence cloud edge color
- *alpha* --- confidence cloud face alpha
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
:Returns:
- *None*
:Example:
A one-sample confidence region for one condition of the plantar arch
angle dataset.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.PlantarArchAngle()
>>> y, A = dataset.y, dataset.x # (20,101) array and (20,) array
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> ci = spm1d.stats.ci_onesample( y0, 0.05 )
>>> spm1d.plot.plot_ci( ci ) # equivalent to ci.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import DataPlotter
y,h,Q = ci.mean, ci.hc, ci.Q
plotter = DataPlotter(ax)
plotter._set_x(x, Q)
plotter.plot(y, color=_style.ink(linecolor), lw=3)
plotter.plot_cloud([y+h, y-h], facecolor, edgecolor, alpha)
if ci.mu is not None:
if ci._isscalarmu:
plotter.plot_datum(y=ci.mu, color='r', linestyle='--')
else:
plotter.plot(ci.mu, color='r', linestyle='--')
if autoset_ylim:
plotter._set_ylim()
plotter._set_xlim()
[docs]
def plot_ci_multisample(ci, ax=None, x=None, linecolors=('k','b'), facecolors=('0.8','b'), edgecolors=('0.8','b'), color_criterion='r', alphas=(0.5,0.5), autoset_ylim=True):
'''
Plot a paired- or two-sample 1D confidence region (``datum='meanA'``).
:Parameters:
- *ci* --- 1D confidence interval with ``datum='meanA'``
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *x* --- x positions [default: np.arange(Q)]
- *linecolors* --- mean continuum line colors as a 2-tuple (or one color
for both)
- *facecolors* --- cloud face colors as a 2-tuple (or one color for both)
- *edgecolors* --- cloud edge colors as a 2-tuple (or one color for both)
- *color_criterion* --- second sample mean criterion color (dashed line)
- *alphas* --- cloud face alphas as a 2-tuple (or one value for both)
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
:Returns:
- *None*
:Example:
A paired-sample confidence region centered on the first condition's
mean (``datum='meanA'``) for the plantar arch angle dataset.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.PlantarArchAngle()
>>> y, A = dataset.y, dataset.x # (20,101) array and (20,) array
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> ci = spm1d.stats.ci_pairedsample( y0, y1, 0.05, datum='meanA' )
>>> spm1d.plot.plot_ci_multisample( ci ) # equivalent to ci.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import DataPlotter
mA,mB,h = ci.meanA, ci.meanB, ci.hc
plotter = DataPlotter(ax)
plotter._set_x(x, ci.Q)
linecolors = linecolors if isinstance(linecolors, (tuple,list)) else [linecolors]*2
facecolors = facecolors if isinstance(facecolors, (tuple,list)) else [facecolors]*2
edgecolors = edgecolors if isinstance(edgecolors, (tuple,list)) else [edgecolors]*2
alphas = alphas if isinstance(alphas, (tuple,list)) else [alphas]*2
if ci._criterion_type == 'meanB':
plotter.plot(mA, color=linecolors[0], lw=3)
plotter.plot_cloud([mA+h, mA-h], facecolors[0], edgecolors[0], alphas[0])
plotter.plot(mB, color=color_criterion, linestyle='--')
elif ci._criterion_type == 'tailB':
plotter.plot(mA, color=linecolors[0], lw=3)
plotter.plot(mB, color=linecolors[1], lw=3)
plotter.plot_cloud([mA+0.5*h, mA-0.5*h], facecolors[0], edgecolors[0], alphas[0], edgelinestyle='--')
plotter.plot_cloud([mB+0.5*h, mB-0.5*h], facecolors[1], edgecolors[1], alphas[1], edgelinestyle='--')
if autoset_ylim:
plotter._set_ylim()
plotter._set_xlim()