SpikePlot

Plot spike data in a raster plot.

Plot spike times as a raster plot where the horizontal axis is time and the rows of the vertical axis are spike trains of individual neurons. This sets and holds many variables for plotting such that the user can simply set the parameters once and then redraw the spikes or scale and zoom to navigate spike data.

EXAMPLE:

This simple example generates some random “spikes” and displays them.

import numpy
from neuronpy.graphics import spikeplot

spikes = []
num_cells = 10
num_spikes_per_cell = 20
frequency = 20

# Make the spike data. Use a simple Poisson-like spike generator 
# (just for illustrative purposes here. Better spike generators should 
# be used in simulations).
for i in range(num_cells):
    isi=numpy.random.poisson(frequency, num_spikes_per_cell)
    spikes.append(numpy.cumsum(isi))
    
# spikes is now a list of lists where each cell has a list of spike
# times. Now, let's plot these spikes.
sp = spikeplot.SpikePlot(savefig=True)
sp.plot_spikes(spikes)

AUTHORS:

  • THOMAS MCTAVISH (2010-03-01): initial version, 0.1

  • THOMAS MCTAVISH (2012-04-10): Few changes to get spiketimehistograms and

    spikesums to coexist better.

class neuronpy.graphics.spikeplot.SpikePlot(**kwargs)[source]

Manager for the spike raster axes as well as the axes manager for SpikeTimeHistogram, and SpikeSum subplots. Additionally, the line formatting parameters in SpikeParams funnel through this object. In fact, it may be rare to access those classes directly and instead use the methods in this class

Plots are drawn iteratively in that the user specifies the drawing parameters for a set of spikes before telling the SpikePlot object to plot any spikes. Therefore, when plotting multiple sets of spikes in the same plot, the paradigm is:

sp = SpikePlot()
sp.plot_spikes(some_spikes)
sp.set_<some_line_format_method>()
sp.set_<some_other_formatting_options>()
sp.plot_spikes(some_other_spikes)

Indeed, the method to be most familiar with is plot_spikes().

If the spike data is not too large to fit into RAM and you want to navigate it, it is generally a good idea to specify the complete data to plot and then simply modify the view of the axes to zoom or translate the plot. This is detailed in update_xlim().

Parameters can be set during instanciation of a SpikePlot object by passing in various <parameter>=<value> pairs or through accessor methods, get_<parameter>() and set_<parameter>(). As <parameter>=<value> pairs, they use the kwargs (short for keyword arguments) parameter on instanciation. Below is a summary of keyword parameters, which are more detailed in the corresponding set_<parameter>() method. Some lesser-used parameters have accessor methods, but are not available as keyword parameters.

Summary of SpikePlot keyword parameters:

Parameter Description Default
fig The Figure handle. None
fig_name Name of figure, if saved as a file. 'spikeplot.png'
figsize Size of the figure, in inches. (8, 6)
marker Tick marks style. '|'
markercolor Line and marker color. 'black'
markeredgewidth Line and edge width of tick marks. 0.75
markerscale Tick mark row height. 1.0
savefig Flag to save a figure to a file when drawing. False
sth_ratio SpikeTimeHistogram axes height. 0
sum_ratio SpikeSum Histogram axes width. 0
get_axes_pad()[source]

Returns the amount of padding between the raster axes and the spike time histogram and/or spike sum axes if they are drawn.

get_data_pad()[source]

Returns the amount of padding in terms of the data width to draw a full-screen image. The data width is the last spike in the data minus the first spike in the data. The pad ensures that the first and last spike will appear in the figure and not be blocked by the vertical axis.

get_fig()[source]

Returns a handle to the Figure.

get_fig_name()[source]

Returns the name of the file that will be written when plotted with the savefig variable set to True.

get_figsize()[source]

Returns the size of the figure in inches.

get_linestyle()[source]

Returns the linestyle of the raster plot.

get_linewidth()[source]

Returns the line width of the raster plot if the linestyle is not ‘none’.

get_marker()[source]

Returns the marker type for raster tick marks.

get_markercolor()[source]

Returns the marker and line color.

get_markeredgewidth()[source]

Returns the size of the tick mark edge. In the case of a vertical bar, this is the linewidth of that bar.

get_markerscale()[source]

Returns the relative size of the marker in the raster plot.

get_raster_axes()[source]

Returns a handle to the raster axes.

get_savefig()[source]

Returns the flag of whether a file is saved upon plotting.

get_sth()[source]

Returns the instance of the SpikeTimeHistogram object.

get_sum()[source]

Returns the instance of the SpikeSum object.

plot_spikes(spikes, label=None, draw=True, savefig=None, cell_offset=0)[source]

Plot the spikes. This sets or appends the spike data to be drawn. If a single snapshot is needed, simply pass in the desired spikes to plot. If, however, the plot is to be interactive, it is best to send the full range of spikes and then zoom and translate by modifying the axes via the update_xlim() method.

Parameters:
  • spikes – a 2D vector where cells are in the first dimension and their spike times are in the second. This can be a list of lists or a 2D numpy.ndarray. If the spike train of a single cell is desired for plotting, simply encapsulate the 1D vector in square brackets: sp.plot_spikes([spikevec]).
  • label – is the name assigned to this set of spikes. This label is used largely internally by the SpikePlot object to differentiate multiple sets of spikes. Default is None. Any previously assigned spikes with this label will be replaced.
  • draw – is a boolean to specify whether or not to draw. By default, this is True, but it may be desirable to not draw when ascribing multiple sets of spikes and to draw to the screen when setting the last spikes. If other postprocessing is done to the figure or axes, it may also make sense to set this value to False.
  • savefig – is a boolean to specify whether to save the figure to a file. By default this is None and is undefined. If, however, it is set to True or False, then this is equivalent to calling set_savefig() with that value.
  • cell_offset – is a vertical offset for this set of spikes in the spike raster. By default this is 0. However, if the spike raster contains stacked cells of different types (i.e. multiple spike sets each with a different label), then the offset values specifies the vertical offset for these cells.
set_axes_pad(pad)[source]

Set the amount of padding between the spike axes and the spike time histogram and cumulative spike plot if they are shown.

Parameters:pad (float; default 0.01) – amount to pad. This value is in figure coordinates and needs to be between 0 and 1, and probably very close to 0. A value of 0 means that the axes touch each other.
set_data_pad(pad)[source]

Set the amount of padding in terms of the data width to draw a full-screen image. The data width is the last spike in the data minus the first spike in the data. The pad ensures that the first and last spike will appear in the figure and not be blocked by the vertical axis.

Parameters:pad (float; default 0.025) – amount to pad. The default value of 0.025 means that the maximum x-axis dimensions will be (first_spike - 0.025*data_width, last_spike + 0.025*data_width).
set_fig(fig=None)[source]

Set the figure handle.

Parameters:fig – the figure handle. If this is not None, then this also instantiates the ax axes by either creating one if it does not already exist in the figure, or by assigning the first axes object. If None, then the figure handle and the axes handle are both set to None.
set_fig_name(fig_name='spikeplot.png')[source]

Sets the name of the output figure.

Parameters:fig_name (str; default “spikeplot.png”) – the output file name.

Note

The extension should be included with the name and should be a valid graphics format supported by the backend drawing system. This is not checked for, but most graphics formats include png, pdf, ps, eps and svg.

set_figsize(figsize=None)[source]

Set the figure size in inches.

Parameters:figsize (tuple; default None) – a tuple specified as (width, height). If None, then it will use the Matplotlib default for a figure size when creating a new figure.
set_linestyle(linestyle)[source]

Sets the horizontal line style. Valid styles are defined by Matplotlib.lines.Line2D linestyle.

set_linewidth(linewidth)[source]

Sets the width of the horizontal line for each cell, if the linestyle is not ‘None’.

Parameters:linewidth (float; default 0.75) – the width of the line.
set_marker(marker)[source]

Sets the tick mark symbol. Valid markers are defined by Matplotlib.lines.Line2D markers.

set_markercolor(color)[source]

Set the marker color.

Parameters:color – Any Matplotlib color.

Note

Any subsequent drawing will keep this color, so call get_markercolor() if you want to restore drawing.

set_markeredgewidth(markeredgewidth)[source]

Sets the edge thickness of the tick mark.

Parameters:markeredgewidth (float; default 0.75) – the thickness of the marker.
set_markerscale(markerscale)[source]

Sets the scale of the tick marks between rows.

Parameters:markerscale – the relative marker size between rows. A value of 1 means that the tick mark, if the marker is |, will make it so that each row just touches. Smaller values will make the marker size smaller and larger values will cause the markers to bleed across rows.
set_raster_axes(axes)[source]

Sets (or resets) the raster axes to the axes handle passed in. This may be necessary when resizing the image or axes so that the markers can be resized to scale with the image.

set_savefig(savefig)[source]

Set the flag to save a figure after drawing. The name of the file is defined by the fig_name variable.

Parameters:savefig – is a boolean to specify whether to save the plotted figure to a file. By default this is False. When set to True, the file specified by the fig_name member variable is written.

Note

Since the interactive reference running on the NEURON SAGE server draws graphics by importing images into the web page. Many examples set savefig to True during instanciation for proper functionality with that guide.

set_sth_ratio(sth_ratio)[source]

Set the ratio of the spike time histogram axes to the figure.

Parameters:sth_ratio (float; default is zero.) – is the vertical amount between [0,1] in figure space to draw a spike time histogram. A value of zero means that the spike time histogram axes is not drawn. A value of 0.5 means that the figure will split vertically in half the spike raster and the histogram. A value of 1.0 means that the spike raster is not drawn but the histogram takes up the whole space.
set_sum_ratio(sum_ratio)[source]

Set the ratio of the spike sum histogram axes to the figure.

Parameters:sum_ratio (float; default is zero.) – is the horizontal amount between [0,1] in figure space to draw a spike time histogram. A value of zero means that the spike time histogram axes is not drawn. A value of 0.5 means that the figure will split vertically in half the spike raster and the histogram. A value of 1.0 means that the spike raster is not drawn but the histogram takes up the whole space.
update_xlim(xlim)[source]

Update the xlim of the raster axes and propagate this info to the other axes if they are visible, redrawing and re-writing a figure if necessary.

Parameters:xlim – is a tuple of length 2 specifying the new axes bounds.

Previous topic

SpikeParams

Next topic

SpikeSum

This Page