NetworkX Methods

NetworkX Methods#

The following methods are provided for converting and managing NetworkX graph data within PyGraphistry.

graphistry.PlotterBase.PlotterBase.from_networkx(self, G)

Convert a NetworkX graph to a PyGraphistry graph.

This method takes a NetworkX graph and converts it into a format that PyGraphistry can use for visualization. It extracts the node and edge data from the NetworkX graph and binds them to the graph object for further manipulation or visualization using PyGraphistry’s API.

Parameters:

G (networkx.Graph or networkx.DiGraph) – The NetworkX graph to convert.

Returns:

A PyGraphistry Plottable object with the node and edge data from the NetworkX graph.

Return type:

Plottable

Example: Basic NetworkX Conversion
import graphistry
import networkx as nx

# Create a NetworkX graph
G = nx.Graph()
G.add_nodes_from([
    (1, {"v": "one"}), 
    (2, {"v": "two"}), 
    (3, {"v": "three"}), 
    (4, {"v": "four"}), 
    (7, {"v": "seven"}), 
    (8, {"v": "eight"})
])
G.add_edges_from([
    [2, 3], 
    [3, 4], 
    [7, 8]
])

# Convert the NetworkX graph to PyGraphistry format
g = from_networkx(G)

g.plot()

This example creates a simple NetworkX graph with nodes and edges, converts it using from_networkx(), and then plots it with the PyGraphistry API.

Example: Using Custom Node and Edge Bindings
import graphistry
import networkx as nx

# Create a NetworkX graph with attributes
G = nx.Graph()
G.add_nodes_from([
    (1, {"v": "one"}), 
    (2, {"v": "two"}), 
    (3, {"v": "three"}), 
    (4, {"v": "four"}), 
    (7, {"v": "seven"}), 
    (8, {"v": "eight"})
])
G.add_edges_from([
    [2, 3], 
    [3, 4], 
    [7, 8]
])

# Bind custom node and edge names when converting from NetworkX to PyGraphistry
g = graphistry.bind(source='src', destination='dst').from_networkx(G)

g.plot()
graphistry.PlotterBase.PlotterBase.networkx2pandas(self, g)
graphistry.PlotterBase.PlotterBase.networkx_checkoverlap(self, g)

Raise an error if the node attribute already exists in the graph