Get the same shape of graph with Networkx in Python with and without arrows #5101
-
|
I wrote a code that plots a network with Networkx lib in Python. My code works good, but I'm confused with one thing. I get totally different looks of the graphs when I use .Graph() and .DiGraph(). Is there a chance to get the same shape of the graph with and without arrows? You can see the graph images here: |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
One way I found that was able to fix your issue is doing that if analysis_graph.is_directed():
my_pos = nx.spring_layout(analysis_graph.to_undirected(), seed=0)
else:
my_pos = nx.spring_layout(analysis_graph, seed=0)That way you always compute the undirected graph's positions. Other than that, you can store the Finally, you can use another layout that isn't producing random positions. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for raising this @aleksandar-devedzic , this is an interesting corner case. I agree with @z3y50n 's answer, though I wonder if the conversion to undirected graphs should be added to I'm tempted to convert this to an issue and see if it can't be "fixed". Thoughts? |
Beta Was this translation helpful? Give feedback.
-
|
A simple solution is to always use DiGraphs, and specify the import networkx as nx
from matplotlib import pyplot as plt
g = nx.DiGraph([(1, 2), (2, 3)])
pos = nx.spring_layout(g)
_, (ax0, ax1) = plt.subplots(2, 1)
nx.draw(g, ax=ax0, pos=pos, arrows=True)
nx.draw(g, ax=ax1, pos=pos, arrows=False)
plt.savefig('arrow.png') |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, you cen move this to fixed.
if analysis_graph.is_directed():
my_pos = nx.spring_layout(analysis_graph.to_undirected(), seed=0)
else:
my_pos = nx.spring_layout(analysis_graph, seed=0)
суб, 25. сеп 2021. у 18:58 Ross Barnowski ***@***.***> је
написао/ла:
… Thanks for raising this @aleksandar-devedzic
<https://github.com/aleksandar-devedzic> , this is an interesting corner
case.
I agree with @z3y50n <https://github.com/z3y50n> 's answer, though I
wonder if the conversion to undirected graphs should be added to
spring_layout itself. In this case, I think the difference in positions
between directed/undirected graphs is due to the fact that the
Fruchterman-Reingold functions convert to adjacency matrices under the hood
(either scipy.sparse or numpy depending on what's installed). This affects
the calculation, even though in principle I don't think FR should be
sensitive to this.
I'm tempted to convert this to an issue and see if it can't be "fixed".
Thoughts?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5101 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ATCV65PUW6TDTKO2DXLYWYDUDX5SLANCNFSM5EXODCBQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.

One way I found that was able to fix your issue is doing that
That way you always compute the undirected graph's positions.
Other than that, you can store the
my_posobject and pass it to both cases.Finally, you can use another layout that isn't producing random positions.