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

Network Graphs using Dash FrameWork

I’m unable to display edges and I want the Grid Lines to be removed . And the files are provided in the link . Thank You
https://drive.google.com/drive/folders/1TVXE4n3OV6ZZ3Z1V-5igC22d_UID8y8Z?usp=sharing

@teja_m1

To remove the scene (grid lines) replace the returned dict with:

return  {"data": [edge_trace, node_trace],
         "layout": go.Layout(title='Network Graph With Dash', 
                          showlegend=False, hovermode='closest',
                          scene=dict(xaxis={'visible':False},
                                     yaxis={'visible':False},
                                     zaxis={'visible':False}))}

Using my own data I was able to plot the network. That’s why I suspect that your list E is not a list of int tuples
i.e E =[(2, 5), (7, 4)…].

Could paste here a few elements in G.nodes() and E, to realize why your edges are not plotted?

G.nodes() contains list of countries provided in excel sheet , e.g brunei,china,india,indonesia while G.edges() should contain list of edges between these countries which has trade value above zero

You have in code print(E). Please paste here what is printed!!

This should be the output when the year is 1996
EdgeView([(‘china’, ‘brunei’), (‘china’, ‘cambodia’), (‘china’, ‘india’), (‘china’, ‘indonesia’), (‘china’, ‘japan’), (‘china’, ‘lao pdr’), (‘china’, ‘malaysia’), (‘china’, ‘myanmar’), (‘china’, ‘philippines’), (‘china’, ‘singapore’), (‘china’, ‘thailand’), (‘china’, ‘vietnam’), (‘brunei’, ‘india’), (‘brunei’, ‘japan’), (‘brunei’, ‘malaysia’), (‘brunei’, ‘philippines’), (‘brunei’, ‘singapore’), (‘brunei’, ‘thailand’), (‘cambodia’, ‘india’), (‘cambodia’, ‘indonesia’), (‘cambodia’, ‘japan’), (‘cambodia’, ‘malaysia’), (‘cambodia’, ‘philippines’), (‘cambodia’, ‘singapore’), (‘cambodia’, ‘thailand’), (‘india’, ‘indonesia’), (‘india’, ‘japan’), (‘india’, ‘lao pdr’), (‘india’, ‘malaysia’), (‘india’, ‘myanmar’), (‘india’, ‘philippines’), (‘india’, ‘singapore’), (‘india’, ‘thailand’), (‘india’, ‘vietnam’), (‘indonesia’, ‘japan’), (‘indonesia’, ‘lao pdr’), (‘indonesia’, ‘malaysia’), (‘indonesia’, ‘myanmar’), (‘indonesia’, ‘philippines’), (‘indonesia’, ‘singapore’), (‘indonesia’, ‘thailand’), (‘indonesia’, ‘vietnam’), (‘japan’, ‘lao pdr’), (‘japan’, ‘malaysia’), (‘japan’, ‘myanmar’), (‘japan’, ‘philippines’), (‘japan’, ‘singapore’), (‘japan’, ‘thailand’), (‘japan’, ‘vietnam’), (‘lao pdr’, ‘malaysia’), (‘lao pdr’, ‘philippines’), (‘lao pdr’, ‘singapore’), (‘lao pdr’, ‘thailand’), (‘malaysia’, ‘myanmar’), (‘malaysia’, ‘philippines’), (‘malaysia’, ‘singapore’), (‘malaysia’, ‘thailand’), (‘malaysia’, ‘vietnam’), (‘myanmar’, ‘philippines’), (‘myanmar’, ‘singapore’), (‘myanmar’, ‘thailand’), (‘philippines’, ‘singapore’), (‘philippines’, ‘thailand’), (‘philippines’, ‘vietnam’), (‘singapore’, ‘thailand’), (‘singapore’, ‘vietnam’), (‘thailand’, ‘vietnam’)])

To be able to plot the graph I defined it from numerical nodes, and edges, Ed, as a list of int tuples:
UPDATE: Unfortunately initially was pasted here a code without the changes that make axes invisible

import networkx as nx
import numpy as np
import json
import urllib.request
import plotly.graph_objects as go

url = "https://raw.githubusercontent.com/empet/Datasets/master/miserables.json"
#Read a fake file whose nodes we call ountries although they are not countries       
with urllib.request.urlopen(url) as url:
        jdata = json.loads(url.read().decode())

#jdata

#print (jdata.keys())

countries = [el['name'] for el in jdata['nodes']]
#countries[:5]

#Define the list of edges:

#jdata['links'][:5]

L = len(jdata['links'])
Ed = [(jdata['links'][k]['source'], jdata['links'][k]['target']) for k in range(L)]
#Ed[:5]

#E=[]

#wb=xl.load_workbook(path)
#countries=wb.sheetnames
#YRA=format(year_value)
G=nx.Graph()
for k, c in enumerate(countries):
    G.add_node(k)
"""
for k, c in enumerate(countries):
    G.add_node(k)
    #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:
                    E.append((c,cnt))
                                                        
                else:
                    continue
                                                                                
        

print(E)
d_country = {country: k for k, country in enumerate(countries)}

Ed = [(d_country[e[0]], d_country[e[1]]) for e in E]
"""
G.add_edges_from(Ed)
pos = nx.spring_layout(G,dim=3)
#nx.draw(G,with_labels=1)

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=countries, 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])
fig= {"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},
                                  scene={'xaxis': {'visible':False},
                                         'yaxis': {'visible':False},
                                         'zaxis': {'visible':False}})}




fig=go.Figure(fig)
fig

Still unresolved . But Thanks

@teja_m19

The code I pasted here works with synthetic data (I didn’t run your code because I should have installed packages I don’t work with, like openpyxl ). Perhaps your data are not read as you expect.

Thanks Man for the help and patience

@teja_m19 I repasted a slightly modified code, above https://community.plotly.com/t/network-graphs-using-dash-framework/36936/5, to see that it works with my data and as a consequence to
know that you can’t get the network plotted from a different reason. I provided a link to data, too.