Skip to content

Instantly share code, notes, and snippets.

@jiqiujia
Created August 18, 2020 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiqiujia/0a6e231bb9fbeff1277263f82c70b574 to your computer and use it in GitHub Desktop.
Save jiqiujia/0a6e231bb9fbeff1277263f82c70b574 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import matplotlib
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
matplotlib.rcParams['font.sans-serif']=['SimHei']
# Create a list of 10 nodes numbered [0, 9]
wstr = '奥利奥:0.891408,oreo:0.99299026,巧克力味:1.1276144,夹心饼干:1.1379249,早餐:1.0526645,蛋糕:0.92405164,糕点:1.1213604,休闲零食:1.082368,家庭装:0.66965526'
edges_str = """奥利奥,oreo,0.65780985
奥利奥,巧克力味,0.60445136
奥利奥,夹心饼干,0.59882
奥利奥,早餐,0.44065893
奥利奥,蛋糕,0.46414903
奥利奥,糕点,0.4855876
奥利奥,休闲零食,0.47012872
奥利奥,家庭装,0.26005575
oreo,奥利奥,0.65780985
oreo,巧克力味,0.6227207
oreo,夹心饼干,0.65168345
oreo,早餐,0.56458294
oreo,蛋糕,0.49637643
oreo,糕点,0.5621315
oreo,休闲零食,0.59318155
oreo,家庭装,0.36204144
巧克力味,奥利奥,0.60445136
巧克力味,oreo,0.6227207
巧克力味,夹心饼干,0.89612067
巧克力味,早餐,0.68159616
巧克力味,蛋糕,0.5001422
巧克力味,糕点,0.7029004
巧克力味,休闲零食,0.8459676
巧克力味,家庭装,0.3763494
夹心饼干,奥利奥,0.59882
夹心饼干,oreo,0.65168345
夹心饼干,巧克力味,0.89612067
夹心饼干,早餐,0.6911473
夹心饼干,蛋糕,0.5098166
夹心饼干,糕点,0.7697738
夹心饼干,休闲零食,0.842823
夹心饼干,家庭装,0.32875672
早餐,奥利奥,0.44065893
早餐,oreo,0.56458294
早餐,巧克力味,0.68159616
早餐,夹心饼干,0.6911473
早餐,蛋糕,0.6281946
早餐,糕点,0.77799106
早餐,休闲零食,0.67655826
早餐,家庭装,0.37160566
蛋糕,奥利奥,0.46414903
蛋糕,oreo,0.49637643
蛋糕,巧克力味,0.5001422
蛋糕,夹心饼干,0.5098166
蛋糕,早餐,0.6281946
蛋糕,糕点,0.7314884
蛋糕,休闲零食,0.45881543
蛋糕,家庭装,0.35862136
糕点,奥利奥,0.4855876
糕点,oreo,0.5621315
糕点,巧克力味,0.7029004
糕点,夹心饼干,0.7697738
糕点,早餐,0.77799106
糕点,蛋糕,0.7314884
糕点,休闲零食,0.764642
糕点,家庭装,0.3977269
休闲零食,奥利奥,0.47012872
休闲零食,oreo,0.59318155
休闲零食,巧克力味,0.8459676
休闲零食,夹心饼干,0.842823
休闲零食,早餐,0.67655826
休闲零食,蛋糕,0.45881543
休闲零食,糕点,0.764642
休闲零食,家庭装,0.3454593
家庭装,奥利奥,0.26005575
家庭装,oreo,0.36204144
家庭装,巧克力味,0.3763494
家庭装,夹心饼干,0.32875672
家庭装,早餐,0.37160566
家庭装,蛋糕,0.35862136
家庭装,糕点,0.3977269
家庭装,休闲零食,0.3454593"""
g = nx.Graph()
nodes = []
node_sizes = []
labels = {}
for nw in wstr.split(','):
n, w = nw.split(':')
w = float(w[:5])
nodes.append(n)
node_sizes.append( 500 * w )
labels[n] = nw
# Node sizes: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900]
# Connect each node to its successor
edges = []
for edge_str in edges_str.split('\n'):
na, nb, w = edge_str.split(',')
g.add_edge(na, nb, weight=float(w[:5]))
# Create the graph and draw it with the node labels
g.add_nodes_from(nodes)
plt.figure(1, figsize=(10, 10))
pos = nx.spring_layout(g, k=0.3*1/np.sqrt(len(g.nodes())), iterations=20)
nx.draw(g, pos=pos, node_size = node_sizes, labels=labels, with_labels=True)
edge_labels = nx.get_edge_attributes(g,'weight')
nx.draw_networkx_edge_labels(g,pos,edge_labels=edge_labels)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment