paulbrodersen / netgraph

Publication-quality network visualisations in python
GNU General Public License v3.0
667 stars 40 forks source link

Redrawing Edge Labels #20

Closed mabernico closed 3 years ago

mabernico commented 3 years ago

I am working on a visualization of a network that includes moveable nodes with edge labels that are updated by streaming data. Currently I am using randint to update a pandas dataframe while I work on the plotting.

The current code can generate the nodes and allows them to move and also updates the edge labels, but it feels "clunky" and every once in a while the plot flashes the axes (which I do not want to see). I believe this is just an artifact of clearing and regenerating the plot. I can't seem to find a good hook in netgraph to simply refresh graph without doing a clear and redraw which will inevitably get worse as the network grows. Any ideas on how to make this smoother?

Here is the current code:

import pandas as pd
import matplotlib.pyplot as plt
#plt.ion()
import networkx as nx
import random as r
import netgraph
import numpy as np

#Graph creation:
G=nx.Graph(type="")

#edges automatically create nodes
df=pd.read_csv('diyNodeSet.csv')  
G = nx.from_pandas_edgelist(df, source='sdr', target='rxr', \
    create_using=nx.DiGraph)

#Create edge list from dataframe
df['xy']=list(zip(df.sdr,df.rxr))
ed=list(zip(df.br,df.pct))
el=dict(zip(df.xy,ed))

pos = nx.layout.circular_layout(G)  ##initial node placement

# drag nodes around #########
plot_instance =   netgraph.InteractiveGraph(G,  node_positions=pos, node_color='red', edge_labels=el)

#update the edge labels with random data
import threading
interval = 3

def updatePlot(oldPlot):
    nodePos=oldPlot.node_positions
    new_pct=pd.Series([r.randint(1, 100),r.randint(1, 100),r.randint(1, 100),r.randint(1, 100)], name='pct', index=[0,1,2,3])
    df.update(new_pct)
    ed=list(zip(df.br,df.pct))
    el=dict(zip(df.xy,ed))
    oldPlot.fig.clear()
    global plot_instance
    plot_instance =   netgraph.InteractiveGraph(G,  node_positions=nodePos, node_color='red', edge_labels=el)

#call update each interval    
def startTimer():
    threading.Timer(interval, startTimer).start()
    updatePlot(plot_instance)

startTimer()
paulbrodersen commented 3 years ago

Not tested (since I don't know what is in diyNodeSet.csv) but all you should need is:

def updatePlot(plot_instance):
    new_labels = ... # get your label dict
    plot_instance.draw_edge_labels(plot_instance.edge_list, new_labels, plot_instance.node_positions)
    plot_instance.fig.canvas.draw_idle()

This adds new edge labels and updates existing ones. If you want to delete edge labels, you will have to remove them explicitly. The artists are stored in a dictionary that maps edges to artists.

for edge in edge_labels_to_remove:
    plot_instance.edge_label_artists[edge].remove() # delete artist
    del plot_instance.edge_label_artists[edge] # delete reference
paulbrodersen commented 3 years ago

@mabernico Does this solve your issue? Then I will close this thread (or you can if you want to).

mabernico commented 3 years ago

@paulbrodersen Thanks for this solution! It worked like a charm and helped me see a bit more about how to hook into various layers.