K. Rahul
K. Rahul

Reputation: 23

Update the networkx plot for different edge weights using slider

I am trying to show the effect of edge weights on the structure of a networkx graph using a slider. For that I am importing networkx graph from a three column csv file with information of node1, node2, weight (0 to 1).

Initially, I was considering all the edges to construct the network and draw it. Now, I want to embed the information of weights in a slider so that when I move the slider I get only those edges whose weights are greater than the slider value. I am getting following issues:

I have tried the following code:

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

f = open("network.csv",'r')
G = nx.Graph()
net = [line.split() for line in f]
r = 0.0
for i in net:
    if float(i[6]) >= r:
        G.add_edge(i[0], i[1], weight=i[6])
pos = nx.spring_layout(G)
axnet = plt.axes([0.25, 0.2, 0.65, 0.5])
new_net = Slider(axnet, 'Cf', 0.0, 1.0, 1.0)
def update(val):
    r = new_net.val
    edges = [(i[0],i[1]) for i in net if i[6] <= r]
    G.remove_edges_from(edges)
    nx.draw_random(G)
    return()
new_net.on_changed(update)
# G.update()
# plt.clf()
plt.show()

Upvotes: 1

Views: 308

Answers (1)

VirtualScooter
VirtualScooter

Reputation: 1888

In the code below, I've generated a random network to work with.

Several points to make:

  • Removing edges from a network only works if the threshold gets higher every time. To ensure that edges get added back for lower threshold settings, I'm recreating the graph with every slider update.
  • Each edge in this network has a different weight, ranging from 0 to 29/30; so every 3% movement in the slider adds or drops one edge.
  • The pos dictionary by default uses coordinates around origin from -1 to 1.
  • I'm seeding the random library and the spring_layout, to make sure that results are identical every time.
import random
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

def remake_graph(r):
    G.clear()
    for i in net:
        if float(i[6]) >= r:
            G.add_edge(i[0], i[1], weight=i[6])

def update(val):
    r = val
    # edges = [(i[0],i[1]) for i in net if i[6] <= r]
    # G.remove_edges_from(edges)
    remake_graph(r)
    ax.clear()
    pos = nx.spring_layout(G, seed=42)
    nx.draw(G, pos, ax)
    fig.canvas.draw_idle()
    pass

random.seed(42)
nodesel = range(10)
max_edges = 30
rn = lambda : random.choice(nodesel)
net = [(rn(), rn(), 0, 0, 0, 0, i/max_edges) for i in range(max_edges)]
r = 0.0
G = nx.Graph()
remake_graph(r)

fig, ax = plt.subplots()
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, ax)
plt.subplots_adjust(bottom=0.25)
# Make a horizontal slider to control the frequency.
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
freq_slider = Slider(
    ax=axfreq,
    label='Weight treshold',
    valmin=0.0,
    valmax=1.0,
    valinit=r,
)
freq_slider.on_changed(update)
plt.show()

Upvotes: 1

Related Questions