paulbrodersen / netgraph

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

Can I connect two graphs and still retain their individual references? #40

Closed vabichequer closed 2 years ago

vabichequer commented 2 years ago

Hi! I am using Networkx to draw several graphs in a figure. Each graph is represented in a circular layout and may have an edge with another circle. The issue is that I can't use Networkx's union function, otherwise I'll end up with a single graph if two individual ones have an edge between them. What I wanted was to just join them with an edge but still retain their individual references, since it may be that I there is a link between one of the graphs mentiones earlier and another one in the future. The reference is important to me because I still want to be able to plot them individually in a circle layout, rather than plotting them together in a circle layout.

Cheers!

paulbrodersen commented 2 years ago

I am using Networkx to draw several graphs in a figure.

  1. Are you using networkx or netgraph for plotting?
  2. If you are using netgraph, please create a minimal, working example that demonstrates the current output. Also, add a sketch with your desired output (easiest done by drawing on paper and taking a picture with your phone). At the moment, it is unclear what the issue is.
vabichequer commented 2 years ago

Sure. Sorry, I didn't detail it enough. I am using networkx still, but I'm thinking on migrating to netgraph, if I can do what I'm asking.

This image shows what I'm trying to achieve.

The main thing is that I can't create an edge between the two graphs without turning them into only one. The only way to do this is by using the union function from networkx, which returns a new, relabeled graph, with the edge I want, but with the two graphs mixed together.

What I want to know is if netgraph is able to create edges between nodes in different graphs without mixing them together. That is, if I'm able to create an edge between two graphs and still plot them separately.

I can't create a minimal working example, I'm sorry. This is more of a feature question, I guess.

paulbrodersen commented 2 years ago

Sounds like you simply want to add an arrow to your drawing, positioned between two objects that happen to be graphs/networks. Both networkx and netgraph build on matplotlib, so you can simply add an arrow to an existing plot using matplotlib directives. With netgraph, this would look something like this:

Figure_1

import matplotlib.pyplot as plt
import networkx as nx

from netgraph import Graph
from matplotlib.patches import FancyArrow

# create four networks
networks = [nx.complete_graph(n) for n in [7, 8, 9, 10]]

# initialize a figure
fig, ax = plt.subplots()

# partition canvas into four quadrants using the origin and scale parameters
origins = [(0, 0), (0.5, 0), (0, 0.5), (0.5, 0.5)]
scale = (0.45, 0.45)

# draw each network in its own quadrant
for g, origin in zip(networks, origins):
    Graph(g, node_layout='circular', origin=origin, scale=scale, ax=ax)

# add an arrow
ax.add_patch(FancyArrow(x=0.42, y=0.52, dx=0.125, dy=-0.125, width=0.01, length_includes_head=True, color='lightgray'))

plt.show()
vabichequer commented 2 years ago

Not really. Although it seems like it, I actually want to draw an arrow between two nodes from different graphs. I figured that I could use the positional argument from the circular_layout function in networkx and use ConnectionPatch to make the connection between two subplots. I just didn't want to go through the hassle of decoding the positions. I thought that maybe networkx or netgraph had a function such as "edge between graphs" that would accept two graphs and two indexes and link them, but that doesn't seem to be the case.

Since I figured a solution, I guess we could close the issue.