|
| 1 | +import networkx as nx |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +# Create a random graph with 10,000 nodes and 50,000 edges |
| 6 | +G = nx.gnm_random_graph(1000, 5000) |
| 7 | + |
| 8 | +# Set random weights for the edges |
| 9 | +weights = np.random.rand(len(G.edges)) |
| 10 | +nx.set_edge_attributes(G, dict(zip(G.edges, weights)), 'weight') |
| 11 | + |
| 12 | +# Generate a layout using the spring layout algorithm |
| 13 | +pos = nx.spring_layout(G) |
| 14 | + |
| 15 | +# Assign colors to the nodes based on their degree |
| 16 | +node_colors = np.array(list(dict(G.degree()).values())) |
| 17 | +node_colors = (node_colors - node_colors.min()) / (node_colors.max() - node_colors.min()) |
| 18 | + |
| 19 | +# Assign colors to the edges based on their weight |
| 20 | +edge_weights = nx.get_edge_attributes(G, 'weight') |
| 21 | +edge_weights_norm = (np.array(list(edge_weights.values())) - min(edge_weights.values())) / (max(edge_weights.values()) - min(edge_weights.values())) |
| 22 | +cmap = plt.cm.get_cmap('Blues') |
| 23 | +edge_colors = cmap(edge_weights_norm) |
| 24 | + |
| 25 | +# Draw the graph using the layout and color scheme |
| 26 | +nx.draw(G, pos=pos, node_color=node_colors, edge_color=edge_colors, with_labels=False, node_size=5, width=0.5) |
| 27 | + |
| 28 | +# Add a title and subtitle to the plot |
| 29 | +plt.title("Random Graph with 10,000 Nodes and 50,000 Edges") |
| 30 | +plt.suptitle("Generated using the gnm_random_graph() function from NetworkX") |
| 31 | + |
| 32 | +# Display the plot |
| 33 | +plt.show() |
0 commit comments