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

barycenter_spring_layout plots everything too close in the center not allowing a clear visualization #340

Closed
Mattehub opened this issue Apr 20, 2023 · 6 comments · Fixed by #439
Labels
good first issue Good for newcomers

Comments

@Mattehub
Copy link

Mattehub commented Apr 20, 2023

H_hypergraph is a weighted hypergraph, with 9 nodes and only triangles. With this code I got a plot with all the nodes very close to each other in the center of the figure. Is there a way in which i can chose a minimum or average distance between nodes when using this layout?

xgi.draw(H_hypergraph, pos=None, ax=None, 
         dyad_color='black', dyad_lw=1.5, edge_fc=cmap, node_fc='white', 
         node_ec='grey', node_lw=1, node_size=10, max_order=4, 
         node_labels=True, hyperedge_labels=False)
@leotrs
Copy link
Collaborator

leotrs commented Apr 20, 2023

Hi @Mattehub! Thanks for your report. Could you please send us the edge list of this hypergraph so we can play around with it and see how best to help here? It would also help us if you could tell us the version of XGI you're using. Thanks!

@Mattehub
Copy link
Author

Mattehub commented May 2, 2023

Thank you very much for your answer @leotrs and sorry for the late reply. Here you can find a script and a list of links, that I use to plot a hypergraph. How can I solve the fact that the nodes collapse all very close to each others?

@leotrs
Copy link
Collaborator

leotrs commented May 3, 2023

Hi @Mattehub ! I was not able to run the code you provided, but I think I have a version that may or may not be what you are looking for. Please see the code before, which I arrived at by modifying your code. While it's not the prettiest picture, I do not see the nodes clumping together in the middle. If this works for you, great! If it does not, would you be able to provide us with an image of your output that specifically shows what is the problem you are trying to fix? Thanks!

import xgi
import numpy as np
import pickle
import operator
import matplotlib.cm as cm


# load data
with open('links_test', "rb") as f:
    links = pickle.load(f)

# set random weights
zipped = zip(links, np.random.rand(len(links)))
zipped_ord = sorted(zipped, key = operator.itemgetter(1))
weights = [tuple(link) + (weig, ) for link, weig in zipped_ord]

# create Hypergraph
H_hypergraph = xgi.Hypergraph()
H_hypergraph.add_weighted_edges_from(weights)

# compute color of all edges
h1 = [h for _, h in zipped_ord]
cmap = cm.Reds((h1 - min(h1)) / max(h1 - min(h1)), alpha=0.1)

# plot!
xgi.draw(
    H_hypergraph,
    dyad_color='black',
    dyad_lw=1.5,
    edge_fc=cmap,
    node_fc='white',
    node_ec='grey',
    node_lw=1,
    node_size=10,
    max_order=3,
    node_labels=True,
    hyperedge_labels=False,
)

download

@nwlandry
Copy link
Collaborator

nwlandry commented May 3, 2023

One more comment on this is that barycenter_spring_layout() accounts for not only node positions but edge positions. This seems to work better for sparser hypergraphs, but may lead to messier layouts for denser hypergraphs like yours. If I modify the last line to

pos = xgi.pairwise_spring_layout(H_hypergraph)
xgi.draw(
    H_hypergraph,
    pos,
    dyad_color='black',
    dyad_lw=1.5,
    edge_fc=cmap,
    node_fc='white',
    node_ec='grey',
    node_lw=1,
    node_size=10,
    max_order=3,
    node_labels=True,
    hyperedge_labels=False,
)

it looks a bit cleaner.

@maximelucas
Copy link
Collaborator

maximelucas commented May 15, 2023

Our spring layouts are based on networkx's, but networkx's have more parameters:
https://networkx.org/documentation/stable/reference/generated/networkx.drawing.layout.spring_layout.html

like k: Optimal distance between nodes.

We could have more control by allowing these parameters to be passed to our functions too and then internally to networkx's. Two options:

  • pick a few a have them as explicit arguments, like k
  • allow all of them implicitly with **kwargs.

What do you think? After deciding to implement this or nothing I think we can close the issue.

@leotrs
Copy link
Collaborator

leotrs commented May 15, 2023

I'd allow all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
4 participants