0

Data: Currently I am working on a quite big network. Therefore I am sending a picture of a part of nodes and edges. enter image description here

pos = nx.nx_agraph.graphviz_layout(G, prog="twopi")
Xs=[a[0] for a in pos.values()]
Ys=[a[1] for a in pos.values()]

from pyvis.network import Network
g=Network(height=2000,width=2000,directed=True)
g.from_nx(G)
g.add_nodes(Nodes,x=Xs,y=Ys)
g.barnes_hut(gravity=-80000,central_gravity=0.5,spring_length=150,spring_strength=0.001,damping=0.09,overlap=0)
g.show('nx.html')
 
  • What I am trying to do is setting other edges invisible when I click on a node. For example, if I click on 'node A', I just want to see just the node A's edges.*

enter image description here

What can I do to create this?

Renat
  • 7,718
  • 2
  • 20
  • 34
Hakan Akgün
  • 872
  • 5
  • 13

1 Answers1

1

If I can understand correctly, you want to hide certain edges after selection of particular node. I do not quite understand the network image, however I am assuming there is a certain node A that you need to add separately or a list of nodes with the functionality:

Since Pyvis is build on top of Visjs these are the interaction-configurations available under the network: https://visjs.github.io/vis-network/docs/network/interaction.html

I cannot see any hide edges option. However you can use these two -

"selectConnectedEdges"
*#Boolean   *true*  
#When true, on selecting a node, its connecting edges are highlighted.*

OR

**hoverConnectedEdges** 
#Boolean    **true**    
#When true, on hovering over a node, it's connecting edges are highlighted

Working notebook - https://colab.research.google.com/drive/1S3zpSotmAhwx_8Qo81vvTk_egpNpStPu?usp=sharing

Example Here is a sample code

#!pip install pyvis
import pyvis
from pyvis import network
from pyvis.network import Network



G = Network(height='400px', width='80%', bgcolor='white', notebook=True, 
font_color ='black')
G.add_node(1)
G.add_node(2)
G.add_node(3)

G.add_edges([(1,2,4),(1,3,2),(2,3,6)])

options = {
          "nodes":{
              "font":{
                  "size": 50,
                  "bold":True
              }
          },
          "edges":{
              "color":'red',
              "smooth":False
          },
          "physics":{
              "barnesHut":{
                  "gravitationalConstant":-500000,
                  "centralGravity":12,
                  "springLength": 50,
                  "springConstant": 0.7,
                  "damping": 3,
                  "avoidOverlap": 10
              }
          },
          "interaction":{   
               "selectConnectedEdges": True

}}

G.options=options

G.show('sample.html')


import IPython
IPython.display.HTML(filename= r"/content/sample.html")
  • Hi! I'm trying something similar for changing the color of the node when I click on it: ´´´ options = { "configure": { "enabled": True }, "nodes": { "borderWidth": 2, "borderWidthSelected": 4, "chosen" : True, "color": { "highlight": { "border": "#FF4040", "background": "#EE3B3B" }, "hover": { "border": "#DEB887", "background": "#FFD39B" } } } } But it doesn't work – TomasC8 Dec 06 '22 at 20:57
  • @TomasC8 Not sure why you are supplying borderwidth attributes for nodes. Can you check that this is the attribute allowed only for edges? This function of hiding works better for edges, and node colors are specific in different key 'nodes.' – Paritosh Kulkarni Jan 17 '23 at 01:27