pythonmatplotlibnetworkxnetgraph

Python Netgraph Matplotlib Refresh Plot


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). Is 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. Anyone know how I can 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()

Here is a snip of the desired appearance: enter image description here


Solution

  • Here is a response from the author of Netgraph (here) which avoids redrawing the plot and removes the ticks from appearing:

    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