# Copyright (C) 2026 Todd Pataky
[docs]
def plot_spm(spm, ax=None, plot_ylabel=True, autoset_xlim=True, autoset_ylim=True, **kwdargs):
'''
Plot an **spm1d** SPM object as a line.
:Parameters:
- *spm* --- **spm1d** SPM object
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *plot_ylabel* --- if *True*, add an ``SPM{t}`` or ``SPM{F}`` y axis label
- *autoset_xlim* --- if True (default), set x axis limits to the domain
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
- *kwdargs* --- **matplotlib.pyplot.plot** keyword arguments
:Returns:
- *h* --- **matplotlib.lines.Line2D** object
:Example:
The t statistic continuum of a paired t test on 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 ]
>>> spm = spm1d.stats.ttest_paired( y0, y1 )
>>> line = spm1d.plot.plot_spm( spm ) # equivalent to line = spm.plot()
>>> line.set_color( 'r' )
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import SPMPlotter
plotter = SPMPlotter(spm, ax=ax)
h = plotter.plot(**kwdargs)
if plot_ylabel:
plotter.plot_ylabel()
if autoset_xlim:
plotter._set_xlim()
if autoset_ylim:
plotter._set_ylim()
return h
[docs]
def plot_spmi(spmi, ax=None, color=None, linestyle='-', marker='', facecolor=None, lw=2, plot_thresh=True,
plot_ylabel=True, thresh_color=None, autoset_xlim=True, autoset_ylim=True, label=None,
with_bars=False, bar_ax_loc=None, bar_color=None, bar_pvalues=False):
'''
Plot an **spm1d** SPM inference object as a line.
:Parameters:
- *spmi* --- **spm1d** SPM inference object
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *color* --- raw SPM line color
- *linestyle* --- raw SPM line style
- *marker* --- raw SPM marker
- *facecolor* --- suprathreshold cluster face color
- *lw* --- raw SPM line width (default 2)
- *plot_thresh* --- if *True*, plot threshold lines
- *plot_ylabel* --- if *True*, add an ``SPM{t}`` or ``SPM{F}`` y axis label
- *thresh_color* --- threshold line color
- *autoset_xlim* --- if True (default), set x axis limits to the domain
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
- *label* --- raw SPM legend label
- *with_bars* --- plot clusters as bars if True
- *bar_ax_loc* --- bar location: None (default), ``'below'`` or ``'above'``
- *bar_color* --- bar color
- *bar_pvalues* --- if True, write cluster p values on bars
:Returns:
- bar axes if *bar_ax_loc* is ``'below'`` or ``'above'``, otherwise *None*
:Example:
Inference results of a paired t test on the plantar arch angle dataset
with cluster bars below the panel.
>>> 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 ]
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> spm1d.plot.plot_spmi( spmi ) # equivalent to spmi.plot()
>>> axb = spm1d.plot.plot_spmi( spmi, ax=plt.figure().gca(), with_bars=True, bar_ax_loc='below' )
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import SPMiPlotter
plotter = SPMiPlotter(spmi, ax=ax)
plotter.plot(color=color, lw=lw, linestyle=linestyle, marker=marker, facecolor=facecolor, label=label, thresh_color=thresh_color, plot_thresh=plot_thresh)
if plot_ylabel:
plotter.plot_ylabel()
if autoset_xlim:
plotter._set_xlim()
if autoset_ylim:
plotter._set_ylim()
if with_bars:
from . _clusters import plot_cluster_barh
if bar_ax_loc not in (None, 'below', 'above'):
from .. exceptions import SPM1DError
raise SPM1DError( 'bar_ax_loc must be None, "below" or "above"' )
return plot_cluster_barh( spmi, ax=plotter.ax, loc=bar_ax_loc, color=bar_color, pvalues=bar_pvalues )
[docs]
def plot_spmi_p_values(spmi, ax=None, size=8, offsets=None, offset_all_clusters=None, autoplace=None, autoset_ylim=True):
'''
Plot SPM inference p values as text (if present).
:Parameters:
- *spmi* --- **spm1d** SPM inference object
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *size* --- font size
- *offsets* --- list of (x,y) offsets relative to cluster centroids
- *offset_all_clusters* --- (x,y) offset for all clusters relative to
cluster centroids
- *autoplace* --- if True, avoid overlapping the SPM curve and other labels
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
:Returns:
- list of **matplotlib.text.Text** objects (one per cluster)
:Example:
Cluster p values placed with an explicit offset from the cluster
centroid.
>>> 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 ]
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> spmi.plot()
>>> labels = spm1d.plot.plot_spmi_p_values( spmi, offsets=[(0, 0.2)] ) # one offset per cluster, equivalent to spmi.plot_p_values( offsets=[(0, 0.2)] )
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import SPMiPlotter
plotter = SPMiPlotter(spmi, ax=ax)
h = plotter.plot_p_values(size, offsets, offset_all_clusters, autoplace=autoplace)
if autoset_ylim:
plotter._set_ylim()
if h and (autoplace or ((autoplace is None) and (offsets is None) and (offset_all_clusters is None) and (len(h) <= plotter._p_autoplace_max))):
# New y limits change the labels' sizes in data units.
for hh, c in zip(h, spmi.clusters):
hh.set_position( c.centroid )
plotter._place_p_values(h)
plotter._set_ylim()
return h
[docs]
def plot_spmi_threshold_label(spmi, ax=None, lower=False, pos=None, autoset_ylim=True, **kwdargs):
'''
Write the critical threshold value on its threshold line.
:Parameters:
- *spmi* --- **spm1d** SPM inference object
- *ax* --- matplotlib.axes object [default: matplotlib.pyplot.gca()]
- *lower* --- if True, label the lower threshold (two-tailed inference)
- *pos* --- text location as a 2-tuple
- *autoset_ylim* --- if True (default), set y limits to show all plotted
elements
- *kwdargs* --- **matplotlib.pyplot.text** keyword arguments
:Returns:
- **matplotlib.text.Text** object
:Example:
Threshold label at a chosen position.
>>> 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 ]
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> spmi.plot()
>>> label = spm1d.plot.plot_spmi_threshold_label( spmi, pos=(50, 3.0) ) # equivalent to spmi.plot_threshold_label( pos=(50, 3.0) )
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _plotters import SPMiPlotter
plotter = SPMiPlotter(spmi, ax=ax)
h = plotter.plot_threshold_label(lower=lower, pos=pos, **kwdargs)
if autoset_ylim:
plotter._set_ylim()
return h