paulbrodersen / netgraph

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

Question: support for `add_edge`? #91

Closed jamesbraza closed 1 month ago

jamesbraza commented 1 month ago

Do you offer the ability to add an edge to a Graph after its construction? I don't see anything in the docs for this.


Aside: https://stackoverflow.com/a/61459641 needs updating too I think

paulbrodersen commented 1 month ago

The class you are looking for is called EditableGraph. The class is intended for manual graph creation, which is why you can add edges by double-clicking on nodes (see second animation here). However, you can also use the _add_edge(self, source, target, edge_properties=None) method directly. Depending on your environment and matplotlib parameterisation, you may need to call graph.fig.canvas.draw() afterwards to update the figure and show the changes.

import matplotlib.pyplot as plt

from netgraph import EditableGraph

plt.ion()

graph = EditableGraph([(0, 1), (1, 2)], node_layout={0 : (0.1, 0.1), 1 : (0.9, 0.1), 2: (0.5, 0.69)}, node_labels=True)
plt.pause(3)
graph._add_edge(2, 0)
plt.pause(3)
paulbrodersen commented 1 month ago

Aside: https://stackoverflow.com/a/61459641 needs updating too I think

Thanks for the heads up. What specifically do you feel should be updated?

jamesbraza commented 1 month ago

Aside: stackoverflow.com/a/61459641 needs updating too I think

Thanks for the heads up. What specifically do you feel should be updated?

Oh sorry for not specifying. When you run it, the netgraph.draw interface no longer exists:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 13
     10 G.add_edge('3','4')
     11 G.add_edge('4','3')
---> 13 netgraph.draw(G, node_color='w', edge_color='k', edge_width=2.0, node_labels={str(ii) : str(ii) for ii in range(1,5)})
     14 plt.show()

AttributeError: module 'netgraph' has no attribute 'draw'
jamesbraza commented 1 month ago

EditableGraph._add_edge looks great, I will investigate it! Appreciate the answer, going to close this as resolved 👍

paulbrodersen commented 1 month ago

Oh sorry for not specifying. When you run it, the netgraph.draw interface no longer exists

Good catch, thanks!