Graph Classes

BaseGraph(edges[, nodes, node_layout, ...])

The Graph base class.

Graph(graph[, edge_cmap])

Parses the given graph data object and initialises the BaseGraph object.

InteractiveGraph(*args, **kwargs)

Extends the Graph class to support node placement with the mouse, emphasis of graph elements when hovering over them, and toggleable annotations.

EditableGraph(*args, **kwargs)

Extends InteractiveGraph to support adding, deleting, and editing graph elements interactively.

class netgraph.BaseGraph(edges, nodes=None, node_layout='spring', node_layout_kwargs=None, node_shape='o', node_size=3.0, node_edge_width=0.5, node_color='w', node_edge_color='#2c404c', node_alpha=1.0, node_zorder=2, node_labels=False, node_label_offset=(0.0, 0.0), node_label_fontdict=None, edge_width=1.0, edge_color='#2c404c', edge_alpha=0.5, edge_zorder=1, arrows=False, edge_layout='straight', edge_layout_kwargs=None, edge_labels=False, edge_label_position=0.5, edge_label_rotate=True, edge_label_fontdict=None, origin=(0.0, 0.0), scale=(1.0, 1.0), prettify=True, ax=None, *args, **kwargs)[source]

The Graph base class.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

nodeslist or None, default None

List of nodes. Required argument if any node in the graph is unconnected. If None, nodes is initialised to the set of the flattened edges.

node_layoutstr or dict, default ‘spring’

If node_layout is a string, the node positions are computed using the indicated method:

  • ‘random’ : place nodes in random positions;

  • ‘circular’ : place nodes regularly spaced on a circle;

  • ‘spring’ : place nodes using a force-directed layout (Fruchterman-Reingold algorithm);

  • ‘dot’ : place nodes using the Sugiyama algorithm; the graph should be directed and acyclic;

  • ‘radial’ : place nodes radially using the Sugiyama algorithm; the graph should be directed and acyclic;

  • ‘community’ : place nodes such that nodes belonging to the same community are grouped together;

  • ‘bipartite’ : place nodes regularly spaced on two parallel lines;

  • ‘multipartite’ : place nodes regularly spaced on several parallel lines;

  • ‘shell’ : place nodes regularly spaced on concentric circles;

  • ‘geometric’ : place nodes according to the length of the edges between them.

If node_layout is a dict, keys are nodes and values are (x, y) positions.

node_layout_kwargsdict or None, default None

Keyword arguments passed to node layout functions. See the documentation of the following functions for a full description of available options:

  • get_random_layout

  • get_circular_layout

  • get_fruchterman_reingold_layout

  • get_sugiyama_layout

  • get_radial_tree_layout

  • get_community_layout

  • get_bipartite_layout

  • get_multipartite_layout

  • get_shell_layout

  • get_geometric_layout

node_shapestr or dict, default ‘o’

Node shape. If the type is str, all nodes have the same shape. If the type is dict, maps each node to an individual string representing the shape. The string specification is as for matplotlib.scatter marker, i.e. one of ‘so^>v<dph8’.

node_sizefloat or dict, default 3.

Node size (radius). If the type is float, all nodes will have the same size. If the type is dict, maps each node to an individual size.

Note

Values are rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

node_edge_widthfloat or dict, default 0.5

Line width of node marker border. If the type is float, all nodes have the same line width. If the type is dict, maps each node to an individual line width.

Note

Values are rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

node_colormatplotlib color specification or dict, default ‘w’

Node color. If the type is a string or RGBA array, all nodes have the same color. If the type is dict, maps each node to an individual color.

node_edge_colormatplotlib color specification or dict, default {DEFAULT_COLOR}

Node edge color. If the type is a string or RGBA array, all nodes have the same edge color. If the type is dict, maps each node to an individual edge color.

node_alphascalar or dict, default 1.

Node transparency. If the type is a float, all nodes have the same transparency. If the type is dict, maps each node to an individual transparency.

node_zorderint or dict, default 2

Order in which to plot the nodes. If the type is an int, all nodes have the same zorder. If the type is dict, maps each node to an individual zorder.

node_labelsbool or dict, (default False)

If False, the nodes are unlabelled. If True, the nodes are labelled with their node IDs. If the node labels are to be distinct from the node IDs, supply a dictionary mapping nodes to node labels. Only nodes in the dictionary are labelled.

node_label_offset: float or tuple, default (0., 0.)

A (dx, dy) tuple specifies the exact offset from the node position. If a single scalar delta is specified, the value is interpreted as a distance, and the label is placed delta away from the node position while trying to reduce node/label, node/edge, and label/label overlaps.

node_label_fontdictdict

Keyword arguments passed to matplotlib.text.Text. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • size (adjusted to fit into node artists if offset is (0, 0))

  • horizontalalignment (default here: ‘center’)

  • verticalalignment (default here: ‘center’)

  • clip_on (default here: False)

  • zorder (default here: inf)

edge_widthfloat or dict, default 1.

Width of edges. If the type is a float, all edges have the same width. If the type is dict, maps each edge to an individual width.

Note

Value is rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

edge_colormatplotlib color specification or dict, default {DEFAULT_COLOR}

Edge color. If the type is a string or RGBA array, all edges have the same color. If the type is dict, maps each edge to an individual color.

edge_alphafloat or dict, default 1.

The edge transparency, If the type is a float, all edges have the same transparency. If the type is dict, maps each edge to an individual transparency.

edge_zorderint or dict, default 1

Order in which to plot the edges. If the type is an int, all nodes have the same zorder. If the type is dict, maps each node to an individual zorder. If None, the edges will be plotted in the order they appear in ‘adjacency’. Hint: graphs typically appear more visually pleasing if darker edges are plotted on top of lighter edges.

arrowsbool, default False

If True, draw edges with arrow heads.

edge_layoutstr or dict (default ‘straight’)

If edge_layout is a string, determine the layout internally:

  • ‘straight’ : draw edges as straight lines

  • ‘curved’ : draw edges as curved splines; the spline control points are optimised to avoid other nodes and edges

  • ‘arc’ : draw edges as arcs with a fixed curvature

  • ‘bundled’ : draw edges as edge bundles

If edge_layout is a dict, the keys are edges and the values are edge paths in the form iterables of (x, y) tuples, the edge segments.

edge_layout_kwargsdict, default None

Keyword arguments passed to edge layout functions. See the documentation of the following functions for a full description of available options:

  • get_straight_edge_paths

  • get_curved_edge_paths

  • get_bundled_edge_paths

edge_labelsbool or dict, default False

If False, the edges are unlabelled. If True, the edges are labelled with their edge IDs. If the edge labels are to be distinct from the edge IDs, supply a dictionary mapping edges to edge labels. Only edges in the dictionary are labelled.

edge_label_positionfloat, default 0.5

Relative position along the edge where the label is placed.

  • head : 0.

  • centre : 0.5

  • tail : 1.

edge_label_rotatebool, default True

If True, edge labels are rotated such that they have the same orientation as their edge. If False, edge labels are not rotated; the angle of the text is parallel to the axis.

edge_label_fontdictdict

Keyword arguments passed to matplotlib.text.Text. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • horizontalalignment (default here: ‘center’),

  • verticalalignment (default here: ‘center’)

  • clip_on (default here: False),

  • bbox (default here: dict(boxstyle=’round’, ec=(1.0, 1.0, 1.0), fc=(1.0, 1.0, 1.0)),

  • zorder (default here: inf),

  • rotation (determined by edge_label_rotate argument)

origintuple, default (0., 0.)

The lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1., 1.)

The width and height of the bounding box specifying the extent of the canvas.

prettifybool, default True

If True, despine and remove ticks and tick labels. Set figure background to white. Set axis aspect to equal.

axmatplotlib.axis instance or None, default None

Axis to plot onto; if none specified, one will be instantiated with plt.gca().

Attributes:
node_artistsdict

Mapping of node IDs to matplotlib PathPatch artists.

edge_artistsdict

Mapping of edge IDs to matplotlib PathPatch artists.

node_label_artistsdict

Mapping of node IDs to matplotlib text objects (if applicable).

edge_label_artistsdict

Mapping of edge IDs to matplotlib text objects (if applicable).

node_positionsdict node(x, y) tuple

Mapping of node IDs to node positions.

class netgraph.Graph(graph, edge_cmap='RdGy', *args, **kwargs)[source]

Parses the given graph data object and initialises the BaseGraph object.

If the given graph includes edge weights, then these are mapped to colors using the edge_cmap parameter.

Parameters:
graphvarious formats

Graph object to plot. Various input formats are supported. In order of precedence:

  • Edge list: Iterable of (source, target) or (source, target, weight) tuples, or equivalent (E, 2) or (E, 3) ndarray, where E is the number of edges.

  • Adjacency matrix: Full-rank (V, V) ndarray, where V is the number of nodes/vertices. The absence of a connection is indicated by a zero.

    Note

    If V <= 3, any (2, 2) or (3, 3) matrices will be interpreted as edge lists.**

  • networkx.Graph, igraph.Graph, or graph_tool.Graph object

node_layoutstr or dict, default ‘spring’

If node_layout is a string, the node positions are computed using the indicated method:

  • ‘random’ : place nodes in random positions;

  • ‘circular’ : place nodes regularly spaced on a circle;

  • ‘spring’ : place nodes using a force-directed layout (Fruchterman-Reingold algorithm);

  • ‘dot’ : place nodes using the Sugiyama algorithm; the graph should be directed and acyclic;

  • ‘radial’ : place nodes radially using the Sugiyama algorithm; the graph should be directed and acyclic;

  • ‘community’ : place nodes such that nodes belonging to the same community are grouped together;

  • ‘bipartite’ : place nodes regularly spaced on two parallel lines;

  • ‘multipartite’ : place nodes regularly spaced on several parallel lines;

  • ‘shell’ : place nodes regularly spaced on concentric circles;

  • ‘geometric’ : place nodes according to the length of the edges between them.

If node_layout is a dict, keys are nodes and values are (x, y) positions.

node_layout_kwargsdict or None, default None

Keyword arguments passed to node layout functions. See the documentation of the following functions for a full description of available options:

  • get_random_layout

  • get_circular_layout

  • get_fruchterman_reingold_layout

  • get_sugiyama_layout

  • get_radial_tree_layout

  • get_community_layout

  • get_bipartite_layout

  • get_multipartite_layout

  • get_shell_layout

  • get_geometric_layout

node_shapestr or dict, default ‘o’

Node shape. If the type is str, all nodes have the same shape. If the type is dict, maps each node to an individual string representing the shape. The string specification is as for matplotlib.scatter marker, i.e. one of ‘so^>v<dph8’.

node_sizefloat or dict, default 3.

Node size (radius). If the type is float, all nodes will have the same size. If the type is dict, maps each node to an individual size.

Note

Values are rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

node_edge_widthfloat or dict, default 0.5

Line width of node marker border. If the type is float, all nodes have the same line width. If the type is dict, maps each node to an individual line width.

node_colormatplotlib color specification or dict, default ‘w’

Node color. If the type is a string or RGBA array, all nodes have the same color. If the type is dict, maps each node to an individual color.

node_edge_colormatplotlib color specification or dict, default DEFAULT_COLOR

Node edge color. If the type is a string or RGBA array, all nodes have the same edge color. If the type is dict, maps each node to an individual edge color.

node_alphascalar or dict, default 1.

Node transparency. If the type is a float, all nodes have the same transparency. If the type is dict, maps each node to an individual transparency.

node_zorderint or dict, default 2

Order in which to plot the nodes. If the type is an int, all nodes have the same zorder. If the type is dict, maps each node to an individual zorder.

node_labelsbool or dict, (default False)

If False, the nodes are unlabelled. If True, the nodes are labelled with their node IDs. If the node labels are to be distinct from the node IDs, supply a dictionary mapping nodes to node labels. Only nodes in the dictionary are labelled.

node_label_offset: float or tuple, default (0., 0.)

A (dx, dy) tuple specifies the exact offset from the node position. If a single scalar delta is specified, the value is interpreted as a distance, and the label is placed delta away from the node position while trying to reduce node/label, node/edge, and label/label overlaps.

node_label_fontdictdict

Keyword arguments passed to matplotlib.text.Text. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • size (adjusted to fit into node artists if offset is (0, 0))

  • horizontalalignment (default here: ‘center’)

  • verticalalignment (default here: ‘center’)

  • clip_on (default here: False)

  • zorder (default here: inf)

edge_widthfloat or dict, default 1.

Width of edges. If the type is a float, all edges have the same width. If the type is dict, maps each edge to an individual width.

Note

Value is rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

edge_cmapmatplotlib color map (default ‘RdGy’)

Color map used to map edge weights to edge colors. Should be diverging. If edge weights are strictly positive, weights are mapped to the left hand side of the color map with vmin=0 and vmax=np.max(weights). If edge weights are positive and negative, then weights are mapped to colors such that a weight of zero corresponds to the center of the color map; the boundaries are set to +/- the maximum absolute weight. If the graph is unweighted or the edge colors are specified explicitly, this parameter is ignored.

edge_colormatplotlib color specification or dict, default DEFAULT_COLOR

Edge color. If provided explicitly, overrides edge_cmap. If the type is a string or RGBA array, all edges have the same color. If the type is dict, maps each edge to an individual color.

edge_alphafloat or dict, default 1.

The edge transparency, If the type is a float, all edges have the same transparency. If the type is dict, maps each edge to an individual transparency.

edge_zorderint or dict, default 1

Order in which to plot the edges. If the type is an int, all nodes have the same zorder. If the type is dict, maps each node to an individual zorder. If None, the edges will be plotted in the order they appear in ‘adjacency’. Hint: graphs typically appear more visually pleasing if darker edges are plotted on top of lighter edges.

arrowsbool, default False

If True, draw edges with arrow heads.

edge_layoutstr or dict (default ‘straight’)

If edge_layout is a string, determine the layout internally:

  • ‘straight’ : draw edges as straight lines

  • ‘curved’ : draw edges as curved splines; the spline control points are optimised to avoid other nodes and edges

  • ‘bundled’ : draw edges as edge bundles

If edge_layout is a dict, the keys are edges and the values are edge paths in the form iterables of (x, y) tuples, the edge segments.

edge_layout_kwargsdict, default None

Keyword arguments passed to edge layout functions. See the documentation of the following functions for a full description of available options: - get_straight_edge_paths - get_curved_edge_paths - get_bundled_edge_paths

edge_labelsbool or dict, default False

If False, the edges are unlabelled. If True, the edges are labelled with their edge IDs. If the edge labels are to be distinct from the edge IDs, supply a dictionary mapping edges to edge labels. Only edges in the dictionary are labelled.

edge_label_positionfloat, default 0.5

Relative position along the edge where the label is placed.

  • head : 0.

  • centre : 0.5

  • tail : 1.

edge_label_rotatebool, default True

If True, edge labels are rotated such that they have the same orientation as their edge. If False, edge labels are not rotated; the angle of the text is parallel to the axis.

edge_label_fontdictdict

Keyword arguments passed to matplotlib.text.Text. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • horizontalalignment (default here: ‘center’),

  • verticalalignment (default here: ‘center’)

  • clip_on (default here: False),

  • bbox (default here: dict(boxstyle=’round’, ec=(1.0, 1.0, 1.0), fc=(1.0, 1.0, 1.0)),

  • zorder (default here: inf),

  • rotation (determined by edge_label_rotate argument)

origintuple, default (0., 0.)

The lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1., 1.)

The width and height of the bounding box specifying the extent of the canvas.

prettifybool, default True

If True, despine and remove ticks and tick labels. Set figure background to white. Set axis aspect to equal.

axmatplotlib.axis instance or None, default None

Axis to plot onto; if none specified, one will be instantiated with plt.gca().

Attributes:
node_artistsdict

Mapping of node IDs to matplotlib PathPatch artists.

edge_artistsdict

Mapping of edge IDs to matplotlib PathPatch artists.

node_label_artistsdict

Mapping of node IDs to matplotlib text objects (if applicable).

edge_label_artistsdict

Mapping of edge IDs to matplotlib text objects (if applicable).

node_positionsdict node(x, y) tuple

Mapping of node IDs to node positions.

class netgraph.InteractiveGraph(*args, **kwargs)[source]

Extends the Graph class to support node placement with the mouse, emphasis of graph elements when hovering over them, and toggleable annotations.

  • Nodes can be selected and dragged around with the mouse.

  • Nodes and edges are emphasized when hovering over them.

  • Supports additional annotations that can be toggled on and off by clicking on the corresponding node or edge.

  • These annotations can also be tables.

Parameters:
graphvarious formats

Graph object to plot. Various input formats are supported. In order of precedence:

  • Edge list: Iterable of (source, target) or (source, target, weight) tuples, or equivalent (E, 2) or (E, 3) ndarray, where E is the number of edges.

  • Adjacency matrix: Full-rank (V, V) ndarray, where V is the number of nodes/vertices. The absence of a connection is indicated by a zero.

    Note

    If V <= 3, any (2, 2) or (3, 3) matrices will be interpreted as edge lists.

  • networkx.Graph, igraph.Graph, or graph_tool.Graph object

node_layoutstr or dict, default ‘spring’

If node_layout is a string, the node positions are computed using the indicated method:

  • ‘random’ : place nodes in random positions;

  • ‘circular’ : place nodes regularly spaced on a circle;

  • ‘spring’ : place nodes using a force-directed layout (Fruchterman-Reingold algorithm);

  • ‘dot’ : place nodes using the Sugiyama algorithm; the graph should be directed and acyclic;

  • ‘radial’ : place nodes radially using the Sugiyama algorithm; the graph should be directed and acyclic;

  • ‘community’ : place nodes such that nodes belonging to the same community are grouped together;

  • ‘bipartite’ : place nodes regularly spaced on two parallel lines;

  • ‘multipartite’ : place nodes regularly spaced on several parallel lines;

  • ‘shell’ : place nodes regularly spaced on concentric circles;

  • ‘geometric’ : place nodes according to the length of the edges between them.

If node_layout is a dict, keys are nodes and values are (x, y) positions.

node_layout_kwargsdict or None, default None

Keyword arguments passed to node layout functions. See the documentation of the following functions for a full description of available options:

  • get_random_layout

  • get_circular_layout

  • get_fruchterman_reingold_layout

  • get_sugiyama_layout

  • get_radial_tree_layout

  • get_community_layout

  • get_bipartite_layout

  • get_multipartite_layout

  • get_shell_layout

  • get_geometric_layout

node_shapestr or dict, default ‘o’

Node shape. If the type is str, all nodes have the same shape. If the type is dict, maps each node to an individual string representing the shape. The string specification is as for matplotlib.scatter marker, i.e. one of ‘so^>v<dph8’.

node_sizefloat or dict, default 3.

Node size (radius). If the type is float, all nodes will have the same size. If the type is dict, maps each node to an individual size.

Note

Values are rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

node_edge_widthfloat or dict, default 0.5ayout Line width of node marker border.

If the type is float, all nodes have the same line width. If the type is dict, maps each node to an individual line width.

..note:: Values are rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

node_colormatplotlib color specification or dict, default ‘w’

Node color. If the type is a string or RGBA array, all nodes have the same color. If the type is dict, maps each node to an individual color.

node_edge_colormatplotlib color specification or dict, default DEFAULT_COLOR

Node edge color. If the type is a string or RGBA array, all nodes have the same edge color. If the type is dict, maps each node to an individual edge color.

node_alphascalar or dict, default 1.

Node transparency. If the type is a float, all nodes have the same transparency. If the type is dict, maps each node to an individual transparency.

node_zorderint or dict, default 2

Order in which to plot the nodes. If the type is an int, all nodes have the same zorder. If the type is dict, maps each node to an individual zorder.

node_labelsbool or dict, (default False)

If False, the nodes are unlabelled. If True, the nodes are labelled with their node IDs. If the node labels are to be distinct from the node IDs, supply a dictionary mapping nodes to node labels. Only nodes in the dictionary are labelled.

node_label_offset: float or tuple, default (0., 0.)

A (dx, dy) tuple specifies the exact offset from the node position. If a single scalar delta is specified, the value is interpreted as a distance, and the label is placed delta away from the node position while trying to reduce node/label, node/edge, and label/label overlaps.

node_label_fontdictdict

Keyword arguments passed to matplotlib.text.Text. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • size (adjusted to fit into node artists if offset is (0, 0))

  • horizontalalignment (default here: ‘center’)

  • verticalalignment (default here: ‘center’)

  • clip_on (default here: False)

  • zorder (default here: inf)

edge_widthfloat or dict, default 1.

Width of edges. If the type is a float, all edges have the same width. If the type is dict, maps each edge to an individual width.

Note

Value is rescaled by BASE_SCALE (1e-2) to be compatible with layout routines in igraph and networkx.

edge_cmapmatplotlib color map (default ‘RdGy’)

Color map used to map edge weights to edge colors. Should be diverging. If edge weights are strictly positive, weights are mapped to the left hand side of the color map with vmin=0 and vmax=np.max(weights). If edge weights are positive and negative, then weights are mapped to colors such that a weight of zero corresponds to the center of the color map; the boundaries are set to +/- the maximum absolute weight. If the graph is unweighted or the edge colors are specified explicitly, this parameter is ignored.

edge_colormatplotlib color specification or dict, default DEFAULT_COLOR

Edge color. If provided explicitly, overrides edge_cmap. If the type is a string or RGBA array, all edges have the same color. If the type is dict, maps each edge to an individual color.

edge_alphafloat or dict, default 1.

The edge transparency, If the type is a float, all edges have the same transparency. If the type is dict, maps each edge to an individual transparency.

edge_zorderint or dict, default 1

Order in which to plot the edges. If the type is an int, all nodes have the same zorder. If the type is dict, maps each node to an individual zorder. If None, the edges will be plotted in the order they appear in ‘adjacency’. Hint: graphs typically appear more visually pleasing if darker edges are plotted on top of lighter edges.

arrowsbool, default False

If True, draw edges with arrow heads.

edge_layoutstr or dict (default ‘straight’)

If edge_layout is a string, determine the layout internally:

  • ‘straight’ : draw edges as straight lines

  • ‘curved’ : draw edges as curved splines; the spline control points are optimised to avoid other nodes and edges

  • ‘bundled’ : draw edges as edge bundles

If edge_layout is a dict, the keys are edges and the values are edge paths in the form iterables of (x, y) tuples, the edge segments.

edge_layout_kwargsdict, default None

Keyword arguments passed to edge layout functions. See the documentation of the following functions for a full description of available options:

  • get_straight_edge_paths

  • get_curved_edge_paths

  • get_bundled_edge_paths

edge_labelsbool or dict, default False

If False, the edges are unlabelled. If True, the edges are labelled with their edge IDs. If the edge labels are to be distinct from the edge IDs, supply a dictionary mapping edges to edge labels. Only edges in the dictionary are labelled.

edge_label_positionfloat, default 0.5

Relative position along the edge where the label is placed.

  • head : 0.

  • centre : 0.5

  • tail : 1.

edge_label_rotatebool, default True

If True, edge labels are rotated such that they have the same orientation as their edge. If False, edge labels are not rotated; the angle of the text is parallel to the axis.

edge_label_fontdictdict

Keyword arguments passed to matplotlib.text.Text. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • horizontalalignment (default here: ‘center’),

  • verticalalignment (default here: ‘center’)

  • clip_on (default here: False),

  • bbox (default here: dict(boxstyle=’round’, ec=(1.0, 1.0, 1.0), fc=(1.0, 1.0, 1.0)),

  • zorder (default here: inf),

  • rotation (determined by edge_label_rotate argument)

annotationsdict

Mapping of nodes or edges to strings or dictionaries, the annotations. The visibility of the annotations can be toggled on or off by clicking on the corresponding node or edge.

annotations = {
0 : ‘Normal node’,
1 : {s : ‘Less important node’, fontsize : 2},
2 : {s : ‘Very important node’, fontcolor : ‘red’},
(0, 1) : ‘Normal edge’,
(1, 2) : {s : ‘Less important edge’, fontsize : 2},
(2, 0) : {s : ‘Very important edge’, fontcolor : ‘red’},
}
annotation_fontdictdict

Keyword arguments passed to matplotlib.text.Text if only the annotation string is given. For a full list of available arguments see the matplotlib documentation. The following default values differ from the defaults for matplotlib.text.Text:

  • horizontalalignment (depends on node position or edge orientation),

  • verticalalignment (depends on node position or edge orientation),

  • clip_on (default here: False),

  • backgroundcolor (default here: ‘white’),

  • zorder (default here: inf),

tablesdict node/edgepandas dataframe

Mapping of nodes and/or edges to pandas dataframes. The visibility of the tables that can toggled on or off by clicking on the corresponding node or edge.

table_kwargsdict

Keyword arguments passed to matplotlib.pyplot.table.

origintuple, default (0., 0.)

The lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1., 1.)

The width and height of the bounding box specifying the extent of the canvas.

prettifybool, default True

If True, despine and remove ticks and tick labels. Set figure background to white. Set axis aspect to equal.

axmatplotlib.axis instance or None, default None

Axis to plot onto; if none specified, one will be instantiated with plt.gca().

See also

Graph

Notes

You must retain a reference to the plot instance! Otherwise, the plot instance will be garbage collected after the initial draw and you won’t be able to move the plot elements around.

Examples

>>> import matplotlib.pyplot as plt
>>> from netgraph import InteractiveGraph
>>> plt.ion()
>>> plot_instance = InteractiveGraph(my_graph_obj)
>>> plt.show()
Attributes:
node_artistsdict

Mapping of node IDs to matplotlib PathPatch artists.

edge_artistsdict

Mapping of edge IDs to matplotlib PathPatch artists.

node_label_artistsdict

Mapping of node IDs to matplotlib text objects (if applicable).

edge_label_artistsdict

Mapping of edge IDs to matplotlib text objects (if applicable).

node_positionsdict node(x, y) tuple

Mapping of node IDs to node positions.

class netgraph.EditableGraph(*args, **kwargs)[source]

Extends InteractiveGraph to support adding, deleting, and editing graph elements interactively.

  1. Addition and removal of nodes and edges:

  • Double clicking on two nodes successively will create an edge between them.

  • Pressing ‘insert’ or ‘+’ will add a new node to the graph.

  • Pressing ‘delete’ or ‘-’ will remove selected nodes and edges.

  • Pressing ‘@’ will reverse the direction of selected edges.

  1. Creation and editing of labels and annotations:

  • To create or edit a node or edge label, select the node (or edge) artist, press the ‘enter’ key, and type.

  • To create or edit an annotation, select the node (or edge) artist, press ‘alt’+’enter’, and type.

  • Terminate either action by pressing ‘enter’ or ‘alt’+’enter’ a second time.

See also

InteractiveGraph

Notes

When adding a new node, the properties of the last selected node will be used to style the node artist. Ditto for edges. If no node or edge has been previously selected the first created node or edge artist will be used.