Tools module

Some useful things to tweak and reproduce the visualizations.

netwulf.tools.add_edge_label(ax, network_properties, edge, label=None, dscale=0.5, dx=0, dy=0, ha='center', va='center', **kwargs)[source]

Add a label to an edge in the drawn matplotlib axis

Parameters:
  • ax (matplotlib.Axis) – The Axis object which has been used to draw the network
  • edge (2-tuple of str or int) – The edge’s node ids
  • network_properties (dict) – The network properties which are returned from the interactive visualization.
  • label (str, default : None) – The text to write at the node’s position If None, the tuple of node ids in edge will be put there.
  • dscale (float, default : 0.5) – At which position between the two nodes to put the label (dscale = 0.0 refers to the position of node edge[0] and dscale = 1.0 refers to the position of node edge[1], so use any number between 0.0 and 1.0).
  • dx (float, default : 0.0) – Additional label offset in x-direction
  • dy (float, default : 0.0) – Additional label offset in y-direction
  • ha (str, default : 'center') – Horizontal anchor orientation of the text
  • va (str, default : 'center') – Vertical anchor orientation of the text
  • **kwargs (dict) – Additional styling arguments forwarded to Axis.text

Example

>>> netw, _ = netwulf.visualize(G)
>>> fig, ax = netwulf.draw_netwulf(netw)
>>> netwulf.add_node_label(ax,netw,0)
netwulf.tools.add_node_label(ax, network_properties, node_id, label=None, dx=0, dy=0, ha='center', va='center', **kwargs)[source]

Add a label to a node in the drawn matplotlib axis

Parameters:
  • ax (matplotlib.Axis) – The Axis object which has been used to draw the network
  • network_properties (dict) – The network properties which are returned from the interactive visualization.
  • node_id (str or int) – The focal node’s id in the network_properties dict
  • label (str, default : None) – The text to write at the node’s position If None, the value of node_id will be put there.
  • dx (float, default : 0.0) – Label offset in x-direction
  • dy (float, default : 0.0) – Label offset in y-direction
  • ha (str, default : 'center') – Horizontal anchor orientation of the text
  • va (str, default : 'center') – Vertical anchor orientation of the text
  • **kwargs (dict) – Additional styling arguments forwarded to Axis.text

Example

>>> netw, _ = netwulf.visualize(G)
>>> fig, ax = netwulf.draw_netwulf(netw)
>>> netwulf.add_node_label(ax,netw,0)
netwulf.tools.bind_properties_to_network(network, network_properties, bind_node_positions=True, bind_node_color=True, bind_node_radius=True, bind_node_stroke_color=True, bind_node_stroke_width=True, bind_link_width=True, bind_link_color=True, bind_link_alpha=True)[source]

Binds calculated positional values to the network as node attributes x and y.

Parameters:
  • network (networkx.Graph or something alike) – The network object to which the position should be bound
  • network_properties (dict) – The network properties which are returned from the interactive visualization.
  • bind_node_positions (bool (default: True)) –
  • bind_node_color (bool (default: True)) –
  • bind_node_radius (bool (default: True)) –
  • bind_node_stroke_color (bool (default: True)) –
  • bind_node_stroke_width (bool (default: True)) –
  • bind_link_width (bool (default: True)) –
  • bind_link_color (bool (default: True)) –
  • bind_link_alpha (bool (default: True)) –

Example

>>> props, _ = netwulf.visualize(G)
>>> netwulf.bind_properties_to_network(G, props)
netwulf.tools.draw_netwulf(network_properties, fig=None, ax=None, figsize=None, draw_links=True, draw_nodes=True, link_zorder=-1, node_zorder=1000)[source]

Redraw the visualization using matplotlib. Creates figure and axes if None provided. In order to add labels, check out netwulf.tools.add_node_label and netwulf.tools.add_edge_label

Parameters:
  • network_properties (dict) – The network properties which are returned from the interactive visualization.
  • fig (matplotlib.Figure, default : None) – The figure in which to draw
  • ax (matplotlib.Axes, default : None) – The Axes in which to draw
  • figsize (float, default : None) – the size of the figure in inches (sidelength of a square) if None, will be taken as the minimum of the values in matplotlib.rcParams['figure.figsize'].
  • draw_links (bool, default : True) – Whether the links should be drawn
  • draw_nodes (bool, default : True) – Whether the nodes should be drawn
Returns:

  • fig (matplotlib.Figure, default : None) – Resulting figure
  • ax (matplotlib.Axes, default : None) – Resulting axes

netwulf.tools.get_filtered_network(network, edge_weight_key=None, node_group_key=None)[source]

Get a copy of a network where the edge attribute 'weight' is set to the attribute given by the keyword edge_weight_key and the nodes are regrouped according to their node attribute provided by node_group_key.

Parameters:
  • network (networkx.Graph or alike) – The network object which is about to be filtered
  • edge_weight_key (str, default : None) – If provided, set the edge weight to the edge attribute given by edge_weight_key and delete all other edge attributes
  • node_group_key (str, default : None) – If provided, set the node 'group' attribute according to a new grouping provided by the node attribute node_group_key.
Returns:

G – A filtered copy of the original network.

Return type:

networkx.Graph or alike

netwulf.tools.node_pos(network_properties, node_id)[source]

Get the node’s position in matplotlib data coordinates.

Parameters:
  • network_properties (dict) – The network properties which are returned from the interactive visualization.
  • node_id (str or int) – The node of which to get the position
Returns:

  • x (float) – The x-position in matplotlib data coordinates
  • y (float) – The y-position in matplotlib data coordinates

Example

>>> props, _ = visualize(G)
>>> node_pos(props, 0)