Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fit/scale an InteractiveGraph to its matplotlib axe #42

Closed
Thylowz opened this issue May 17, 2022 · 4 comments
Closed

Fit/scale an InteractiveGraph to its matplotlib axe #42

Thylowz opened this issue May 17, 2022 · 4 comments

Comments

@Thylowz
Copy link

Thylowz commented May 17, 2022

First, sorry if raising an issue is not the proper way to log my question.

I have a PyQt app where I draw graphs with various number of nodes.

After determining the nodes positions (in my case using nx.shell_layout(G)), I draw an InteractiveGraph (thanks to your advice on a SO question I opened a few days ago) and it works fine.

However, I can see that the graph is not "adjusted" to the matplotlib canvas it is attached to. It seems to me that netgraph (or networkx) auto determines a "frame" depending on the number of nodes it has to display. So when you click/move the nodes, they are not "displayed" if you moved outside of this frame, which is smaller than my overall matplotlib size.

So I would like to know if there is a way to adjust this frame (or box) so that its limits fit the one of my matplotlib canvas?

@paulbrodersen
Copy link
Owner

It seems to me that netgraph (or networkx) auto determines a "frame" depending on the number of nodes it has to display.

Netgraph doesn't (or shouldn't) but it is built on matplotlib, and matplotlib by default adjusts the axis extents to the data limits of the objects plotted. As with any other matplotlib figure, you can set the x- and y-limits explicitly:

import matplotlib.pyplot as plt
import networkx as nx
from netgraph import InteractiveGraph

fig, ax = plt.subplots()
G = nx.complete_graph(10)
pos = nx.shell_layout(G)
ref = InteractiveGraph(G, node_layout=pos, ax=ax)
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
plt.show()

Figure_1

Does this solve your issue?

@Thylowz
Copy link
Author

Thylowz commented May 18, 2022

As always, thank you for your (quick) answer.

I can see that it works, I just have to figure out how to properly set it to the matplotlib limits (there is currently still some margin). I initialized my Figure to figsize=(5, 3) so maybe I have to dig with this. All these features is just some extensive stuff I have added to my work but is not my core knowledge so it's not very intuitive to me.

By the way, setting the axe xlim/ylim impacted my graph "size", everything got way smaller than it was. Any clue why it did?

@paulbrodersen
Copy link
Owner

I just have to figure out how to properly set it to the matplotlib limits (there is currently still some margin).

In matplotlib, all axes have a margin by default. You can adjust/remove the margins with:

fig, ax = plt.subplots()
fig.subplots_adjust(left=0, bottom=0, right=1, top=1)

By the way, setting the axe xlim/ylim impacted my graph "size", everything got way smaller than it was. Any clue why it did?

This is the expected behaviour, as in netgraph, all object sizes are given in data coordinates (times a constant factor of 0.01). This is in contrast to networkx, where node sizes and edge widths are in display coordinates.

@Thylowz
Copy link
Author

Thylowz commented May 18, 2022

Thanks for your explanation.

I already set plot with the subplots_adjust method.
It turns out that I can adjust as I wish by using xlim/ylim appropriately.

I've also adjusted origin and scale to it so I'm pretty satisfied with it, I have way less coordinates issues than previously.
Thank you very much for the cool library and how easy it was to get info.

I guess you can close the issue, since behaviour seems to be pretty much what I wanted!

@Thylowz Thylowz closed this as completed May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants