简体   繁体   中英

Networkx: Raising the labels above the node using position

I am very new to networkx (just started today!): I am using these two links and replicating:

This is for creating the network

and This is for how I tried adjusting the label positions

So mine looks like this:

layout = nx.spring_layout(g,k=0.2,iterations=50)
for l in layout:  # raise text positions
    layout[l][1] += 0.5

I adjusted the 0.5 to smaller and even larger values, but nothing happens, no adjustment. No change at all. What am I doing wrong?

The two codes combined looks like this:

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")

Thank you so much in advance!!

and on a side note does anyone have a great tutorial on networkx? I have been googling and I haven't found much. And if you know of networkx tutorials that show how to build interactive networks that would be even better!

You first need to draw the graph and then add the value (or create a second variable for the position of the labels). If you read again the code for positioning the labels , you will see that they first draw the graph and then modify the layout and draw the labels.

Your code simply moves everything, ie labels and edges along the y axis. I've corrected the position of the adjustment in your code:

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)
#
# ----- removed correction
#

# 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))

# ------> and move it here
for l in layout:  # raise text positions
    layout[l][1] += 0.1  # probably small value enough
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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM