Skip to content

Instantly share code, notes, and snippets.

@zhicongchen
Last active May 12, 2022 16:41
Show Gist options
  • Save zhicongchen/83db96e25aa7920ce1bc990705b33f7f to your computer and use it in GitHub Desktop.
Save zhicongchen/83db96e25aa7920ce1bc990705b33f7f to your computer and use it in GitHub Desktop.
def plotDegreeDistribution(G):
from collections import defaultdict
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
degs = defaultdict(int)
for i in G.degree().values(): degs[i]+=1
items = sorted ( degs.items () )
x, y = np.array(items).T
y = [float(i) / sum(y) for i in y]
plt.plot(x, y, 'bo')
plt.xscale('log')
plt.yscale('log')
plt.legend(['Degree'])
plt.xlabel('$K$', fontsize = 20)
plt.ylabel('$P_K$', fontsize = 20)
plt.title('$Degree\,Distribution$', fontsize = 20)
plt.show()
# 绘制中文网络
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(10,10))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, nodelist=None, node_size=100, node_color='r', node_shape='o', alpha=1.0, cmap=None, vmin=None, vmax=None, ax=None, linewidths=None, label=None)
nx.draw_networkx_edges(G, pos, edgelist=None, width=1.0, edge_color='k', style='solid', alpha=None, edge_cmap=None, edge_vmin=None, edge_vmax=None, ax=None, arrows=True, label=None)
nx.draw_networkx_labels(G, pos, labels=None, font_size=12, font_color='k', font_family='sans-serif', font_weight='normal', alpha=1.0, ax=None)
edge_labels = nx.get_edge_attributes(G,'times')
nx.draw_networkx_edge_labels(G, pos, edge_labels=None, label_pos=0.5, font_size=10, font_color='k', font_family='sans-serif', font_weight='normal', alpha=1.0, bbox=None, ax=None, rotate=True)
plt.axis('off')
plt.show()
g_df = raw_df[['E_ORG_NAME', 'FUND_NAME', 'DONATE_RECVED_MONEY']].dropna()
E_ORG_list = [i for i in g_df['E_ORG_NAME']]
FUND_NAME_list = [i for i in g_df['FUND_NAME']]
MONEY_list = [i for i in g_df['DONATE_RECVED_MONEY']]
# Build Graph
G = nx.Graph()
for i in range(len(g_df)):
# G.add_edge(E_ORG_list[i], FUND_NAME_list[i], weight = MONEY_list[i]/10000) # 无法收敛
G.add_edge(E_ORG_list[i], FUND_NAME_list[i])
h, a = nx.hits(G, 1000)
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))
path = '/home/sysadmin/zhicongchen/MSYH.TTC'
fontprop = font_manager.FontProperties(fname=path)
sorted_values = sorted(a.values(), reverse=True)[:10]
for i in a:
if a[i] in sorted_values:
if i in FUND_NAME_list:
# plot scatter at (a[i], h[i])
ax.scatter(a[i], h[i])
# plot text beside the scatter
ax.text(a[i], h[i]-random.random()*0.04*max(sorted_values), i, fontproperties=fontprop, fontsize = 14)
ax.set_xlabel(u'权威性', fontproperties=fontprop, fontsize = 16)
ax.set_ylabel(u'中心性', fontproperties=fontprop, fontsize =16)
ax.set_title(u'基金名称', fontproperties=fontprop, fontsize = 18)
# plt.xscale('log')
# plt.yscale('log')
# plt.xlim(0, max(a.values()))
# plt.ylim(0, max(h.values()))
plt.show()
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))
path = '/home/sysadmin/zhicongchen/MSYH.TTC'
fontprop = font_manager.FontProperties(fname=path)
sorted_values = sorted(a.values(), reverse=True)[:10]
for i in a:
if a[i] in sorted_values:
if i in E_ORG_list:
ax.scatter(a[i], h[i])
ax.text(a[i], h[i], i, fontproperties=fontprop, fontsize = 12)
ax.set_xlabel(u'权威性', fontproperties=fontprop, fontsize = 16)
ax.set_ylabel(u'中心性', fontproperties=fontprop, fontsize =16)
ax.set_title(u'发起方名称', fontproperties=fontprop, fontsize=18)
# plt.xscale('log')
# plt.yscale('log')
# plt.xlim(0, max(a.values()))
# plt.ylim(0, max(h.values()))
plt.show()
# community detection
# https://python-louvain.readthedocs.io/en/latest/
# build the network, nodes are category, edges are correlations
category_correlations = pd.read_excel('./Category Correlations.xlsx', sheet_name='daily')
G = nx.Graph()
for i,j,k in category_correlations.values:
if abs(k) > 0.5:
G.add_edge(i,j,weight=abs(k))
fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(121)
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos=pos, with_labels=True, linewidths=2, edge_color='grey', font_color='purple')
# better with karate_graph() as defined in networkx example.
# erdos renyi don't have true community structure
# G = nx.karate_graph(30, 0.05)
# G = nx.karate_club_graph()
ax = fig.add_subplot(122)
# first compute the best partition
partition = community.best_partition(G)
# drawing
size = len(set(partition.values()))
color=iter(plt.cm.rainbow(np.linspace(0,1,size)))
# pos = nx.spring_layout(G)
count = 0.
for com in set(partition.values()) :
c = next(color)
count = count + 1.
list_nodes = [nodes for nodes in partition.keys()
if partition[nodes] == com]
nx.draw_networkx_nodes(G, pos, list_nodes, node_color = c)
nx.draw_networkx_labels(G, pos, font_size=10, font_color='purple')
nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.tight_layout()
plt.show()
@joelmiller
Copy link

joelmiller commented Jul 3, 2017

It would be better to use a counter rather than a defaultdict.

c = Counter(G.degree().values())
x, y =  zip(*sorted(c.items()))

replaces

degs = defaultdict(int)
for i in G.degree().values(): degs[i]+=1
items = sorted ( degs.items () )
x, y = np.array(items).T

@zhicongchen
Copy link
Author

Good advice. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment