Plot module#

Functions for plotting graphs and graph properties.

The following features are provided:

  • basic graph plotting
  • plotting the distribution of some attribute over the graph
  • animation of some recorded activity

Content#

nngt.plot.Animation2d(source, multimeter[, …]) Class to plot the raster plot, firing-rate, and average trajectory in a 2D phase-space for a network activity.
nngt.plot.AnimationNetwork(source, network) Class to plot the raster plot, firing-rate, and space-embedded spiking activity (neurons on the graph representation flash when spiking) in time.
nngt.plot.betweenness_distribution(network) Plotting the betweenness distribution of a graph.
nngt.plot.compare_population_attributes(…) Compare node attributes between two sets of nodes.
nngt.plot.correlation_to_attribute(network, …) For each node plot the value of reference_attributes against each of the other_attributes to check for correlations.
nngt.plot.degree_distribution(network[, …]) Plotting the degree distribution of a graph.
nngt.plot.draw_network(network[, nsize, …]) Draw a given graph/network.
nngt.plot.edge_attributes_distribution(…) Return node attributes for a set of nodes.
nngt.plot.library_draw(network[, nsize, …]) Draw a given Graph using the underlying library’s drawing functions.
nngt.plot.node_attributes_distribution(…) Return node attributes for a set of nodes.
nngt.plot.palette_continuous([numbers])
nngt.plot.palette_discrete([numbers])

Details#

class nngt.plot.Animation2d(source, multimeter, start=0.0, timewindow=None, trace=5.0, x='time', y='V_m', sort_neurons=None, network=None, interval=50, vector_field=False, **kwargs)[source]#

Class to plot the raster plot, firing-rate, and average trajectory in a 2D phase-space for a network activity.

Generate a SubplotAnimation instance to plot a network activity.

Parameters:
  • source (tuple) – NEST gid of the ``spike_detector``(s) which recorded the network.
  • multimeter (tuple) – NEST gid of the ``multimeter``(s) which recorded the network.
  • timewindow (double, optional (default: None)) – Time window which will be shown for the spikes and self.second.
  • trace (double, optional (default: 5.)) – Interval of time (ms) over which the data is overlayed in red.
  • x (str, optional (default: “time”)) – Name of the x-axis variable (must be either “time” or the name of a NEST recordable in the multimeter).
  • y (str, optional (default: “V_m”)) – Name of the y-axis variable (must be either “time” or the name of a NEST recordable in the multimeter).
  • vector_field (bool, optional (default: False)) – Whether the \dot{x} and \dot{y} arrows should be added to phase space. Requires additional ‘dotx’ and ‘doty’ arguments which are user defined functions to compute the derivatives of x and x in time. These functions take 3 parameters, which are x, y, and time_dependent, where the last parameter is a list of doubles associated to recordables from the neuron model (see example for details). These recordables must be declared in a time_dependent parameter.
  • sort_neurons (str or list, optional (default: None)) – Sort neurons using a topological property (“in-degree”, “out-degree”, “total-degree” or “betweenness”), an activity-related property (“firing_rate”, ‘B2’) or a user-defined list of sorted neuron ids. Sorting is performed by increasing value of the sort_neurons property from bottom to top inside each group.
  • **kwargs (dict, optional (default: {})) – Optional arguments such as ‘make_rate’, ‘num_xarrows’, ‘num_yarrows’, ‘dotx’, ‘doty’, ‘time_dependent’, ‘recordables’, ‘arrow_scale’.
class nngt.plot.AnimationNetwork(source, network, resolution=1.0, start=0.0, timewindow=None, trace=5.0, show_spikes=False, sort_neurons=None, decimate_connections=False, interval=50, repeat=True, resting_size=None, active_size=None, **kwargs)[source]#

Class to plot the raster plot, firing-rate, and space-embedded spiking activity (neurons on the graph representation flash when spiking) in time.

Generate a SubplotAnimation instance to plot a network activity.

Parameters:
  • source (tuple) – NEST gid of the ``spike_detector``(s) which recorded the network.
  • network (SpatialNetwork) – Network embedded in space to plot the actvity of the neurons in space.
  • resolution (double, optional (default: None)) – Time resolution of the animation.
  • timewindow (double, optional (default: None)) – Time window which will be shown for the spikes and self.second.
  • trace (double, optional (default: 5.)) – Interval of time (ms) over which the data is overlayed in red.
  • show_spikes (bool, optional (default: True)) – Whether a spike trajectory should be displayed on the network.
  • sort_neurons (str or list, optional (default: None)) – Sort neurons using a topological property (“in-degree”, “out-degree”, “total-degree” or “betweenness”), an activity-related property (“firing_rate”, ‘B2’) or a user-defined list of sorted neuron ids. Sorting is performed by increasing value of the sort_neurons property from bottom to top inside each group.
  • **kwargs (dict, optional (default: {})) – Optional arguments such as ‘make_rate’, or all arguments for the nngt.plot.draw_network().
nngt.plot.betweenness_distribution(network, btype='both', weights=False, nodes=None, logx=False, logy=False, num_nbins='auto', num_ebins=None, axes=None, colors=None, norm=False, show=True, **kwargs)[source]#

Plotting the betweenness distribution of a graph.

Parameters:
  • graph (Graph or subclass) – the graph to analyze.
  • btype (string, optional (default: “both”)) – type of betweenness to display (“node”, “edge” or “both”)
  • weights (bool or str, optional (default: binary edges)) – Whether edge weights should be considered; if None or False then use binary edges; if True, uses the ‘weight’ edge attribute, otherwise uses any valid edge attribute required.
  • nodes (list or numpy.array of ints, optional (default: all nodes)) – Restrict the distribution to a set of nodes (taken into account only for the node attribute).
  • logx (bool, optional (default: False)) – use log-spaced bins.
  • logy (bool, optional (default: False)) – use logscale for the degree count.
  • num_nbins (int or ‘auto’, optional (default: ‘auto’):) – Number of bins used to sample the node distribution. Defaults to unsupervised Bayesian blocks method.
  • num_ebins (int or ‘auto’, optional (default: None):) – Number of bins used to sample the edge distribution. Defaults to max(num_edges / 500., 10) (‘auto’ method will be slow).
  • axes (list of matplotlib.axis.Axis, optional (default: new ones)) – Axes which should be used to plot the histogram, if None, new ones are created.
  • show (bool, optional (default: True)) – Show the Figure right away if True, else keep it warm for later use.
nngt.plot.compare_population_attributes(network, attributes, nodes=None, reference_nodes=None, num_bins='auto', reference_color='gray', title=None, logx=False, logy=False, show=True, **kwargs)[source]#

Compare node attributes between two sets of nodes. Since number of nodes can vary, normalized distributions are used.

Parameters:
  • network (Graph) – The graph where the nodes belong.
  • attributes (str or list) – Attributes which should be returned, among: * “betweenness” * “clustering” * “in-degree”, “out-degree”, “total-degree” * “subgraph_centrality” * “b2” (requires NEST) * “firing_rate” (requires NEST)
  • nodes (list, optional (default: all nodes)) – Nodes for which the attributes should be returned.
  • reference_nodes (list, optional (default: all nodes)) – Reference nodes for which the attributes should be returned in order to compare with nodes.
  • num_bins (int or list, optional (default: ‘auto’)) – Number of bins to plot the distributions. If only one int is provided, it is used for all attributes, otherwize a list containing one int per attribute in attributes is required. Defaults to unsupervised Bayesian blocks method.
  • logx (bool or list, optional (default: False)) – Use log-spaced bins.
  • logy (bool or list, optional (default: False)) – use logscale for the node count.
nngt.plot.correlation_to_attribute(network, reference_attribute, other_attributes, nodes=None, fig=None, title=None, show=True)[source]#

For each node plot the value of reference_attributes against each of the other_attributes to check for correlations.

Changed in version 2.0: Added fig argument.

Parameters:
  • network (Graph) – The graph where the nodes belong.
  • reference_attribute (str or array-like) – Attribute which should serve as reference, among:
    • “betweenness”
    • “clustering”
    • “in-degree”, “out-degree”, “total-degree”
    • “in-strength”, “out-strength”, “total-strength”
    • “subgraph_centrality”
    • “b2” (requires NEST)
    • “firing_rate” (requires NEST)
    • a custom array of values, in which case one entry per node in nodes is required.
  • other_attributes (str or list) – Attributes that will be compared to the reference.
  • nodes (list, optional (default: all nodes)) – Nodes for which the attributes should be returned.
  • fig (matplotlib.figure.Figure, optional (default: new Figure)) – Figure to which the plot should be added.
  • title (str, optional (default: automatic).) – Custom title, use “” to remove the automatic title.
  • show (bool, optional (default: True)) – Whether the plot should be displayed immediately.
nngt.plot.degree_distribution(network, deg_type='total', nodes=None, num_bins='doane', weights=False, logx=False, logy=False, axis=None, axis_num=None, colors=None, norm=False, show=True, title=None, **kwargs)[source]#

Plotting the degree distribution of a graph.

Parameters:
  • graph (Graph or subclass) – The graph to analyze.
  • deg_type (string or N-tuple, optional (default: “total”)) – Type of degree to consider (“in”, “out”, or “total”)
  • nodes (list or numpy.array of ints, optional (default: all nodes)) – Restrict the distribution to a set of nodes.
  • num_bins (int or N-tuple, optional (default: ‘auto’):) – Number of bins used to sample the distribution. Defaults to unsupervised Bayesian blocks method.
  • weights (bool or str, optional (default: binary edges)) – Whether edge weights should be considered; if None or False then use binary edges; if True, uses the ‘weight’ edge attribute, otherwise uses any valid edge attribute required.
  • logx (bool, optional (default: False)) – Use log-spaced bins.
  • logy (bool, optional (default: False)) – Use logscale for the degree count.
  • axis (matplotlib.axes.Axes instance, optional (default: new one)) – Axis which should be used to plot the histogram, if None, a new one is created.
  • show (bool, optional (default: True)) – Show the Figure right away if True, else keep it warm for later use.
  • **kwargs (keyword arguments for matplotlib.axes.Axes.bar().)
nngt.plot.draw_network(network, nsize='total-degree', ncolor='group', nshape='o', nborder_color='k', nborder_width=0.5, esize=1.0, ecolor='k', ealpha=0.5, max_nsize=None, max_esize=2.0, curved_edges=False, threshold=0.5, decimate_connections=None, spatial=True, restrict_sources=None, restrict_targets=None, restrict_nodes=None, restrict_edges=None, show_environment=True, fast=False, size=(600, 600), xlims=None, ylims=None, dpi=75, axis=None, colorbar=False, cb_label=None, layout=None, show=False, **kwargs)[source]#

Draw a given graph/network.

Parameters:
  • network (Graph or subclass) – The graph/network to plot.
  • nsize (float, array of float or string, optional (default: “total-degree”)) – Size of the nodes as a percentage of the canvas length. Otherwise, it can be a string that correlates the size to a node attribute among “in/out/total-degree”, “in/out/total-strength”, or “betweenness”.
  • ncolor (float, array of floats or string, optional (default: 0.5)) – Color of the nodes; if a float in [0, 1], position of the color in the current palette, otherwise a string that correlates the color to a node attribute among “in/out/total-degree”, “betweenness” or “group”.
  • nshape (char, array of chars, or groups, optional (default: “o”)) – Shape of the nodes (see Matplotlib markers). When using groups, they must be pairwise disjoint; markers will be selected iteratively from the matplotlib default markers.
  • nborder_color (char, float or array, optional (default: “k”)) – Color of the node’s border using predefined Matplotlib colors). or floats in [0, 1] defining the position in the palette.
  • nborder_width (float or array of floats, optional (default: 0.5)) – Width of the border in percent of canvas size.
  • esize (float, str, or array of floats, optional (default: 0.5)) – Width of the edges in percent of canvas length. Available string values are “betweenness” and “weight”.
  • ecolor (str, char, float or array, optional (default: “k”)) – Edge color. If ecolor=”groups”, edges color will depend on the source and target groups, i.e. only edges from and toward same groups will have the same color.
  • max_esize (float, optional (default: 5.)) – If a custom property is entered as esize, this normalizes the edge width between 0. and max_esize.
  • threshold (float, optional (default: 0.5)) – Size under which edges are not plotted.
  • decimate_connections (int, optional (default: keep all connections)) – Plot only one connection every decimate_connections. Use -1 to hide all edges.
  • spatial (bool, optional (default: True)) – If True, use the neurons’ positions to draw them.
  • restrict_sources (str, group, or list, optional (default: all)) – Only draw edges starting from a restricted set of source nodes.
  • restrict_targets (str, group, or list, optional (default: all)) – Only draw edges ending on a restricted set of target nodes.
  • restrict_nodes (str, group, or list, optional (default: plot all nodes)) – Only draw a subset of nodes.
  • restrict_edges (list of edges, optional (default: all)) – Only draw a subset of edges.
  • show_environment (bool, optional (default: True)) – Plot the environment if the graph is spatial.
  • fast (bool, optional (default: False)) – Use a faster algorithm to plot the edges. Zooming on the drawing made using this method leaves the size of the nodes and edges unchanged, it is therefore not recommended when size consistency matters, e.g. for some spatial representations.
  • size (tuple of ints, optional (default: (600,600))) – (width, height) tuple for the canvas size (in px).
  • dpi (int, optional (default: 75)) – Resolution (dot per inch).
  • axis (matplotlib axis, optional (default: create new axis)) – Axis on which the network will be plotted.
  • colorbar (bool, optional (default: False)) – Whether to display a colorbar for the node colors or not.
  • cb_label (str, optional (default: None)) – A label for the colorbar.
  • layout (str, optional (default: random or spatial positions)) – Name of a standard layout to structure the network. Available layouts are: “circular” or “random”. If no layout is provided and the network is spatial, then node positions will be used by default.
  • show (bool, optional (default: True)) – Display the plot immediately.
  • **kwargs (dict) – Optional keyword arguments including node_cmap to set the nodes colormap (default is “magma” for continuous variables and “Set1” for groups) and the boolean simple_nodes to make node plotting faster.
nngt.plot.edge_attributes_distribution(network, attributes, edges=None, num_bins='auto', logx=False, logy=False, norm=False, title=None, colors=None, show=True, **kwargs)[source]#

Return node attributes for a set of nodes.

New in version 1.0.3.

Parameters:
  • network (Graph) – The graph where the nodes belong.
  • attributes (str or list) – Attributes which should be returned (e.g. “betweenness”, “delay”, “weight”).
  • edges (list, optional (default: all edges)) – Edges for which the attributes should be returned.
  • num_bins (int or list, optional (default: ‘auto’)) – Number of bins to plot the distributions. If only one int is provided, it is used for all attributes, otherwise a list containing one int per attribute in attributes is required. Defaults to unsupervised Bayesian blocks method.
  • logx (bool or list, optional (default: False)) – Use log-spaced bins.
  • logy (bool or list, optional (default: False)) – use logscale for the node count.
nngt.plot.library_draw(network, nsize='total-degree', ncolor='group', nshape='o', nborder_color='k', nborder_width=0.5, esize=1.0, ecolor='k', ealpha=0.5, max_nsize=5.0, max_esize=2.0, curved_edges=False, threshold=0.5, decimate_connections=None, spatial=True, restrict_sources=None, restrict_targets=None, restrict_nodes=None, restrict_edges=None, show_environment=True, size=(600, 600), xlims=None, ylims=None, dpi=75, axis=None, colorbar=False, show_labels=False, layout=None, show=False, **kwargs)[source]#

Draw a given Graph using the underlying library’s drawing functions.

New in version 2.0.

Parameters:
  • network (Graph or subclass) – The graph/network to plot.
  • nsize (float, array of float or string, optional (default: “total-degree”)) – Size of the nodes as a percentage of the canvas length. Otherwise, it can be a string that correlates the size to a node attribute among “in/out/total-degree”, or “betweenness”.
  • ncolor (float, array of floats or string, optional (default: 0.5)) – Color of the nodes; if a float in [0, 1], position of the color in the current palette, otherwise a string that correlates the color to a node attribute among “in/out/total-degree”, “betweenness” or “group”.
  • nshape (char, array of chars, or groups, optional (default: “o”)) – Shape of the nodes (see Matplotlib markers). When using groups, they must be pairwise disjoint; markers will be selected iteratively from the matplotlib default markers.
  • nborder_color (char, float or array, optional (default: “k”)) – Color of the node’s border using predefined Matplotlib colors). or floats in [0, 1] defining the position in the palette.
  • nborder_width (float or array of floats, optional (default: 0.5)) – Width of the border in percent of canvas size.
  • esize (float, str, or array of floats, optional (default: 0.5)) – Width of the edges in percent of canvas length. Available string values are “betweenness” and “weight”.
  • ecolor (str, char, float or array, optional (default: “k”)) – Edge color. If ecolor=”groups”, edges color will depend on the source and target groups, i.e. only edges from and toward same groups will have the same color.
  • max_esize (float, optional (default: 5.)) – If a custom property is entered as esize, this normalizes the edge width between 0. and max_esize.
  • threshold (float, optional (default: 0.5)) – Size under which edges are not plotted.
  • decimate_connections (int, optional (default: keep all connections)) – Plot only one connection every decimate_connections. Use -1 to hide all edges.
  • spatial (bool, optional (default: True)) – If True, use the neurons’ positions to draw them.
  • restrict_sources (str, group, or list, optional (default: all)) – Only draw edges starting from a restricted set of source nodes.
  • restrict_targets (str, group, or list, optional (default: all)) – Only draw edges ending on a restricted set of target nodes.
  • restrict_nodes (str, group, or list, optional (default: plot all nodes)) – Only draw a subset of nodes.
  • restrict_edges (list of edges, optional (default: all)) – Only draw a subset of edges.
  • show_environment (bool, optional (default: True)) – Plot the environment if the graph is spatial.
  • fast (bool, optional (default: False)) – Use a faster algorithm to plot the edges. This method leads to less pretty plots and zooming on the graph will make the edges start or ending in places that will differ more or less strongly from the actual node positions.
  • size (tuple of ints, optional (default: (600, 600))) – (width, height) tuple for the canvas size (in px).
  • dpi (int, optional (default: 75)) – Resolution (dot per inch).
  • colorbar (bool, optional (default: False)) – Whether to display a colorbar for the node colors or not.
  • axis (matplotlib axis, optional (default: create new axis)) – Axis on which the network will be plotted.
  • layout (str, optional (default: library-dependent or spatial positions)) – Name of a standard layout to structure the network. Available layouts are: “circular”, “spring-block”, “random”. If no layout is provided and the network is spatial, then node positions will be used by default.
  • show (bool, optional (default: True)) – Display the plot immediately.
  • **kwargs (dict) – Optional keyword arguments including node_cmap to set the nodes colormap (default is “magma” for continuous variables and “Set1” for groups) and the boolean simple_nodes to make node plotting faster.
nngt.plot.node_attributes_distribution(network, attributes, nodes=None, num_bins='auto', logx=False, logy=False, norm=False, title=None, colors=None, show=True, **kwargs)[source]#

Return node attributes for a set of nodes.

Parameters:
  • network (Graph) – The graph where the nodes belong.
  • attributes (str or list) – Attributes which should be returned, among: * “betweenness” * “clustering” * “closeness” * “in-degree”, “out-degree”, “total-degree” * “subgraph_centrality” * “b2” (requires NEST) * “firing_rate” (requires NEST)
  • nodes (list, optional (default: all nodes)) – Nodes for which the attributes should be returned.
  • num_bins (int or list, optional (default: ‘auto’)) – Number of bins to plot the distributions. If only one int is provided, it is used for all attributes, otherwise a list containing one int per attribute in attributes is required. Defaults to unsupervised Bayesian blocks method.
  • logx (bool or list, optional (default: False)) – Use log-spaced bins.
  • logy (bool or list, optional (default: False)) – use logscale for the node count.
nngt.plot.palette_continuous(numbers=None)[source]#
nngt.plot.palette_discrete(numbers=None)[source]#