Network Visualization

driada.network.drawing.draw_degree_distr(net, mode=None, cumulative=0, survival=1, log_log=0, ax=None, **kwargs)[source]

Draw the degree distribution of a network.

Visualizes the degree distribution as a histogram or line plot, with options for cumulative distributions and log-log scaling. Production-quality styling is applied automatically via make_beautiful().

Parameters:
  • net (Network) – Network object to analyze.

  • mode ({'all', 'in', 'out'} or None, optional) – Which degree type to plot. If None and graph is directed, plots all three types. Default is None.

  • cumulative (int, optional) – If 1, plot cumulative distribution. Default is 0.

  • survival (int, optional) – If 1 and cumulative=1, plot survival function (1-CDF). Default is 1.

  • log_log (int, optional) – If 1, use log-log scale. Default is 0.

  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure.

  • **kwargs – Additional styling parameters passed to make_beautiful(). Common options: - spine_width : float (default 3) - tick_width : float (default 3) - tick_labelsize : int (default 20) - label_size : int (default 24) - title_size : int (default 24) - legend_fontsize : int (default 16) - legend_loc : str (default “upper right”)

Returns:

ax – The styled axes object.

Return type:

matplotlib.axes.Axes

Notes

For directed graphs, shows in-degree, out-degree, and total degree distributions in different colors unless mode is specified. Log-log plots are useful for identifying power-law distributions. In log-log mode, the last element is excluded to avoid log(0).

Examples

>>> import matplotlib
>>> matplotlib.use('Agg')  # Use non-interactive backend for testing
>>> from driada.network import Network
>>> import networkx as nx
>>> # Create a simple directed network with a triangle
>>> edges = [(0, 1), (1, 2), (2, 0)]
>>> graph = nx.DiGraph(edges)
>>> net = Network(graph=graph)
>>> # Draw degree distribution (will show uniform degree of 2)
>>> draw_degree_distr(net)  
>>> # For undirected network
>>> graph_undir = nx.Graph(edges)
>>> net_undir = Network(graph=graph_undir)
>>> draw_degree_distr(net_undir, log_log=1)  
driada.network.drawing.draw_spectrum(net, mode='adj', ax=None, colors=None, cmap='plasma', nbins=None, **kwargs)[source]

Visualize the eigenvalue spectrum of a network matrix.

For directed graphs, plots eigenvalues in the complex plane. For undirected graphs, shows a histogram of real eigenvalues. Production-quality styling is applied automatically via make_beautiful().

Parameters:
  • net (Network) – Network object to analyze.

  • mode ({'adj', 'lap', 'nlap'}, optional) – Matrix type: ‘adj’ for adjacency, ‘lap’ for Laplacian, ‘nlap’ for normalized Laplacian. Default is ‘adj’.

  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure. Default is None.

  • colors (array-like, optional) – Colors for scatter plot points (directed graphs only). Default is None.

  • cmap (str, optional) – Colormap name. Default is ‘plasma’.

  • nbins (int, optional) – Number of histogram bins (undirected graphs only). If None, uses Sturges’ rule: ceil(log2(n)) + 1.

  • **kwargs – Additional styling parameters passed to make_beautiful(). See draw_degree_distr() docstring for common options.

Returns:

ax – The styled axes object.

Return type:

matplotlib.axes.Axes

Notes

The spectrum provides insights into network properties: - Largest eigenvalue relates to network connectivity - Spectral gap indicates mixing time and robustness - Complex eigenvalues (directed) indicate cyclic structure

Examples

>>> import matplotlib
>>> matplotlib.use('Agg')  # Use non-interactive backend for testing
>>> from driada.network import Network
>>> import networkx as nx
>>> # Create a small cycle graph
>>> graph = nx.cycle_graph(5)
>>> net = Network(graph=graph)
>>> # Draw adjacency matrix spectrum
>>> draw_spectrum(net, mode='adj')  
>>> # Draw Laplacian spectrum - eigenvalues should be non-negative
>>> draw_spectrum(net, mode='lap')  
driada.network.drawing.get_vector_coloring(vec, cmap='plasma')[source]

Create color mapping for vector values.

Maps numerical values to colors using matplotlib colormap, normalizing to [0, 1] range.

Parameters:
  • vec (array-like) – Vector of numerical values to map to colors.

  • cmap (str, optional) – Matplotlib colormap name. Default is ‘plasma’.

Returns:

Array of RGBA color values, shape (n, 4).

Return type:

numpy.ndarray

Raises:

ValueError – If all values in vec are identical (no variation to map).

Examples

>>> import numpy as np
>>> values = [0.1, 0.5, 0.9, 0.3]
>>> colors = get_vector_coloring(values, cmap='viridis')
>>> colors.shape
(4, 4)
>>> # Colors are RGBA values in range [0, 1]
>>> assert np.all(colors >= 0) and np.all(colors <= 1)
>>> # Test with uniform values - should raise error
>>> try:
...     get_vector_coloring([1.0, 1.0, 1.0])
... except ValueError as e:
...     print("Error:", str(e))
Error: All values in vector are identical, cannot create color mapping
driada.network.drawing.draw_eigenvectors(net, left_ind, right_ind, mode='adj', nodesize=None, cmap='plasma', draw_edges=True, edge_options={})[source]

Draw network nodes colored by eigenvector components.

Visualizes a network with nodes colored according to the values of eigenvectors in the specified index range. Creates a grid of subplots, one for each eigenvector from left_ind to right_ind (inclusive).

Parameters:
  • net (Network) – Network object to visualize.

  • left_ind (int) – Starting index of eigenvectors to visualize (inclusive).

  • right_ind (int) – Ending index of eigenvectors to visualize (inclusive).

  • mode (str, optional) – Matrix mode for eigendecomposition. Options: ‘adj’, ‘lap’, ‘nlap’. Default is ‘adj’.

  • nodesize (float or None, optional) – Size of nodes in the visualization. If None, uses default size. Default is None.

  • cmap (str or matplotlib colormap, optional) – Colormap for coloring nodes. Default is ‘plasma’.

  • draw_edges (bool, optional) – Whether to draw edges. Default is True.

  • edge_options (dict, optional) – Additional options for edge drawing (passed to networkx). Default is empty dict.

Returns:

Figure containing the eigenvector visualization.

Return type:

matplotlib.figure.Figure

Notes

The function creates a grid of subplots arranged to fit all requested eigenvectors. Each subplot shows the network with nodes colored by the corresponding eigenvector’s components. The subplot title shows the eigenvector index and its eigenvalue.

Examples

>>> import matplotlib
>>> matplotlib.use('Agg')  # Use non-interactive backend for testing
>>> from driada.network import Network
>>> import networkx as nx
>>> # Create a graph with interesting spectral properties
>>> graph = nx.cycle_graph(8)
>>> net = Network(graph=graph)
>>> # Visualize first 4 eigenvectors of adjacency matrix
>>> draw_eigenvectors(net, 0, 3, mode='adj')  
>>> # Visualize Laplacian eigenvectors (Fiedler vector is at index 1)
>>> draw_eigenvectors(net, 1, 2, mode='lap')  
driada.network.drawing.draw_net(net, colors=None, nodesize=None, ax=None)[source]

Visualize a network graph with customizable node properties.

Parameters:
  • net (Network) – Network object to visualize.

  • colors (array-like, optional) – Node colors. Can be a single color or array of values to be mapped to colors. Default is None.

  • nodesize (array-like, optional) – Node sizes. If None, sizes are based on node out-degree. Default is None.

  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure. Default is None.

Returns:

Displays the network visualization.

Return type:

None

Notes

If no node positions are stored in the network, uses spring layout for automatic positioning. Node sizes by default are proportional to the square root of scaled out-degree.

Examples

>>> import matplotlib
>>> matplotlib.use('Agg')  # Use non-interactive backend for testing
>>> from driada.network import Network
>>> import networkx as nx
>>> import numpy as np
>>> # Create a small network with a path
>>> edges = [(0, 1), (1, 2)]
>>> graph = nx.Graph(edges)
>>> net = Network(graph=graph, create_nx_graph=True)
>>> # Draw network with default settings
>>> draw_net(net)  
>>> # Draw with custom colors based on node index
>>> colors = [0, 0.5, 1]  # Three nodes with gradient colors
>>> draw_net(net, colors=colors)  
driada.network.drawing.show_mat(net, dtype=None, mode='adj', ax=None)[source]

Display a network matrix as a heatmap.

Parameters:
  • net (Network) – Network object containing the matrix.

  • dtype (numpy.dtype, optional) – Data type to cast matrix to before display. Useful for binary visualization. Default is None.

  • mode (str, optional) – Matrix type to display: - ‘adj’: adjacency matrix - ‘lap’/’lap_out’: Laplacian matrix - ‘nlap’: normalized Laplacian Default is ‘adj’.

  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure. Default is None.

Returns:

Displays the matrix visualization.

Return type:

None

Notes

If the requested matrix hasn’t been computed yet, it will be generated on demand. Sparse matrices are converted to dense for visualization.

Examples

>>> import matplotlib
>>> matplotlib.use('Agg')  # Use non-interactive backend for testing
>>> from driada.network import Network
>>> import networkx as nx
>>> import numpy as np
>>> # Create a small cycle graph
>>> edges = [(0, 1), (1, 2), (2, 0)]
>>> graph = nx.Graph(edges)
>>> net = Network(graph=graph)
>>> # Show adjacency matrix as binary (shows connections)
>>> show_mat(net, dtype=bool)  
>>> # Show Laplacian matrix (degree matrix minus adjacency)
>>> show_mat(net, mode='lap')  
>>> # For a directed graph
>>> digraph = nx.DiGraph(edges)
>>> net_dir = Network(graph=digraph)
>>> show_mat(net_dir, mode='adj')  
driada.network.drawing.plot_lem_embedding(net, ndim, colors=None)[source]

Plot Laplacian Eigenmaps embedding of the network.

Computes a low-dimensional embedding using Laplacian Eigenmaps and visualizes nodes in the embedded space.

Parameters:
  • net (Network) – Network object to embed.

  • ndim (int) – Number of dimensions for embedding (2 or 3).

  • colors (array-like, optional) – Node colors for visualization. Default is None.

Returns:

Displays the embedding plot.

Return type:

None

Notes

Laplacian Eigenmaps use the eigenvectors of the graph Laplacian corresponding to the smallest non-zero eigenvalues to embed nodes in a low-dimensional space while preserving local structure.

Examples

>>> import matplotlib
>>> matplotlib.use('Agg')  # Use non-interactive backend for testing
>>> from driada.network import Network
>>> import networkx as nx
>>> # Create a small network that can be well-embedded
>>> graph = nx.cycle_graph(6)
>>> net = Network(graph=graph)
>>> # Create 2D Laplacian Eigenmaps embedding
>>> plot_lem_embedding(net, ndim=2)  
>>> # Create 3D embedding for more complex visualization
>>> graph_3d = nx.complete_graph(5)
>>> net_3d = Network(graph=graph_3d)
>>> plot_lem_embedding(net_3d, ndim=3)  

Tools for visualizing networks and their properties.