paulbrodersen / netgraph

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

self-loop labeling problem using EditableGraph #84

Closed Franck-Delaplace closed 5 months ago

Franck-Delaplace commented 5 months ago

I plan to use EditableGraph of Netgraph as the foundation of my application. However, I have encountered the following issue. Let

import matplotlib.pyplot as plt
from netgraph import  EditableGraph
from numpy import array
fig,ax = plt.subplots()
editgraph = EditableGraph([(0,1),(0,2),(2,2)],
                          node_label_fontdict=dict(size=9),
                          node_labels=True,
                          edge_width=2,
                          node_layout= {0: array([0.5, 0.5]), 1: array([0.25, 0.5]), 2: array([0.75, 0.5])},
                          ax=ax)
plt.show()

If:

  1. I create a self loop on node 1 (i.e. edge (1,1) );
  2. I add a label on this loop;
  3. The label cannot be added and I have the error message below:
Initiated writing label(s).
C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\netgraph\_main.py:1082: UserWarning: Plotting of edge labels for self-loops not supported for straight edges.
Ignoring edge with label: 
  warnings.warn(msg)
Traceback (most recent call last):
  File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\matplotlib\cbook.py", line 298, in process
    func(*args, **kwargs)
  File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\netgraph\_interactive_variants.py", line 518, in _on_key_press
    self._edit_labels(event.key)
  File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\netgraph\_interactive_variants.py", line 550, in _edit_labels
    self._edit_edge_label(artist, key)
  File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\netgraph\_interactive_variants.py", line 571, in _edit_edge_label
    self._edit_text_object(self.edge_label_artists[edge], key)
                          ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
KeyError: (1, 1)

However when:

  1. I first add a label to the self loop (2,2);
  2. Then I create the self-loop (1,1);
  3. I can label the self-loop (1,1) normally !?

Moreover, I have not encountered any problems with edges that are not self-loops.

What is the problem with this example that prevents adding a label to a new self-loop? Why does labeling the existing self-loop enable the possibility to label the created self-loop? How can this issue be fixed? It would be helpful to be able to label the self-loop using EditableGraph regardless of the protocol used.

paulbrodersen commented 5 months ago

Thanks for the detailed instructions to reproduce the issue.

What is the problem with this example that prevents adding a label to a new self-loop?

When initializing a new edge, no new (empty) edge label artist was created.

Why does labeling the existing self-loop enable the possibility to label the created self-loop?

Still no idea. Your workaround should not have worked (but does also on my machine).

How can this issue be fixed?

It should now be fixed on the dev branch.

pip install https://github.com/paulbrodersen/netgraph/archive/dev.zip

I will close the issue for now, but let me know if this does not resolve the issue on your end / if you run into any further issues.

Franck-Delaplace commented 5 months ago

I have tested with this version, it works fine. Thank you.