import matplotlib.pyplot as plt
import networkx as nx
G = nx.grid_2d_graph(5, 5)
options = {
'node_color': 'red',
'node_size': 800,
'width': 3,
'with_labels': True,
'font_weight': 'bold',
}
nx.draw(G, **options)
G = nx.cycle_graph(24)
pos = nx.spring_layout(G, iterations=200)
nx.draw(G, pos, node_color=range(24), node_size=800, cmap=plt.cm.PiYG)
DG = nx.DiGraph()
DG.add_nodes_from(['A', 'B', 'C', 'D', 'E', 'F'])
DG.add_edge('A', 'B', weight=4)
DG.add_edge('A', 'C', weight=2)
DG.add_edge('B', 'C', weight=5)
DG.add_edge('B', 'D', weight=10)
DG.add_edge('C', 'E', weight=3)
DG.add_edge('E', 'D', weight=4)
DG.add_edge('D', 'F', weight=11)
weights = list(nx.get_edge_attributes(DG,'weight').values())
pos=nx.circular_layout(DG)
colors = range(20)
options = {
"node_color": "#A0CBE2",
"edge_color": weights,
"width": 4,
"edge_cmap": plt.cm.PiYG,
"with_labels": True,
"node_size": 800
}
nx.draw(DG, pos, **options)
labels = nx.get_edge_attributes(DG,'weight')
nx.draw_networkx_edge_labels(DG,nx.circular_layout(DG),edge_labels=labels)
{('A', 'B'): Text(0.7499999888241293, 0.433012741974907, '4'), ('A', 'C'): Text(0.24999998137354879, 0.4330127121725851, '2'), ('B', 'C'): Text(-2.9802321943606103e-08, 0.8660254296429223, '5'), ('B', 'D'): Text(-0.24999999627470976, 0.4330126982635194, '10'), ('C', 'E'): Text(-0.49999996274709757, -5.297752070365647e-09, '3'), ('D', 'F'): Text(-0.2500000260770317, -0.43301273667715495, '11'), ('E', 'D'): Text(-0.7499999292194854, -0.43301273667715495, '4')}
from operator import itemgetter
G = nx.generators.barabasi_albert_graph(1000, 2)
node_and_degree = G.degree()
largest_hub, degree = sorted(node_and_degree, key=itemgetter(1))[-1]
hub_ego = nx.ego_graph(G, largest_hub)
pos = nx.spring_layout(hub_ego)
nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)
options = {"node_size": 300, "node_color": "r"}
nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
<matplotlib.collections.PathCollection at 0x167750df0>
G = nx.random_geometric_graph(200, 0.125)
pos = nx.get_node_attributes(G, "pos")
# find node near center (0.5,0.5)
dmin = 1
ncenter = 0
for n in pos:
x, y = pos[n]
d = (x - 0.5) ** 2 + (y - 0.5) ** 2
if d < dmin:
ncenter = n
dmin = d
p = dict(nx.single_source_shortest_path_length(G, ncenter))
sizes = []
for size in p.values():
sizes.append(size*10)
plt.figure(figsize=(8, 8))
nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
nx.draw_networkx_nodes(
G,
pos,
nodelist=list(p.keys()),
node_size=sizes,
node_color=list(p.values()),
cmap=plt.cm.PiYG,
)
<matplotlib.collections.PathCollection at 0x167792190>
G = nx.generators.directed.random_k_out_graph(10, 3, 0.5)
pos = nx.layout.spring_layout(G)
M = G.number_of_edges()
edge_colors = range(2, M + 2)
edge_alphas = [(5 + i) / (M + 4) for i in range(M)]
nodes = nx.draw_networkx_nodes(G, pos, node_size=600, node_color="blue")
edges = nx.draw_networkx_edges(
G,
pos,
arrowstyle="->",
arrowsize=10,
edge_color=edge_colors,
edge_cmap=plt.cm.Blues,
width=2,
)
for i in range(M):
edges[i].set_alpha(edge_alphas[i])
import plotly.graph_objects as go
G = nx.random_geometric_graph(200, 0.125)
pos = nx.layout.spring_layout(G)
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines')
node_x = []
node_y = []
for node in G.nodes():
x, y = G.nodes[node]['pos']
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 options
#'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
#'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
#'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
colorscale='YlGnBu',
reversescale=True,
color=[],
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
),
line_width=2))
node_adjacencies = []
node_text = []
for node, adjacencies in enumerate(G.adjacency()):
node_adjacencies.append(len(adjacencies[1]))
node_text.append('# of connections: '+str(len(adjacencies[1])))
node_trace.marker.color = node_adjacencies
node_trace.text = node_text
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='<br>Network graph made with Python',
titlefont_size=16,
showlegend=False,
hovermode='closest',
margin=dict(b=20,l=5,r=5,t=40),
annotations=[ dict(
text="Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>",
showarrow=False,
xref="paper", yref="paper",
x=0.005, y=-0.002 ) ],
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
)
fig.show()