cibrrig.plot
Attributes
Functions
|
Flexibly overlay laser stimulation. |
|
Plot laser data from a "laser" AlfBunch. |
|
Plot laser intervals from arrays. |
|
Plot laser data from a "log" AlfBunch object. |
|
Create a new figure and axis. |
|
Set up a colorbar for the plot. |
|
Plot low-D projection with unique coloring for the given intervals. |
|
Plot low-d projection as a line. Optionally |
|
Plot 2D projection line. |
|
Plot 3D projection line. |
|
Plot projection in 2D or 3D. |
|
Plot 3D projection. |
|
Plot 2D projection. |
|
Plot covariate y as a function of phase x on a polar. |
|
Plot a reset curve for optogenetic stimulation, showing phase-dependent effects on breathing cycles. |
|
Time-aligns a trace x to event times specified in times. |
|
Plotting of underlying vector fields from Linderman Lab |
|
Extension of the linderman vectorfield plot to 3D |
|
Modify 3D axes to be cleaner: |
|
Clean the appearance of a polar plot. |
Clean the appearance of a plot with a range o [-pi,pi] but on a normal, linear axis |
|
|
#TODO: Update documentation |
|
Replace the x-axis with a horizontal bar showing the time scale of the plot. |
|
|
|
Plot a peri-event time histogram and raster plot |
Module Contents
- cibrrig.plot.plot_laser(laser_in, **kwargs)[source]
Flexibly overlay laser stimulation.
- Parameters:
laser_in (AlfBunch or array-like) – The laser data to be plotted. Can be an AlfBunch object or an array of intervals.
**kwargs – Additional keyword arguments to be passed to the plotting functions. These may include: mode (str): The plotting mode. Options are “shade”, “bar”, “vline”, or any other (defaults to steps). ax (matplotlib.axes.Axes): The axes object to plot on. If None, a new figure and axes will be created. amp_label (str): Label for the amplitude axis when plotting amplitudes. wavelength (int): Laser wavelength in nm, used to determine the color of the plot. alpha (float or array-like): The alpha (transparency) value(s) for shaded areas. color (str or tuple): The color to use for plotting. If not provided, a default color based on wavelength is used. query (str): Query string to filter the data (only used in _plot_laser_log). rotation (int): Rotation angle for text annotations (only used in _plot_laser_log). fontsize (int): Font size for text annotations (only used in _plot_laser_log). Any other keyword arguments accepted by matplotlib plotting functions.
- Returns:
The axes object containing the plot.
- Return type:
matplotlib.axes.Axes
Notes
This function determines the appropriate plotting method based on the input type: - If laser_in is an AlfBunch object with a ‘category’ key, it calls _plot_laser_log. - If laser_in is an AlfBunch object without a ‘category’ key, it calls _plot_laser_alf. - For other input types, it calls _plot_laser_intervals.
The specific kwargs used may vary depending on which underlying plotting function is called.
- cibrrig.plot._plot_laser_alf(laser_in, **kwargs)[source]
Plot laser data from a “laser” AlfBunch.
- Parameters:
laser_in (AlfBunch) – The AlfBunch object containing laser data.
**kwargs – Additional keyword arguments to be passed to _plot_laser_intervals.
Notes
This function extracts intervals and amplitudes from the AlfBunch object and calls _plot_laser_intervals with the appropriate parameters. It determines whether to use milliwatts or volts for the amplitude label based on the available keys in laser_in.
- cibrrig.plot._plot_laser_intervals(intervals, amplitudes=None, ax=None, mode='shade', amp_label='', wavelength=473, alpha=0.2, **kwargs)[source]
Plot laser intervals from arrays.
- Parameters:
intervals (array-like) – Array of laser intervals, where each interval is [start_time, end_time].
amplitudes (array-like, optional) – Array of amplitude values corresponding to each interval.
ax (matplotlib.axes.Axes, optional) – The axes object to plot on. If None, a new figure and axes will be created.
mode (str, optional) – The plotting mode. Options are “shade”, “bar”, “vline”, or any other (defaults to steps).
amp_label (str, optional) – Label for the amplitude axis when plotting amplitudes.
wavelength (int, optional) – Laser wavelength in nm, used to determine the color of the plot. Default is 473.
alpha (float or array-like, optional) – The alpha (transparency) value(s) for shaded areas. Default is 0.2.
**kwargs – Additional keyword arguments to be passed to the plotting functions.
- Returns:
The axes object containing the plot.
- Return type:
matplotlib.axes.Axes
Notes
This function supports multiple plotting modes: - “shade”: Shades the intervals on the plot. - “bar”: Plots horizontal bars for each interval. - “vline”: Plots vertical lines at the start of each interval. - Any other mode defaults to plotting steps of the amplitudes.
The function handles color selection based on the wavelength and can use a list of alpha values for varying transparency across intervals.
- cibrrig.plot._plot_laser_log(log, query=None, rotation=45, fontsize=6, **kwargs)[source]
Plot laser data from a “log” AlfBunch object.
- Parameters:
log (AlfBunch) – The AlfBunch object containing laser log data.
query (str, optional) – Query string to filter the data. Default is None.
rotation (int, optional) – Rotation angle for text annotations. Default is 45.
fontsize (int, optional) – Font size for text annotations. Default is 6.
**kwargs – Additional keyword arguments to be passed to _plot_laser_intervals.
- Returns:
The axes object containing the plot.
- Return type:
matplotlib.axes.Axes
Notes
This function extracts opto data from the log, plots the intervals using _plot_laser_intervals, and adds text annotations for each interval. It handles both milliwatt and voltage amplitudes.
- cibrrig.plot._create_ax(dims, projection=None)[source]
Create a new figure and axis.
- Parameters:
dims (list) – Dimensions to plot.
projection (str, optional) – Type of projection for 3D plots.
- Returns:
Figure and Axes objects.
- Return type:
tuple
- cibrrig.plot._setup_colorbar(ax, p, vmin, vmax, colorbar_title)[source]
Set up a colorbar for the plot.
- Parameters:
ax (Axes) – The axes object to add the colorbar to.
p – The plot object to create the colorbar from.
vmin (float) – Minimum value for the colorbar.
vmax (float) – Maximum value for the colorbar.
colorbar_title (str) – Title for the colorbar.
- cibrrig.plot.plot_projection_line_multicondition(X, tbins, intervals, colors, dims=[0, 1], ax=None, alpha=0.5, lw=1, **kwargs)[source]
Plot low-D projection with unique coloring for the given intervals.
- Parameters:
X (array) – Data to plot.
tbins (array) – Time bins.
intervals (array) – Start and end times for each condition.
colors (list) – Colors for each condition.
dims (list) – Dimensions to plot.
ax (Axes, optional) – Axes to plot on.
alpha (float) – Alpha value for transparency.
lw (float) – Line width.
- Returns:
The axes object containing the plot.
- Return type:
Axes
- cibrrig.plot.plot_projection_line(X, cvar=None, dims=[0, 1], cmap='viridis', **kwargs)[source]
Plot low-d projection as a line. Optionally
- Parameters:
X (array) – Data to plot.
cvar (array, optional) – Color variable.
dims (list) – Dimensions to plot.
cmap (str) – Colormap to use.
**kwargs – Additional keyword arguments.
- Keyword Arguments:
ax (matplotlib.axes.Axes, optional) – The axes to plot on. If not provided, a new figure and axes will be created.
color (str, optional) – Color of the line if cvar is not provided. Default is “k” (black).
alpha (float, optional) – The alpha blending value, between 0 (transparent) and 1 (opaque). Default is 0.5.
lw (float, optional) – The line width. Default is 0.5.
vmin (float, optional) – Minimum of the colormap range. If not provided, it’s inferred from cvar.
vmax (float, optional) – Maximum of the colormap range. If not provided, it’s inferred from cvar.
colorbar_title (str, optional) – Title for the colorbar. Default is an empty string.
title (str, optional) – Title for the plot. Only used in 3D plots. Default is an empty string.
lims (list, optional) – The x, y, (and z for 3D) limits of the plot as [min, max]. Only used in 3D plots. Default is [-4, 4].
pane_color (color, optional) – Color of the panes in 3D plots. If None, default matplotlib style is used.
plot_colorbar (bool, optional) – Whether to plot the colorbar. Only used in 3D plots. Default is True.
- Returns:
The axes object containing the plot.
- Return type:
Axes
- cibrrig.plot._plot_projection_line_2D(X, cvar=None, dims=[0, 1], cmap='viridis', color='k', ax=None, alpha=0.5, vmin=None, vmax=None, lw=0.5, colorbar_title='', plot_colorbar=True, **kwargs)[source]
Plot 2D projection line.
- Parameters:
X (array) – Data to plot.
cvar (array, optional) – Color variable.
dims (list) – Dimensions to plot.
cmap (str) – Colormap to use.
color (str) – Color for the line if cvar is None.
ax (Axes, optional) – Axes to plot on.
alpha (float) – Alpha value for transparency.
vmin (float, optional) – Minimum value for colormap.
vmax (float, optional) – Maximum value for colormap.
lw (float) – Line width.
colorbar_title (str) – Title for the colorbar.
**kwargs – Additional keyword arguments.
- Returns:
The axes object containing the plot.
- Return type:
Axes
- cibrrig.plot._plot_projection_line_3D(X, cvar=None, dims=[0, 1, 2], cmap='viridis', color='k', ax=None, title='', alpha=0.5, lims=None, pane_color=None, colorbar_title='', plot_colorbar=True, vmin=None, vmax=None, lw=0.5, **kwargs)[source]
Plot 3D projection line.
- Parameters:
X (array) – Data to plot.
cvar (array, optional) – Color variable.
dims (list) – Dimensions to plot.
cmap (str) – Colormap to use.
color (str) – Color for the line if cvar is None.
ax (Axes3D, optional) – 3D axes to plot on.
title (str) – Title for the plot.
alpha (float) – Alpha value for transparency.
lims (list) – Limits for the axes.
pane_color – Color for the panes.
colorbar_title (str) – Title for the colorbar.
plot_colorbar (bool) – Whether to plot the colorbar.
vmin (float, optional) – Minimum value for colormap.
vmax (float, optional) – Maximum value for colormap.
lw (float) – Line width.
**kwargs – Additional keyword arguments.
- Returns:
The 3D axes object containing the plot.
- Return type:
Axes3D
- cibrrig.plot.plot_projection(X, dims, **kwargs)[source]
Plot projection in 2D or 3D.
- Parameters:
X (array) – Data to plot.
dims (list) – Dimensions to plot.
**kwargs – Additional keyword arguments.
- Keyword Arguments:
ax (matplotlib.axes.Axes, optional) – The axes to plot on. If not provided, a new figure and axes will be created.
color (str, optional) – The color of the data points or lines. Default is “k” (black) if no color variable (cvar) is provided.
alpha (float, optional) – Transparency level for the points. Should be between 0 (fully transparent) and 1 (fully opaque). - Default is 0.5.
lw (float, optional) – Line width for plotting. Applies if plotting a line plot. Default is 0.5.
vmin (float, optional) – Minimum value for the colormap if a color variable (cvar) is used. If not specified, it’s inferred from cvar.
vmax (float, optional) – Maximum value for the colormap if a color variable (cvar) is used. If not specified, it’s inferred from cvar.
colorbar_title (str, optional) – Title for the colorbar. Default is an empty string.
title (str, optional) – Title for the plot. Only used in 3D plots. Default is an empty string.
lims (list, optional) – Axis limits for the plot. Should be a list of [min, max] values. - Default is [-4, 4] for 3D plots.
pane_color (str, optional) – Background color of the 3D panes in 3D plots. If None, default style is used.
plot_colorbar (bool, optional) – Whether to include a colorbar in the plot. Only applies to 3D plots when cvar is used. Default is True.
s (float, optional) – Size of the markers used in the scatter plot. Default is 1.
cmap (str, optional) – Colormap to use when plotting with a color variable (cvar). Default is “viridis”.
cvar (array-like, optional) – An array of values used to color the data points. If provided, cmap is applied.
s – Size of the markers in the scatter plot. Default is 1.
- Returns:
Figure and Axes objects.
- Return type:
tuple
- cibrrig.plot.plot_3D_projection(X, dims=[0, 1, 2], cvar=None, ax=None, title='', s=1, vmin=None, vmax=None, cmap='viridis', c='k', alpha=0.2, lims=None, plot_colorbar=True, colorbar_title='', pane_color=None, **kwargs)[source]
Plot 3D projection.
- Parameters:
X (array) – Data to plot.
dims (list) – Dimensions to plot.
cvar (array, optional) – Color variable.
ax (Axes3D, optional) – 3D axes to plot on.
title (str) – Title for the plot.
s (float) – Size of the markers.
vmin (float, optional) – Minimum value for colormap.
vmax (float, optional) – Maximum value for colormap.
cmap (str) – Colormap to use.
c (str) – Color for the markers if cvar is None.
alpha (float) – Alpha value for transparency.
lims (list) – Limits for the axes.
plot_colorbar (bool) – Whether to plot the colorbar.
colorbar_title (str) – Title for the colorbar.
pane_color – Color for the panes.
**kwargs – Additional keyword arguments.
- Returns:
Figure and Axes3D objects.
- Return type:
tuple
- cibrrig.plot.plot_2D_projection(X, dims=[0, 1], cvar=None, ax=None, title='', s=1, vmin=None, vmax=None, cmap='viridis', c='C1', alpha=0.2, lims=[-4, 4], plot_colorbar=True, colorbar_title='')[source]
Plot 2D projection.
- Parameters:
X (array) – Data to plot.
dims (list) – Dimensions to plot.
cvar (array, optional) – Color variable.
ax (Axes, optional) – Axes to plot on.
title (str) – Title for the plot.
s (float) – Size of the markers.
vmin (float, optional) – Minimum value for colormap.
vmax (float, optional) – Maximum value for colormap.
cmap (str) – Colormap to use.
c (str) – Color for the markers if cvar is None.
alpha (float) – Alpha value for transparency.
lims (list) – Limits for the axes.
plot_colorbar (bool) – Whether to plot the colorbar.
colorbar_title (str) – Title for the colorbar.
- Returns:
Figure and Axes objects.
- Return type:
tuple
- cibrrig.plot.plot_polar_average(x, y, t, ax=None, t0=None, tf=None, color='k', bins=50, multi='sem', alpha=0.3, **plot_kwargs)[source]
Plot covariate y as a function of phase x on a polar.
If t0,tf are arrays, will average over multiple intervals
- Parameters:
x (1D numpy array) – Phase data with values in the range [-pi, pi].
y (1D numpy array) – Signal data to be plotted against x.
t (1D numpy array) – Time data corresponding to x and y.
ax (matplotlib.axes.Axes, optional) – The axes object to plot on. If None, a new figure and axes are created. Defaults to None.
t0 (int, float, or list, optional) – Start time(s) for epoch selection. If a list or array, averages over multiple epochs are computed. Defaults to None.
tf (int, float, or list, optional) – End time(s) for epoch selection, matching the format of t0. Defaults to None.
color (str or list, optional) – Line color(s) for the plot. Defaults to ‘k’.
bins (int, optional) – Number of bins for the polar histogram. Defaults to 50.
multi (str, optional) – Specifies the method for calculating the shaded region. Options are ‘std’ for standard deviation or ‘sem’ for standard error of the mean. Defaults to ‘sem’.
alpha (float, optional) – Transparency of the shaded region. Defaults to 0.3.
**plot_kwargs – Additional keyword arguments passed to ax.plot.
- Returns:
- A tuple containing:
f: The created figure object (or None if ax was provided).
ax: The axes object used for plotting.
y_polar_out: 2D numpy array of the polar data averaged over epochs.
phase_bins: Phase bin centers for the plot.
- Return type:
tuple
Example
>>> plot_polar_average(x, y, t, t0=0, tf=10, color='b', bins=30, multi='std')
- cibrrig.plot.plot_reset_curve(breaths, events, wavelength=473, annotate=False, norm=True, plot_tgl=True, n_control=100)[source]
Plot a reset curve for optogenetic stimulation, showing phase-dependent effects on breathing cycles.
- Parameters:
breaths (AlfBunch) – Breath timing data with attributes: ‘times’, ‘IBI’, and ‘duration_sec’.
events (np.ndarray) – 1D array of stimulation/event times.
wavelength (int, optional) – Wavelength of optogenetic stimulus. Defaults to 473.
annotate (bool, optional) – If True, add annotations and color overlays to the plot. Defaults to False.
norm (bool, optional) – If True, normalizes time to phase (0-1) for plotting. Defaults to True.
plot_tgl (bool, optional) – If True, creates a plot; if False, returns computed data. Defaults to True.
n_control (int, optional) – Number of random control points for a control distribution. Defaults to 100.
- Returns:
cycle_stim_time: Normalized/raw times of stimulation relative to breath onset.
cycle_duration: Normalized/raw breath cycle durations following stimulation.
cycle_stim_time_rand: Control times for stimulation from random event times.
cycle_duration_rand: Control breath cycle durations for random events.
- Return type:
tuple
- cibrrig.plot.plot_sweeps(xt, x, times, pre, post, ax=None, **kwargs)[source]
Time-aligns a trace x to event times specified in times.
- Parameters:
xt (array-like) – Time values corresponding to the signal trace x.
x (array-like) – Signal trace data to be plotted.
times (array-like) – Event times to align the trace x to.
pre (float) – Time before each event to start the trace.
post (float) – Time after each event to end the trace.
ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, a new figure and axes will be created. Default is None.
**kwargs – Additional keyword arguments passed to ax.plot, such as line style or color.
- Returns:
matplotlib axis
- Return type:
ax
- cibrrig.plot.plot_most_likely_dynamics(model, xlim=(-4, 4), ylim=(-3, 3), nxpts=20, nypts=20, alpha=0.8, ax=None, figsize=(3, 3), colors=[f'C{x}' for x in range(7)], zval=None)[source]
Plotting of underlying vector fields from Linderman Lab
- cibrrig.plot.plot_most_likely_dynamics_3D(model, xlim=(-4, 4), ylim=(-3, 3), zlim=(-3, 3), nxpts=10, nypts=10, nzpts=10, alpha=0.2, ax=None, figsize=(3, 3), length=0.2, colors=[f'C{x}' for x in range(7)])[source]
Extension of the linderman vectorfield plot to 3D
- cibrrig.plot._clean_3d_axes(ax, title, dims, pane_color, lims=None)[source]
- Modify 3D axes to be cleaner:
Set title set axis labels make limits equal turn off grid set background color
- Parameters:
ax (matplotlib.axes._subplots.Axes3DSubplot) – The 3D axes object to customize.
title (str) – The title of the plot.
dims (tuple or list of ints) – Dimensions to label the axes, corresponding to the 3D data dimensions (e.g., (0, 1, 2) for first three components).
pane_color (tuple or None) – RGB color to set for the panes (background of each axis). Use None for default color.
lims (tuple or list of floats,optional) – If None, autoscales axes. Axis limits to set for x, y, and z axes (e.g., (-1, 1) to set limits for all axes).
- Returns:
The modified axes object.
- Return type:
ax (matplotlib.axes._subplots.Axes3DSubplot)
Example
ax = fig.add_subplot(111, projection=’3d’) _clean_3d_axes(ax, “3D Plot”, (0, 1, 2), (0.9, 0.9, 0.9, 0.5), (-1, 1))
- cibrrig.plot.clean_polar_axis(ax)[source]
Clean the appearance of a polar plot. Use pi/2 (90 degrees) angular ticks, no internal radial ticks, and set labels to mathtext pi
- Parameters:
ax (matplotlib.projections.polar.PolarAxes) – The polar axes object to modify.
Example
ax = plt.subplot(projection=’polar’) clean_polar_axis(ax)
- cibrrig.plot.clean_linear_radial_axis(ax)[source]
Clean the appearance of a plot with a range o [-pi,pi] but on a normal, linear axis Sets ticks to every pi/2 interval and uses math text.
- Parameters:
ax (matplotlib.axes._subplots.AxesSubplot) – The axes object to modify.
Example
ax = plt.subplot() clean_linear_radial_axis(ax)
- cibrrig.plot.plot_driftmap_with_trace(spike_times, spike_depths, trace, trace_times, trace_label='', t0=None, tf=None, depth_lim=(None, None), trace_ylim=(None, None), t_bin=0.01, driftmap_kwargs={}, trace_kwargs={}, figsize=(2, 8), use_scalebar=True, use_colorbar=True, cmap=None, raster_ylabel=None)[source]
#TODO: Update documentation #TODO: UPdate to work more intuitively with clusters
Works well as a driftmap, but lass good as a rastermap
Plot a drift map with an covariate trace above.
Built off of ibllib.brainbox.plot.driftmap. This function plots a drift map of spike times and depths, with an overlaid trace such as diaphragm or another continuous signal.
- Parameters:
spike_times (array-like) – Array of spike times.
spike_depths (array-like) – Array of spike depths corresponding to each spike time.
trace (array-like) – Continuous signal to overlay on the drift map. Can be multiple columns as long as each row is a timepoint
trace_times (array-like) – Time points corresponding to the trace signal.
trace_label (str, optional) – Label for the trace. Defaults to ‘’.
t0 (float, optional) – Start time for the plot. Defaults to None, which uses the minimum spike time.
tf (float, optional) – End time for the plot. Defaults to None, which uses the maximum spike time.
depth_lim (tuple, optional) – Depth limits for the plot. Defaults to (None, None), which uses the min and max spike depths.
trace_ylim (tuple, optional) – Y-axis limits for the trace plot. Defaults to (None, None), which uses the min and max trace values.
driftmap_kwargs (dict, optional) – Additional keyword arguments for the drift map plot. Defaults to {}.
trace_kwargs (dict, optional) – Additional keyword arguments for the trace plot. Defaults to {}.
figsize (tuple, optional) – Figure size for the plot. Defaults to (2, 8).
use_scalebar (bool, optional) – Whether to use a scale bar in the plot. Defaults to True.
- Returns:
The axes object containing the drift map. matplotlib.axes._subplots.AxesSubplot: The axes object containing the trace plot.
- Return type:
matplotlib.axes._subplots.AxesSubplot
- cibrrig.plot.replace_timeaxis_with_scalebar(ax, pad=0.025, lw=None, color=None, size=None)[source]
Replace the x-axis with a horizontal bar showing the time scale of the plot.
- Parameters:
ax (matplotlib.axes._subplots.AxesSubplot) – The axes object to modify.
inverted_y (bool, optional) – Set true if the plot has 0 at the top
pad (float, optional) – Padding between the scale bar and the plot. Defaults to 0.01.
lw (float, optional) – Line width for the scale bar. Defaults to None, which uses the default line width.
color (str, optional) – Color for the scale bar. Defaults to None, which uses the default text color.
size (float, optional) – Font size for the scale bar. Defaults to None, which uses the default tick size.
- cibrrig.plot.plot_peth_and_raster(spike_times, starts, stops=None, pre_time=0.2, post_time=0.2, bin_size=0.01, smoothing=0, error_bars='sem', pethline_kwargs={}, errbar_kwargs={'alpha': 0.5}, eventline_kwargs={'color': plt.rcParams['text.color'], 'ls': '--'}, raster_kwargs={'s': 2, 'marker': '|'}, raster_ylabel='', figsize=(3, 6), subplot_ratio=(1, 5))[source]
Plot a peri-event time histogram and raster plot
- Parameters:
spike_times (array) – spike times
starts (array) – event times
stops (array, optional) – stop times. Defaults to None.
pre_time (float, optional) – time before event to plot. Defaults to 0.2.
post_time (float, optional) – time after event to plot. Defaults to 0.2.
bin_size (float, optional) – bin size for histogram. Defaults to 0.01.
smoothing (float, optional) – smoothing factor for histogram. Defaults to 0.
error_bars (str, optional) – error bars to plot. Defaults to “sem”.
pethline_kwargs (dict, optional) – kwargs for the peth line. Defaults to {}.
errbar_kwargs (dict, optional) – kwargs for the error bars. Defaults to {“alpha”: 0.5}.
eventline_kwargs (dict, optional) – kwargs for the event line. Defaults to {“color”: plt.rcParams[“text.color”], “ls”: “–“}.
raster_kwargs (dict, optional) – kwargs for the raster plot. Defaults to {‘s’:2,’marker’:’|’}.
raster_ylabel (str, optional) – ylabel for the raster plot. Defaults to “”.
figsize (tuple, optional) – figure size. Defaults to (3, 6).
subplot_ratio (tuple, optional) – ratio of the subplots. Defaults to (1, 5).
- Returns:
matplotlib axis for the raster and peth plots
- Return type:
ax_raster, ax_peth