NetworkX

NetworkX#

The following methods are provided for converting and managing NetworkX graph data within PyGraphistry. g.compute_networkx(...) exposes the same curated NetworkX algorithm subset as GFQL local Cypher CALL graphistry.nx.*: pagerank, betweenness_centrality, degree_centrality, closeness_centrality, eigenvector_centrality, katz_centrality, connected_components, strongly_connected_components, core_number, hits, edge_betweenness_centrality, and k_core.

Install with pygraphistry[networkx] or pygraphistry[networkx-scipy]. For an executable notebook walkthrough, see NetworkX.

Node algorithm example:

g2 = g.compute_networkx("degree_centrality", out_col="degree_score")
assert "degree_score" in g2._nodes.columns

Edge algorithm example:

g2 = g.compute_networkx("edge_betweenness_centrality", out_col="edge_bc", directed=False)
assert "edge_bc" in g2._edges.columns

Graph-returning algorithm example:

g2 = g.compute_networkx("k_core", params={"k": 2}, directed=False)

Result shape:

  • Node algorithms append one or more columns to g._nodes.

  • Edge algorithms append one column to g._edges.

  • k_core returns a projected PyGraphistry graph.

  • hits writes hubs and authorities and does not accept out_col.

  • connected_components uses weak components when directed=True and connected components when directed=False.

graphistry.PlotterBase.PlotterBase.compute_networkx(self, alg, out_col=None, params=None, directed=True, G=None)

Run a supported NetworkX algorithm and return an enriched PyGraphistry graph.

Parameters:
  • alg (str) – Explicitly supported NetworkX algorithm name.

  • out_col (Optional[str]) – Optional output column name for single-column node/edge algorithms.

  • params (Optional[Mapping[str, Any]]) – Keyword parameters forwarded to the selected NetworkX algorithm.

  • directed (bool) – Whether to build a directed NetworkX graph from the current graph.

  • G (Optional[Any]) – Optional prebuilt NetworkX graph to run instead of converting self.

  • self (Plottable)

Returns:

Plotter

Return type:

Plotter

Example: Degree centrality
g2 = g.compute_networkx("degree_centrality", out_col="degree_score")
Example: HITS
g2 = g.compute_networkx("hits")
assert "hubs" in g2._nodes.columns
assert "authorities" in g2._nodes.columns
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)
Parameters:

G (Any)

Return type:

Tuple[DataFrame, DataFrame]

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

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

Parameters:

g (Any)

Return type:

None