pyneuroml.plot package#

pyneuroml.plot.Plot module#

Plotting helper functions.

File: pyneuroml/plot/Plot.py

Copyright 2023 NeuroML contributors

pyneuroml.plot.Plot.generate_interactive_plot(xvalues: List[float], yvalues: List[float], title: str, labels: List[str] | None = None, linestyles: List[str] | None = None, linewidths: List[int] | List[float] | None = None, markers: List[str] | List[int] | None = None, markersizes: List[int] | List[float] | None = None, plot_bgcolor: str | None = None, modes: List[str] | None = None, xaxis: str = None, yaxis: str = None, legend_title: str = None, xaxis_color: str = '#fff', yaxis_color: str = '#fff', xaxis_width: float | int = 1, yaxis_width: float | int = 1, xaxis_mirror: str | bool = False, yaxis_mirror: str | bool = False, xaxis_spikelines: bool = False, yaxis_spikelines: bool = False, grid: bool = True, logx: bool = False, logy: bool = False, layout: dict | None = None, show_interactive: bool = True, save_figure_to: str | None = None) None#

Utility function to generate interactive plots using Plotly.

This function can be used to generate graphs with multiple plot lines. For example, to plot two metrics you can use:

generate_interactive_plot(xvalues=[[ax1, ax2, ax3], [bx1, bx2, bx3]], yvalues=[[ay1, ay2, ay3], [by1, by2, by3]], labels=["metric 1", "metric 2"])

Please note that while plotting multiple plots, you should take care to ensure that the number of x values and y values for each metric correspond. These lists are passed directly to Plotly for plotting without additional sanity checks.

A number of options are provided for convenience to allow plotting of multiple traces in the same plot and modification of common layout options. A layout dict can also be passed instead, which will overwrite any individually set options. If you need more customisation, please look at the source code of this method to write your own.

See the plotly documentation for more information: https://plotly.com/python-api-reference/generated/plotly.graph_objects.scatter.html

Parameters:
pyneuroml.plot.Plot.generate_plot(xvalues: List[List[float]], yvalues: List[List[float]], title: str, labels: List[str] | None = None, colors: List[str] | None = None, linestyles: List[str] | None = None, linewidths: List[str] | None = None, markers: List[str] | None = None, markersizes: List[str] | None = None, xaxis: str = None, yaxis: str = None, xlim: List[float] = None, ylim: List[float] = None, show_xticklabels: bool = True, show_yticklabels: bool = True, grid: bool = False, logx: bool = False, logy: bool = False, font_size: int = 12, bottom_left_spines_only: bool = False, cols_in_legend_box: int = 3, legend_position: str | None = 'best', show_plot_already: bool = True, save_figure_to: str | None = None, title_above_plot: bool = False, verbose: bool = False, close_plot: bool = False) Axes | None#

Utility function to generate plots using the Matplotlib library.

This function can be used to generate graphs with multiple plot lines. For example, to plot two metrics you can use:

generate_plot(xvalues=[[ax1, ax2, ax3], [bx1, bx2, bx3]], yvalues=[[ay1, ay2, ay3], [by1, by2, by3]], labels=["metric 1", "metric 2"])

Please note that while plotting multiple plots, you should take care to ensure that the number of x values and y values for each metric correspond. These lists are passed directly to Matplotlib for plotting without additional sanity checks.

Please see the Matplotlib documentation for the complete list of available styles and colours: - https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html - https://matplotlib.org/stable/gallery/index.html

Parameters:
  • xvalues (list of lists) – X values

  • yvalues (lists of lists) – Y values

  • title (str) – title of plot

  • labels (list of strings) – labels for each plot (default: None)

  • colors (list of strings) – colours for each plot (default: None)

  • linestyles (list strings) – list of line styles (default: None)

  • linewidths (list of floats) – list of line widths (default: None)

  • markers (list strings) – list of markers (default: None)

  • markersizes (list of floats) – list of marker sizes (default: None)

  • xaxis (str) – label of X axis (default: None)

  • yaxis (str) – label of Y axis (default: None)

  • xlim (tuple of (float, float) or individual arguments: (left=float), (right=float) See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xlim.html) – left and right extents of x axis (default: None)

  • ylim (tuple of (float, float) or individual arguments: (top=float), (bottom=float) See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.ylim.html) – top and bottom extents of y axis (default: None)

  • show_xticklabels (boolean) – whether labels should be shown on xtics (default: True)

  • show_yticklabels (boolean) – whether labels should be shown on ytics (default: True)

  • grid (boolean) – enable/disable grid (default: False)

  • logx (boolean) – should the x axis be in log scale (default: False)

  • logy (boolean) – should the y ayis be in log scale (default: False)

  • font_size (float) – font size (default: 12)

  • bottom_left_spines_only (boolean) – enable/disable spines on right and top (default: False) (a spine is the line noting the data area boundary)

  • cols_in_legend_box (float) – number of columns to use in legend box (default: 3)

  • legend_position (str) –

    position of legend: See: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html Extra options:

    • ”outer right” places the legend on the right, but outside the axes box

    • ”bottom center” places the legend on the bottom, below the figure

  • show_plot_already (boolean) – if plot should be shown when created (default: True)

  • save_figure_to (str) – location to save generated figure to (default: None)

  • title_above_plot (boolean) – enable/disable title above the plot (default: False)

  • verbose (boolean) – enable/disable verbose logging (default: False)

  • close_plot (bool) – call pyplot.close() to close plot after plotting

Returns:

matplotlib.axes.Axes object if plot is not closed, else None

pyneuroml.plot.PlotSpikes module#

pyneuroml.plot.PlotSpikes.read_sonata_spikes_hdf5_file(file_name)#
pyneuroml.plot.PlotSpikes.run(a=None, **kwargs)#

pyneuroml.plot.PlotMorphology module#

Utilities to plot NeuroML2 cell morphologies.

File: pyneuroml/plot/PlotMorphology.py

Copyright 2023 NeuroML contributors

pyneuroml.plot.PlotMorphology.plot_2D(nml_file: str, plane2d: str = 'xy', min_width: float = 0.8, verbose: bool = False, nogui: bool = False, save_to_file: str | None = None, square: bool = False, plot_type: str = 'detailed', title: str | None = None, close_plot: bool = False)#

Plot cells in a 2D plane.

If a file with a network containing multiple cells is provided, it will plot all the cells. For detailed neuroml.Cell types, it will plot their complete morphology. For point neurons, we only plot the points (locations) where they are.

This method uses matplotlib.

Parameters:
  • nml_file (str) – path to NeuroML cell file

  • plane2d (str) – what plane to plot (xy/yx/yz/zy/zx/xz)

  • min_width (float) – minimum width for segments (useful for visualising very thin segments): default 0.8um

  • verbose (bool) – show extra information (default: False)

  • nogui (bool) – do not show matplotlib GUI (default: false)

  • save_to_file (str) – optional filename to save generated morphology to

  • square (bool) – scale axes so that image is approximately square

  • plot_type (str) –

    type of plot, one of:

    • ”detailed”: show detailed morphology taking into account each segment’s width

    • ”constant”: show morphology, but use constant line widths

    • ”schematic”: only plot each unbranched segment group as a straight line, not following each segment

    This is only applicable for neuroml.Cell cells (ones with some morphology)

  • title (str) – title of plot

  • close_plot (bool) – call pyplot.close() to close plot after plotting

pyneuroml.plot.PlotMorphology.plot_2D_cell_morphology(offset: List[float] = [0, 0], cell: Cell = None, plane2d: str = 'xy', color: str | None = None, title: str = '', verbose: bool = False, fig: Figure = None, ax: Axes = None, min_width: float = 0.8, axis_min_max: List = [inf, -inf], scalebar: bool = False, nogui: bool = True, autoscale: bool = True, square: bool = False, plot_type: str = 'detailed', save_to_file: str | None = None, close_plot: bool = False, overlay_data: Dict[int, float] = None, overlay_data_label: str | None = None, datamin: float | None = None, datamax: float | None = None, colormap_name: str = 'viridis')#

Plot the detailed 2D morphology of a cell in provided plane.

The method can also overlay data onto the morphology.

New in version 1.0.0.

See also

plot_2D()

general function for plotting

plot_2D_schematic()

for plotting only segmeng groups with their labels

plot_2D_point_cells()

for plotting point cells

Parameters:
  • offset ([float, float]) – offset for cell

  • cell (neuroml.Cell) – cell to plot

  • plane2d (str) – plane to plot on

  • color (str) – color to use for all segments

  • fig (matplotlib.figure.Figure) – a matplotlib.figure.Figure object to use

  • ax (matplotlib.axes.Axes) – a matplotlib.axes.Axes object to use

  • min_width (float) – minimum width for segments (useful for visualising very thin segments): default 0.8um

  • axis_min_max ([float, float]) – min, max value of axes

  • title (str) – title of plot

  • verbose (bool) – show extra information (default: False)

  • nogui (bool) – do not show matplotlib GUI (default: false)

  • save_to_file (str) – optional filename to save generated morphology to

  • square (bool) – scale axes so that image is approximately square

  • autoscale (bool) – toggle autoscaling

  • scalebar (bool) – toggle scalebar

  • close_plot (bool) – call pyplot.close() to close plot after plotting

  • overlay_data (dict, keys are segment ids, values are magnitudes to overlay on curtain plots) – data to overlay over the morphology this must be a dictionary with segment ids as keys, the single value to overlay as values

  • overlay_data_label (str) – label of data being overlaid

  • colormap_name (str) – name of matplotlib colourmap to use for data overlay See: https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.colormaps Note: random colours are used for each segment if no data is to be overlaid

  • datamin (float) – min limits of data (useful to compare different plots)

  • datamax (float) – max limits of data (useful to compare different plots)

Raises:

ValueError if cell is None

pyneuroml.plot.PlotMorphology.plot_2D_point_cells(offset: List[float] = [0, 0], plane2d: str = 'xy', color: str | None = None, soma_radius: float = 10.0, title: str = '', verbose: bool = False, fig: Figure = None, ax: Axes = None, axis_min_max: List = [inf, -inf], scalebar: bool = False, nogui: bool = True, autoscale: bool = True, square: bool = False, save_to_file: str | None = None, close_plot: bool = False)#

Plot point cells.

New in version 1.0.0.

See also

plot_2D()

general function for plotting

plot_2D_schematic()

for plotting only segmeng groups with their labels

plot_2D_cell_morphology()

for plotting cells with detailed morphologies

Parameters:
  • offset ([float, float]) – location of cell

  • plane2d (str) – plane to plot on

  • color (str) – color to use for cell

  • soma_radius (float) – radius of soma

  • fig (matplotlib.figure.Figure) – a matplotlib.figure.Figure object to use

  • ax (matplotlib.axes.Axes) – a matplotlib.axes.Axes object to use

  • axis_min_max ([float, float]) – min, max value of axes

  • title (str) – title of plot

  • verbose (bool) – show extra information (default: False)

  • nogui (bool) – do not show matplotlib GUI (default: false)

  • save_to_file (str) – optional filename to save generated morphology to

  • square (bool) – scale axes so that image is approximately square

  • autoscale (bool) – toggle autoscaling

  • scalebar (bool) – toggle scalebar

  • close_plot (bool) – call pyplot.close() to close plot after plotting

pyneuroml.plot.PlotMorphology.plot_2D_schematic(cell: Cell, segment_groups: List[SegmentGroup] | None, offset: List[float] = [0, 0], labels: bool = False, plane2d: str = 'xy', width: float = 2.0, verbose: bool = False, square: bool = False, nogui: bool = False, save_to_file: str | None = None, scalebar: bool = True, autoscale: bool = True, fig: Figure = None, ax: Axes = None, title: str = '', close_plot: bool = False) None#

Plot a 2D schematic of the provided segment groups.

This plots each segment group as a straight line between its first and last segment.

New in version 1.0.0.

See also

plot_2D()

general function for plotting

plot_2D_point_cells()

for plotting point cells

plot_2D_cell_morphology()

for plotting cells with detailed morphologies

Parameters:
  • offset ([float, float]) – offset for cell

  • cell (neuroml.Cell) – cell to plot

  • segment_groups (list(SegmentGroup)) – list of unbranched segment groups to plot

  • labels (bool) – toggle labelling of segment groups

  • plane2d (str) – what plane to plot (xy/yx/yz/zy/zx/xz)

  • width (float) – width for lines

  • verbose (bool) – show extra information (default: False)

  • square (bool) – scale axes so that image is approximately square

  • nogui (bool) – do not show matplotlib GUI (default: false)

  • save_to_file (str) – optional filename to save generated morphology to

  • fig (matplotlib.figure.Figure) – a matplotlib.figure.Figure object to use

  • ax (matplotlib.axes.Axes) – a matplotlib.axes.Axes object to use

  • title (str) – title of plot

  • square – scale axes so that image is approximately square

  • autoscale (bool) – toggle autoscaling

  • scalebar (bool) – toggle scalebar

  • close_plot (bool) – call pyplot.close() to close plot after plotting

pyneuroml.plot.PlotMorphology.plot_3D_cell_morphology(offset: List[float] = [0, 0, 0], cell: Cell = None, color: str | None = None, title: str = '', verbose: bool = False, current_scene: SceneCanvas = None, current_view: ViewBox = None, min_width: float = 0.8, axis_min_max: List = [inf, -inf], nogui: bool = True, plot_type: str = 'constant', theme='light')#

Plot the detailed 3D morphology of a cell using vispy. https://vispy.org/

New in version 1.0.0.

See also

plot_2D()

general function for plotting

plot_2D_schematic()

for plotting only segmeng groups with their labels

plot_2D_point_cells()

for plotting point cells

Parameters:
  • offset ([float, float]) – offset for cell

  • cell (neuroml.Cell) – cell to plot

  • color (str) –

    color to use for segments:

    • if None, each segment is given a new unique color

    • if “Groups”, each unbranched segment group is given a unique color, and segments that do not belong to an unbranched segment group are in white

    • if “Default Groups”, axonal segments are in red, dendritic in blue, somatic in green, and others in white

  • min_width (float) – minimum width for segments (useful for visualising very

  • axis_min_max ([float, float]) – min, max value of axes

  • title (str) – title of plot

  • verbose (bool) – show extra information (default: False)

  • nogui (bool) – do not show image immediately

  • current_scene (SceneCanvas) – vispy SceneCanvas to use (a new one is created if it is not provided)

  • current_view (ViewBox) – vispy viewbox to use

  • plot_type (str) –

    type of plot, one of:

    • ”detailed”: show detailed morphology taking into account each segment’s width. This is not performant, because a new line is required for each segment. To only be used for cells with small numbers of segments

    • ”constant”: show morphology, but use constant line widths

    This is only applicable for neuroml.Cell cells (ones with some morphology)

  • theme (str) – theme to use (dark/light)

Raises:

ValueError if cell is None

pyneuroml.plot.PlotMorphology.plot_3D_cell_morphology_plotly(nml_file: str, min_width: float = 0.8, verbose: bool = False, nogui: bool = False, save_to_file: str | None = None, plot_type: str = 'detailed')#

Plot NeuroML2 cell morphology interactively using Plot.ly

Please note that the interactive plot uses Plotly, which uses WebGL. So, you need to use a WebGL enabled browser, and performance here may be hardware dependent.

https://plotly.com/python/webgl-vs-svg/ https://en.wikipedia.org/wiki/WebGL

Parameters:
  • nml_file (str) – path to NeuroML cell file

  • min_width (float) – minimum width for segments (useful for visualising very thin segments): default 0.8um

  • verbose (bool) – show extra information (default: False)

  • nogui (bool) – do not show matplotlib GUI (default: false)

  • save_to_file (str) – optional filename to save generated morphology to

  • plot_type (str) –

    type of plot, one of:

    • detailed: show detailed morphology taking into account each segment’s width

    • constant: show morphology, but use constant line widths

pyneuroml.plot.PlotMorphology.plot_3D_schematic(cell: Cell, segment_groups: List[SegmentGroup] | None, offset: List[float] = [0, 0, 0], labels: bool = False, width: float = 5.0, verbose: bool = False, nogui: bool = False, title: str = '', current_scene: SceneCanvas = None, current_view: ViewBox = None, theme: str = 'light') None#

Plot a 3D schematic of the provided segment groups in Napari as a new layer..

This plots each segment group as a straight line between its first and last segment.

New in version 1.0.0.

See also

plot_2D_schematic()

general function for plotting

plot_2D()

general function for plotting

plot_2D_point_cells()

for plotting point cells

plot_2D_cell_morphology()

for plotting cells with detailed morphologies

Parameters:
  • offset ([float, float, float]) – offset for cell

  • cell (neuroml.Cell) – cell to plot

  • segment_groups (list(SegmentGroup)) – list of unbranched segment groups to plot

  • labels (bool) – toggle labelling of segment groups

  • width (float) – width for lines for segment groups

  • verbose (bool) – show extra information (default: False)

  • title (str) – title of plot

  • nogui (bool) – toggle if plot should be shown or not

  • current_scene (SceneCanvas) – vispy SceneCanvas to use (a new one is created if it is not provided)

  • current_view (ViewBox) – vispy viewbox to use

  • theme (str) – theme to use (light/dark)

pyneuroml.plot.PlotMorphology.plot_interactive_3D(nml_file: str, min_width: float = 0.8, verbose: bool = False, plot_type: str = 'constant', title: str | None = None, theme: str = 'light', nogui: bool = False)#

Plot interactive plots in 3D using Vispy

https://vispy.org

Parameters:
  • nml_file (str) – path to NeuroML cell file

  • min_width (float) – minimum width for segments (useful for visualising very thin segments): default 0.8um

  • verbose (bool) – show extra information (default: False)

  • plot_type (str) –

    type of plot, one of:

    • ”detailed”: show detailed morphology taking into account each segment’s width

    • ”constant”: show morphology, but use constant line widths

    • ”schematic”: only plot each unbranched segment group as a straight line, not following each segment

    This is only applicable for neuroml.Cell cells (ones with some morphology)

  • title (str) – title of plot

  • theme (str) – theme to use (light/dark)

  • nogui (bool) – toggle showing gui (for testing only)

pyneuroml.plot.PlotMorphology.plot_segment_groups_curtain_plots(cell: Cell, segment_groups: List[SegmentGroup], labels: bool = False, verbose: bool = False, nogui: bool = False, save_to_file: str | None = None, overlay_data: Dict[str, List[Any]] = None, overlay_data_label: str = '', width: float | int = 4, colormap_name: str = 'viridis', title: str = 'SegmentGroup', datamin: float | None = None, datamax: float | None = None, close_plot: bool = False) None#

Plot curtain plots of provided segment groups.

New in version 1.0.0.

Parameters:
  • cell (neuroml.Cell) – cell to plot

  • segment_groups (list(SegmentGroup)) – list of unbranched segment groups to plot

  • labels (bool) – toggle labelling of segment groups

  • verbose (bool) – show extra information (default: False)

  • nogui (bool) – do not show matplotlib GUI (default: false)

  • save_to_file (str) – optional filename to save generated morphology to

  • overlay_data (dict, keys are segment group ids, values are lists of magnitudes to overlay on curtain plots) – data to overlay over the curtain plots; this must be a dictionary with segment group ids as keys, and lists of values to overlay as values. Each list should have a value for every segment in the segment group.

  • overlay_data_label (str) – label of data being overlaid

  • width (float/int) – width of each segment group

  • colormap_name (str) – name of matplotlib colourmap to use for data overlay See: https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.colormaps Note: random colours are used for each segment if no data is to be overlaid

  • title (str) – plot title, displayed at bottom

  • datamin (float) – min limits of data (useful to compare different plots)

  • datamax (float) – max limits of data (useful to compare different plots)

  • close_plot (bool) – call pyplot.close() to close plot after plotting

Returns:

None

Raises:
  • ValueError – if keys in overlay_data do not match ids of segment groups of segment_groups

  • ValueError – if number of items for each key in overlay_data does not match the number of segments in the corresponding segment group