Black Lives Matter. Please consider donating to Black Girls Code today.

Network Graphs using plotly

I’m unable to get network graph for a call back in my web app which changes according to slider

Here’s the code
def update_graph(year_value):

    path="C:/Users/ilike/Desktop/aseann.xlsx"
    wb=xl.load_workbook(path)
    countries=wb.sheetnames
    YRA=format(year_value)
    G=nx.Graph()
    pos=nx.spring_layout(G,dim=3)
    for c in countries:
                    ws=wb[c]
                    
                    max_r=ws.max_row
                    max_c=ws.max_column
                    for r in range(4,max_r,3):
                                    yr_ob=ws.cell(row=r,column=1)
                                    yr=yr_ob.value
                                    if yr==YRA:
                                                    for cl in range(2,max_c):
                                                                    cob=ws.cell(row=3,column=cl)
                                                                    cnt=cob.value
                                                                    cov=ws.cell(row=r,column=cl)
                                                                    ov=cov.value
                                                                    if ov!=0 and ov!=None:
                                                                                    G.add_edge(c,cnt)
                                                    
                                                                    else:
                                                                            continue
                                                                            
    



    nx.draw(G,with_labels=1)
    pos=nx.spring_layout(G,dim=3)
    edge_trace=go.Scatter3d(x=[],y=[],z=[],line={'width':0.5,'color':'#888'},hoverinfo='none',mode='lines')
    for edge in G.edges():
            x0,y0,z0=pos.get(edge[0])
            x1,y1,z1=pos.get(edge[1])
            edge_trace['x']+=tuple([x0,x1,None])
            edge_trace['y']+=tuple([y0,y1,None])
            edge_trace['z']+=tuple([z0,z1,None])
    node_trace = go.Scatter3d(x=[], y=[], text=[], mode='markers', hoverinfo='text') 
    for node in G.nodes():
             x,y,z=pos.get(node)
             node_trace['x']+=tuple([x])
             node_trace['y']+=tuple([y])
             node_trace['z']+=tuple([z])
    return{"data":[edge_trace, node_trace],
          "layout": go.Layout(title='Network Graph With Dash', showlegend=False, hovermode='closest',
                              margin={'b': 20, 'l': 5, 'r': 5, 't': 40},
                              xaxis={'showgrid': False, 'zeroline': False, 'showticklabels': False},
                              yaxis={'showgrid': False, 'zeroline': False, 'showticklabels': False})}

if name == ‘main’:

            app.run_server(debug=True)

Hi @teja_m19, what is the issue with this app? Are you able to display the figure? Do you get an error? Please provide more details and/or attach the whole app code, ideally hosting your data file somewhere so that we can reproduce.

The program should display network graphs when user changes the slider according to the year . But the graph isn’t getting displayed . .
The project file :- https://drive.google.com/drive/folders/1TVXE4n3OV6ZZ3Z1V-5igC22d_UID8y8Z?usp=sharing

where [year_wise.py] is the program for displaying network graphs according to the excel sheet [aseann.xlsx]

and the graph should be displayed in Main File [nx.py] i.e dash file

THANKS

@teja_m19

The network isn’t displayed because in node_trace, which should be an instance of go.Scatter3d, z isn’t initialized:

 node_trace = go.Scatter3d(x=[], y=[], text=[], mode='markers', hoverinfo='text') 

Usually you should get an error message after this line:
node_trace['z']+=tuple([z])
The right definition of node_trace:

node_trace = go.Scatter3d(x=[], y=[], z=[],text=[], mode='markers', hoverinfo='text') 

Done but still incomplete. Thanks ! :slight_smile:

@teja_m19

I detected the error running this code:

import networkx as nx
import numpy as np
import json
import requests
import plotly.graph_objects as go

content = requests.get("https://raw.githubusercontent.com/plotly/datasets/master/miserables.json")
jdata = json.loads(content.content)


N = len(jdata['nodes'])
L = len(jdata['links'])
E = [(jdata['links'][k]['source'], jdata['links'][k]['target']) for k in range(L)]

V = np.arange(N)

G = nx.Graph()
G.add_nodes_from(V)
G.add_edges_from(E)

pos = nx.spring_layout(G,dim=3)
edge_trace = go.Scatter3d(x=[],y=[],z=[],line={'width':0.5,'color':'#888'},hoverinfo='none',mode='lines')
for edge in G.edges():
    x0,y0,z0=pos.get(edge[0])
    x1,y1,z1=pos.get(edge[1])
    edge_trace['x']+=tuple([x0,x1,None])
    edge_trace['y']+=tuple([y0,y1,None])
    edge_trace['z']+=tuple([z0,z1,None])
node_trace = go.Scatter3d(x=[], y=[], z=[], text=[], mode='markers', hoverinfo='text') 
for node in G.nodes():
    x, y, z=pos.get(node)
    node_trace['x']+=tuple([x])
    node_trace['y']+=tuple([y])
    node_trace['z']+=tuple([z])
#    return{"data":[edge_trace, node_trace],
fig= go.Figure(data=[edge_trace, node_trace])

fig.update_layout(width=800, height=800, title='Network Graph With Dash', showlegend=False, hovermode='closest',
                              margin={'b': 20, 'l': 5, 'r': 5, 't': 40},
                              xaxis={'showgrid': False, 'zeroline': False, 'showticklabels': False},
                              yaxis={'showgrid': False, 'zeroline': False, 'showticklabels': False})


Maybe your data used to define the graph are incomplete or contain nans.

1 Like

unable to get edges . I am newly using plot . It doesnt display edges . Thanks for the help