paulbrodersen / netgraph

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

Fixed node size in Netgraph ? #87

Closed Franck-Delaplace closed 3 months ago

Franck-Delaplace commented 4 months ago

I useNetgraph (EditableGraph) to represent networks. However, when the window is scaled, the nodes become disproportionately large compared to the labels, which remain the same size. This results in an unappealing visual representation.

How to keep the size of nodes invariant in Python, i.e. insensitive to the window size scaling ? Thank you for your answer.

paulbrodersen commented 4 months ago

Hi, thanks for raising the issue. A few questions before I dive in:

  1. What are the versions of PyQt (assuming you are still using PyQt), matplotlib, and netgraph that your using?
  2. Does the issue persist if you install netgraph from the dev branch?
pip install https://github.com/paulbrodersen/netgraph/archive/dev.zip
  1. Does the issue persist if you install the version from the dev branch AND use a different interactive backend?
Franck-Delaplace commented 4 months ago

Hi, thanks for raising the issue. A few questions before I dive in:

  1. What are the versions of PyQt (assuming you are still using PyQt), matplotlib, and netgraph that your using?
  2. Does the issue persist if you install netgraph from the dev branch?
pip install https://github.com/paulbrodersen/netgraph/archive/dev.zip
  1. Does the issue persist if you install the version from the dev branch AND use a different interactive backend?
  1. I use pyqt5 5.15.10, matplotlib 3.8.2, netgraph 4.12.10
  2. The font remains the same whatever the window size variation but the size of the nodes grows when the window grows. I do not know if the issue is solved since I expect a way to definitely fix the node size whatever the window size.
paulbrodersen commented 4 months ago

What about:

  1. Does the issue persist if you install the version from the dev branch AND use a different interactive backend?
Franck-Delaplace commented 4 months ago

I am sorry, I am unable to answer. I use the dev branch but I don't know what you mean when you talk about interactive backend. I am a beginner in python and netgraph.

paulbrodersen commented 4 months ago

No worries, you are doing great. You can read about matplotlib backends here.

I suspect that the issue arises because of the PyQt backend you are using in your application. I would like you to test a minimum working example with a different backend to confirm that suspicion. For example:

import matplotlib
matplotlib.use('tkagg')

import matplotlib.pyplot as plt
from netgraph import InteractiveGraph

g = InteractiveGraph([(0, 1)], node_labels=True)
plt.show()

# increase the size of the window
Franck-Delaplace commented 4 months ago

I finally understand your question my backend is "Qt5Agg". You can find the whole application dedicated to genetic network analysis where I use EditGraph there : https://github.com/Franck-Delaplace/BooN

paulbrodersen commented 4 months ago

Have you tried running the code snippet?

import matplotlib
matplotlib.use('tkagg')

import matplotlib.pyplot as plt
from netgraph import InteractiveGraph

g = InteractiveGraph([(0, 1)], node_labels=True)
plt.show()

# increase the size of the window
Franck-Delaplace commented 4 months ago

Not yet, I am too busy, I will try soon  Best Franck

Franck Delaplace, PhD. Le 12 mars 2024 à 12:23 +0100, Paul Brodersen @.***>, a écrit :

Have you tried running the code snippet? import matplotlib matplotlib.use('tkagg')

import matplotlib.pyplot as plt from netgraph import InteractiveGraph

g = InteractiveGraph([(0, 1)], node_labels=True) plt.show()

increase the size of the window

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

Franck-Delaplace commented 4 months ago

I have finally tested the program, The size of the nodes grows as well as the size of the font.

Le mar. 12 mars 2024 à 11:58, Paul Brodersen @.***> a écrit :

No worries, you are doing great. You can read about matplotlib backends here https://matplotlib.org/stable/users/explain/figure/backends.html.

I suspect that the issue arises because of the PyQt backend you are using in your application. I would like you to test a minimum working example with a different backend to confirm that suspicion. For example:

import matplotlib matplotlib.use('tkagg')

import matplotlib.pyplot as plt from netgraph import InteractiveGraph

g = InteractiveGraph([(0, 1)], node_labels=True) plt.show()

increase the size of the window

— Reply to this email directly, view it on GitHub https://github.com/paulbrodersen/netgraph/issues/87#issuecomment-1991366725, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6RWCKKRVRMG3F2TDDQKNZLYX3NV5AVCNFSM6AAAAABEJKVJL6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJRGM3DMNZSGU . You are receiving this because you authored the thread.Message ID: @.***>

-- Franck Delaplace, Ph. D.

Franck-Delaplace commented 4 months ago

However if I change the backend i.e. matplotlib.use("Qt5Agg") I have the same behavior as using 'tkagg'. The size of the nodes and the font grow.

Le mar. 12 mars 2024 à 12:23, Paul Brodersen @.***> a écrit :

Have you tried running the code snippet?

import matplotlib matplotlib.use('tkagg')

import matplotlib.pyplot as plt from netgraph import InteractiveGraph

g = InteractiveGraph([(0, 1)], node_labels=True) plt.show()

increase the size of the window

— Reply to this email directly, view it on GitHub https://github.com/paulbrodersen/netgraph/issues/87#issuecomment-1991423405, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6RWCKMMGVJLVCHR647MFL3YX3QSPAVCNFSM6AAAAABEJKVJL6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJRGQZDGNBQGU . You are receiving this because you authored the thread.Message ID: @.***>

-- Franck Delaplace, Ph. D.

paulbrodersen commented 4 months ago

So you no longer observe the behaviour you originally reported?

However, when the window is scaled, the nodes become disproportionately large compared to the labels, which remain the same size.

Franck-Delaplace commented 4 months ago

In my application yes, but not in your example. However, in my application the font size is fixed as parameters.

Franck Delaplace, PhD. Le 25 mars 2024 à 12:08 +0100, Paul Brodersen @.***>, a écrit :

So you no longer observe the behaviour you originally reported?

However, when the window is scaled, the nodes become disproportionately large compared to the labels, which remain the same size. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

paulbrodersen commented 4 months ago

Well, if you keep the font-sized fixed, then any zoom event will result in a mismatch between node sizes and font sizes. There isn't much that can be done then.

paulbrodersen commented 4 months ago

There isn't much that can be done then.

On further reflection, it might be doable. The basic strategy is the following:

1) Keep track of node sizes in display units.

node_size_in_data_units = [node : artist.size for node, artist in my_graph_instance["node_artists"].items()]
transform = ... # TODO: https://matplotlib.org/stable/users/explain/artists/transforms_tutorial.html
node_size_in_display_units = [node :  transform(size) for node, size for node_size_in_data_units.items()]

2) Add a callback, such that on every zoom event, you compute the new node size(s) in display units. Compute the ratio between old and new node sizes.

3) Redraw the graph with the new node sizes: my_graph_instance = EditableGraph(..., node_size=old_node_size * ratio, ...)

Franck-Delaplace commented 4 months ago

Based on this code closed to yours since I simply add node_label_offset=(0., 0.05)

from netgraph import InteractiveGraph
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
g = InteractiveGraph([(0, 1)], node_labels=True, node_label_offset=(0., 0.05))
plt.show()

In this case, the font size remains constant even when the window is enlarged. However, if I replace (0., 0.05)with (0., 0.), the font size increases proportionally with the window size again. I think that the origin comes from the position of the node label when it is not at the center of the node. Please test it.

paulbrodersen commented 4 months ago

What you describe is the intended behaviour: if node labels fill the node artists, then upon zoom events, node label font sizes are adjusted. If the labels are outside the node artists, the font sizes are not adjusted.

Franck-Delaplace commented 4 months ago

Thank you, I do not know this element. However it's annoying.

Le mer. 27 mars 2024 à 10:55, Paul Brodersen @.***> a écrit :

What you describe is the intended behaviour: if node labels fill the node artists, then upon zoom events, node label font sizes are adjusted. If the labels are outside the node artists, the font sizes are not adjusted.

— Reply to this email directly, view it on GitHub https://github.com/paulbrodersen/netgraph/issues/87#issuecomment-2022350051, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6RWCKMR3O5GLNBOTGSCXDDY2KCQHAVCNFSM6AAAAABEJKVJL6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRSGM2TAMBVGE . You are receiving this because you authored the thread.Message ID: @.***>

-- Franck Delaplace, Ph. D.

paulbrodersen commented 3 months ago

Closing the issue for now, as most of your problems seem to stem from intended behaviour, not a bug. If I have misunderstood the issue(s), or you have any other questions on this specific topic, feel free to re-open.