paulbrodersen / netgraph

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

Issue with Node-Edge Connection when using Custom Shapes #63

Closed hdahmou closed 1 year ago

hdahmou commented 1 year ago

Hi there,

I am facing an issue with node-edge connection when using custom shapes. Although I have tried to increase the shape's size, there is still a gap between the triangle and the link connected to it. I have attached an image below for reference.

output

I have also attached the code snippet and nodes positions that I have used to generate the graph:

  import networkx as nx
  import netgraph

  g = nx.Graph()
  g.add_edges_from([[0, 1], [1, 2], [1, 3]])

  I = netgraph.Graph(g, 
        edge_layout='curved',
        node_layout="spring",
        node_shape={0:"o", 1:"s", 2:"^", 3:"o"})
I.node_positions
>>> {0: array([0.05, 0.79835102]), 1: array([0.50335189, 0.6716986 ]), 2: array([0.41813053, 0.20164898]), 3: array([0.95, 0.79835102])}

Could you please help me resolve this issue?

Thank you for your time and attention to this matter. Sincerely, Hicham

hdahmou commented 1 year ago

Hi,

I think I know what's the problem, I believe that it might be related to the offset used when drawing edges. In the current implementation, the offset is set as the size of the node, which works well when using circular shapes because the distance from the edge of the circle to the center of the shape is the same, regardless of the angle.

However, when using "non-uniform" shapes like triangles, the offset should depend on the angle. For non-directed graphs, a quick workaround is to reduce the offset, for example, with node_size[target] * 0.5. However, this solution might not work with directed graphs as the arrow may get hidden by the node. Additionally, it may cause strange artifacts when using small nodes and thick edges.

paulbrodersen commented 1 year ago

Hi Hicham,

Yeah, this is a known issue. The problem is very similar to issue #37, which is about providing support for arbitrary node shapes derived from text boxes, which runs into the same sort of problems. You are also absolutely right with your diagnosis, as well as the assessment that there isn't an easy fix that doesn't cause problems in other places. In issue #37, I outlined some of the things that I believe would need to be done to properly address this problem, and I have even started to work on it on a (local) branch.

In theory, the solution is quite straightforward: simply compute the intersection between the matplotlib Path that defines the node artist and then shorten the edge accordingly. This approach works well for paths forming convex polygons. The problem that I got stuck on was how to handle paths that are Bezier curves (i.e. circles and rectangles with rounded corners, for example). Here, the path does not correspond to the outline of the patch but only defines it implicitly. Properly supporting arbitrary node shapes also raises issues in other places, e.g. with the automatic scaling of node label fontsizes.

All of this a long-winded way of saying that I am interested in properly handling arbitrary node shapes and actively working on it but the problem is complex. Since I am short on time at the moment (work is busy and I have a 9 month old baby at home), it will take me a good while (months not days) until the issue will be resolved. If you are interested in working on this in the meantime, I am happy to draw up a proper roadmap.

Thanks for chiming in on the other issue, Paul

paulbrodersen commented 1 year ago

Some progress:

test_offset_curved_edge

paulbrodersen commented 1 year ago

On the dev branch, edges now connect precisely to their targets for arbitrary node shapes. I am in the process of preparing a new major release, and it hence will be some time until dev will get merged into the master branch. To install the dev branch, use:

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