behav_utils.plotting#

behav_utils.plotting — draw-only plotters.

Every plotter takes a typed result from the matching compute_x function and computes nothing; every one is single-panel and takes ax:

compute_psychometric_curve(arrays)             → plot_psychometric_curve(curve, ax=)
compute_update_matrix(arrays)                  → plot_update_matrix(um, ax=)
compute_conditional_psychometric(arrays)       → plot_conditional_psychometric(cp, ax=)
compute_binned_curve(arrays)                   → plot_binned_curve(bc, ax=)
compute_sd_profile(arrays)                     → plot_sd_profile(sd, ax=)
compute_phase_stats(sessions, per_session=True)→ plot_trajectory(result, stat, ax=)
compute_rolling_stats(sessions, names)         → plot_rolling_stats(result, stat, ax=)
compute_delta_stat({...})                      → plot_psychometric_comparison(d, ax=) / plot_stat_comparison(d, stat, ax=)
compute_interaction(r1, r2, key)               → plot_interaction(ix, stat, ax=)

Layout is the caller’s: pass ax. Overlay by calling the same plotter twice on one axes with different colours. The only exception: plot_stat_comparison_grid and plot_interaction_grid build a grid of axes and call the single-panel plotter once per stat.

behav_utils.plotting.plot_response_curve(curve, ax=None, *, color=None, label=None, show_identity=True, title='')[source]#

Mean response per stimulus bin (± SEM), with the identity line a faithful reproducer would follow.

Parameters:
  • curve (ResponseCurve) – From compute_response_curve.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • show_identity (bool) – Draw the identity line.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_error_distribution(dist, ax=None, *, color=None, label=None, title='')[source]#

Density of signed errors (response − truth), with zero marked.

Parameters:
  • dist (ErrorDistribution) – From compute_error_distribution.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_serial_bias(curve, ax=None, *, color=None, label=None, title='')[source]#

Mean error (current stimulus’s effect removed) per bin of previous − current stimulus, ± SEM.

Parameters:
  • curve (SerialBiasCurve) – From compute_serial_bias.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_psychometric_curve(curve, ax=None, *, color=None, label=None, alpha=0.7, linewidth=1.5, linestyle='-', show_data=True, show_band=True, show_params=False, show_lapse=False, show_reference=True, title='')[source]#

Fitted curve, binned data points, and the bootstrap band if present.

The curve is drawn only when the fit succeeded; the points are drawn either way.

Parameters:
  • curve (PsychometricCurve) – From compute_psychometric_curve.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • alpha (float) – Opacity of the curve.

  • linewidth (float) – Width of the curve.

  • linestyle (str) – Line style of the curve.

  • show_data (bool) – Draw the binned proportions as points.

  • show_band (bool) – Draw the bootstrap band, when the curve has one.

  • show_params (bool) – Add the PSE and sigma to the legend label.

  • show_lapse (bool) – Draw the two lapse asymptotes as dotted lines.

  • show_reference (bool) – Draw dashed lines at P = 0.5 and at the category boundary.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_update_matrix(um, ax=None, *, vmin=None, vmax=None, cmap=None, colorbar=True, title='', xlabel='Previous stimulus', ylabel='Current stimulus')[source]#

Heatmap of the shift matrix (symmetric colour scale unless given).

Parameters:
  • um (UpdateMatrix) – From compute_update_matrix.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • vmin (float | None) – Lower end of the colour scale; None for minus the matrix’s largest absolute value.

  • vmax (float | None) – Upper end of the colour scale; None for the matrix’s largest absolute value.

  • cmap (str | Colormap | None) – Colour map; None for UM_CMAP.

  • colorbar (bool) – Add a colour bar.

  • title (str) – Axes title; ‘’ for none.

  • xlabel (str) – Label of the x axis (the previous stimulus).

  • ylabel (str) – Label of the y axis (the current stimulus).

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_conditional_psychometric(cp, ax=None, *, param='mu', color=None, label=None, show_unconditional=True, title='')[source]#

One parameter as a function of the previous-stimulus bin; hollow markers where it fell back.

Parameters:
  • cp (ConditionalPsychometric) – From compute_conditional_psychometric.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • param (str) – The parameter to draw: ‘mu’, ‘sigma’, ‘lapse_low’ or ‘lapse_high’.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • show_unconditional (bool) – Draw the unconditional fit’s value as a dashed line.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_binned_curve(curve, ax=None, *, color=None, label=None, show_reference=True, title='')[source]#

Binned P(choose B), or P(correct), per stimulus bin.

Parameters:
  • curve (BinnedCurve) – From compute_binned_curve.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • show_reference (bool) – Draw dashed lines at P = 0.5 and at the category boundary.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_sd_profile(prof, ax=None, *, color=None, label=None, title='')[source]#

The serial-dependence profile: P(B) shift per previous-stimulus bin.

Parameters:
  • prof (SerialDependenceProfile) – From compute_sd_profile.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_trajectory(result, stat, ax=None, color=None, label=None, alpha=0.7, linewidth=1.5, marker='o', markersize=5, linestyle='-', show_distribution_boundaries=True, title='')[source]#

One stat per session, in session order, from compute_phase_stats(..., per_session=True).

Parameters:
  • result (PhaseStats) – A PhaseStats with sessions filled.

  • stat (str) – The statistic to draw.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – Colour of the line and markers; None for the default colour.

  • label (str | None) – Legend label; None for none.

  • alpha (float) – Opacity of the line.

  • linewidth (float) – Width of the line.

  • marker (str) – Marker style.

  • markersize (float) – Marker size.

  • linestyle (str) – Line style.

  • show_distribution_boundaries (bool) – Draw vertical lines where distribution changes.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_rolling_stats(result, stat, ax=None, color=None, label=None, alpha=0.7, linewidth=1.5, sessions=None, title='')[source]#

One stat against trial position, one line per session, from compute_rolling_stats.

Parameters:
  • result (RollingStats) – A RollingStats.

  • stat (str) – The statistic to draw; it must be in result.names.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color (str | None) – One colour for every session’s line; None gives each session its own.

  • label (str | None) – Legend label, attached to the first line only; None for none.

  • alpha (float) – Opacity of the lines.

  • linewidth (float) – Width of the lines.

  • sessions (Sequence[str] | None) – Which of result.sessions to draw; None for all.

  • title (str) – Axes title; ‘’ for none.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_psychometric_comparison(result, contrast=None, ax=None, *, color_a='#d62728', color_b='#444444', show_stats=True, show_band=True, show_data=True, stats_keys=('mu', 'sigma', 'accuracy'), unit=None)[source]#

Two psychometric curves (phase over reference) with the Δ, CI and p annotated.

Requires compute_delta_stat(..., curve=True).

Parameters:
  • result (DeltaStats) – From compute_delta_stat.

  • contrast (str | None) – Which contrast to draw; None for the result’s only one.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • color_a (str) – Colour of the compared phase.

  • color_b (str) – Colour of the reference phase.

  • show_stats (bool) – Write the Δ, CI and p of stats_keys on the axes.

  • show_band (bool) – Draw each curve’s bootstrap band.

  • show_data (bool) – Draw each phase’s binned proportions as points.

  • stats_keys (Sequence[str]) – The statistics to write.

  • unit (str | None) – Which unit’s bootstrap CI to quote; None for the result’s primary unit.

Returns:

The figure and the axes drawn on.

Raises:

KeyError – contrast is not in the result, or is None while the result has several.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_stat_comparison(result, stat, ax=None, *, phase_order=None, palette=None, show_p=True, units=None)[source]#

One stat: a point per phase with its bootstrap CI(s); p above each non-reference phase.

The point is the pooled estimate and the interval its sampling uncertainty, not a spread over sessions. Above a non-reference phase the permutation p is shown when there is one, otherwise the bootstrap p per unit (pt trials, ps sessions).

Parameters:
  • result (DeltaStats) – From compute_delta_stat.

  • stat (str) – The statistic to draw.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • phase_order (Sequence[str] | None) – The phases from left to right; None for the result’s order.

  • palette (Sequence[str] | None) – One colour per phase; None for the default colours.

  • show_p (bool) – Write the p values.

  • units (Sequence[str] | None) – Which units’ intervals to draw; None for the result’s primary unit only.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_stat_comparison_grid(result, stats=None, *, ncols=3, phase_order=None, palette=None, panel_size=(3.0, 3.0), suptitle=None, show_p=True, units=None)[source]#

Grid of plot_stat_comparison(), one panel per stat.

A layout helper, the documented exception to single-panel plotters.

Parameters:
  • result (DeltaStats) – From compute_delta_stat.

  • stats (Sequence[str] | None) – The statistics, one panel each; None for every statistic in the result.

  • ncols (int) – Panels per row.

  • phase_order (Sequence[str] | None) – The phases from left to right; None for the result’s order.

  • palette (Sequence[str] | None) – One colour per phase; None for the default colours.

  • panel_size (tuple) – Width and height of one panel, in inches.

  • suptitle (str | None) – Title over the grid; None for none.

  • show_p (bool) – Write the p values.

  • units (Sequence[str] | None) – Which units’ intervals to draw; None for the result’s primary unit only.

Returns:

The figure and its axes, one per statistic.

Return type:

Tuple[Figure, list]

behav_utils.plotting.plot_interaction(interaction, stat, ax=None, *, show_p=True, show_components=True, colour_a='#1f77b4', colour_b='#ff7f0e', colour_interaction='#7f2704', units=None)[source]#

One stat: Δ_a, Δ_b (context) and their difference with its CI and bootstrap p.

A ‘∗’ marks a session CI that excludes zero.

Parameters:
  • interaction (Interaction) – From compute_interaction.

  • stat (str) – The statistic to draw.

  • ax (Axes | None) – Axes to draw on; None makes a new figure.

  • show_p (bool) – Write the bootstrap p.

  • show_components (bool) – Draw the two contrasts as well as their difference.

  • colour_a (str) – Colour of the first contrast.

  • colour_b (str) – Colour of the second contrast.

  • colour_interaction (str) – Colour of the difference.

  • units (Sequence[str] | None) – Which units’ intervals to draw; None for the first unit in the result.

Returns:

The figure and the axes drawn on.

Return type:

Tuple[Figure, Axes]

behav_utils.plotting.plot_interaction_grid(interaction, stats=None, *, ncols=4, panel_size=(3.0, 3.0), suptitle=None, show_p=True, show_components=True, colour_a='#1f77b4', colour_b='#ff7f0e', colour_interaction='#7f2704', units=None)[source]#

Grid of plot_interaction(), one panel per stat, each with its own y-scale.

A layout helper, the documented exception to single-panel plotters.

Parameters:
  • interaction (Interaction) – From compute_interaction.

  • stats (Sequence[str] | None) – The statistics, one panel each; None for every statistic in the result.

  • ncols (int) – Panels per row.

  • panel_size (tuple) – Width and height of one panel, in inches.

  • suptitle (str | None) – Title over the grid; None for none.

  • show_p (bool) – Write the bootstrap p.

  • show_components (bool) – Draw the two contrasts as well as their difference.

  • colour_a (str) – Colour of the first contrast.

  • colour_b (str) – Colour of the second contrast.

  • colour_interaction (str) – Colour of the difference.

  • units (Sequence[str] | None) – Which units’ intervals to draw; None for the first unit in the result.

Returns:

The figure and its axes, one per statistic.

Return type:

Tuple[Figure, list]

behav_utils.plotting.apply_style(dpi=100, font_size=10)[source]#

Apply default behav_utils style to matplotlib.

It sets matplotlib’s rcParams for the rest of the session: white backgrounds, no top or right spines, and tight bounding boxes when saving.

Parameters:
  • dpi (int) – Figure resolution, in dots per inch.

  • font_size (float) – Base font size, in points.

Return type:

None

behav_utils.plotting.get_colour(index_or_name)[source]#

A colour from an index into PALETTE (wrapping round), a role in COLOURS, or a colour itself.

get_colour(0) is PALETTE[0], get_colour('correct') is COLOURS['correct'], and a colour such as '#ff0000' comes back unchanged.

Parameters:

index_or_name – The index, role or colour.

Returns:

The colour.

Constants#

PALETTE is the list of colours the plotters cycle through, COLOURS gives the colour for a named role (correct, error, no_response, mean_line, sem_fill, default), and UM_CMAP is the colour map for update matrices.