【发布时间】:2020-03-03 07:16:23
【问题描述】:
我对networkx很陌生(今天才开始!): 我正在使用这两个链接并进行复制:
This is for creating the network
和This is for how I tried adjusting the label positions
所以我的看起来像这样:
layout = nx.spring_layout(g,k=0.2,iterations=50)
for l in layout: # raise text positions
layout[l][1] += 0.5
我将 0.5 调整为更小甚至更大的值,但是没有任何反应,没有调整。完全没有变化。我做错了什么?
两个代码组合起来是这样的:
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx
import os
plt.figure(figsize=(12, 12))
df = pd.read_csv(df_path)
# 1. Create the graph
g = nx.from_pandas_edgelist(df, source='name', target='club')
# 2. Create a layout for our nodes
layout = nx.spring_layout(g,k=0.2,iterations=50)
for l in layout: # raise text positions
layout[l][1] += 0.5
# 3. Draw the parts we want
nx.draw_networkx_edges(g, layout, edge_color='#AAAAAA')
clubs = [node for node in g.nodes() if node in df.club.unique()]
size = [g.degree(node) * 80 for node in g.nodes() if node in df.club.unique()]
nx.draw_networkx_nodes(g, layout, nodelist=clubs, node_size=size, node_color='lightblue')
people = [node for node in g.nodes() if node in df.name.unique()]
nx.draw_networkx_nodes(g, layout, nodelist=people, node_size=100, node_color='#AAAAAA')
high_degree_people = [node for node in g.nodes() if node in df.name.unique() and g.degree(node) > 1]
nx.draw_networkx_nodes(g, layout, nodelist=high_degree_people, node_size=100, node_color='#fc8d62')
club_dict = dict(zip(clubs, clubs))
nx.draw_networkx_labels(g, layout, labels=club_dict)
# 4. Turn off the axis because I know you don't want it
plt.axis('off')
plt.title("Revolutionary Clubs")
非常感谢您!
顺便说一句,有人有关于networkx的精彩教程吗?我一直在谷歌搜索,但没有找到太多。如果您知道 networkx 教程,这些教程展示了如何构建交互式网络,那就更好了!
【问题讨论】:
标签: python graph label networkx sna