Top 3 Python Tools for Stunning Network Graphs
1. NetworkX
NetworkX is a powerful library for the creation, manipulation, and study of complex networks. It provides basic visualization capabilities, which can be extended using Matplotlib.
import networkx as nx
import matplotlib.pyplot as plt
# Create a graph
G = nx.erdos_renyi_graph(30, 0.05)
# Draw the graph
nx.draw(G, with_labels=True, node_color='skyblue', node_size=30, edge_color='gray')
plt.show()
No description has been provided for this image
2. Pyvis
Pyvis is a library built on top of NetworkX that allows for interactive network visualization in web browsers. It uses the Vis.js library to create dynamic and interactive graphs.
from pyvis.network import Network
import networkx as nx
# Create a graph
G = nx.erdos_renyi_graph(30, 0.05)
# Initialize Pyvis network
net = Network(notebook=True)
# Populate the network with nodes and edges from NetworkX graph
net.from_nx(G)
# Show the network
net.show("network.html")
#clcoding.com
Warning: When cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.
network.html
3. Plotly
Plotly is a graphing library that makes interactive, publication-quality graphs online. It supports interactive network graph visualization
import plotly.graph_objects as go
import networkx as nx
# Create a graph
G = nx.random_geometric_graph(200, 0.125)
# Extract the positions of nodes
pos = nx.get_node_attributes(G, 'pos')
# Create the edges
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
edge_x.extend([x0, x1, None])
edge_y.extend([y0, y1, None])
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines')
# Create the nodes
node_x = []
node_y = []
for node in G.nodes():
x, y = pos[node]
node_x.append(x)
node_y.append(y)
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
marker=dict(
showscale=True,
colorscale='YlGnBu',
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
)))
# Combine the traces
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='Network graph made with Python',
showlegend=False,
hovermode='closest',
margin=dict(b=20, l=5, r=5, t=40),
xaxis=dict(showgrid=False, zeroline=False),
yaxis=dict(showgrid=False, zeroline=False)))
# Show the plot
fig.show()
0 Comments:
Post a Comment