paulbrodersen / netgraph

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

Is there way to get position of node after manual tweaking? #49

Closed HGDD0 closed 2 years ago

HGDD0 commented 2 years ago

Hi,

Excellent job for the Interactivity drawing network.

In my case, I'll draw rectangles after each node to represent their attributions. So I wonder if there is a way to get node position so I can refresh the matplotlib drawing?

Like this example, I wanna move the rectangles together with nodes. jjaIsS.png

Here is my code in jupyterlab

%matplotlib widget
import matplotlib
import numpy as np
import matplotlib.pyplot as plt; plt.ion()
import networkx as nx
import netgraph

fig, axs = plt.subplots(figsize = (10,10))

nodes = np.array(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
edges = np.array([['A', 'B'], ['A', 'C'], ['B', 'D'], ['B', 'E'], ['C', 'F'], ['C', 'G']])
pos_ = np.array([[0, 0], [-2, 1], [2, 1], [-3, 2], [-1, 2], [1, 2], [3, 2]])

G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

x_move = 0
y_move = 0
block_size = 0.5
for pos in pos_:
    rect1  = matplotlib.patches.Rectangle((pos[0]+x_move,pos[1]+y_move), 0.85*block_size, 0.75*block_size, facecolor='red', edgecolor=None)
    axs.add_patch(rect1)

I = netgraph.InteractiveGraph(G,
                              node_positions=dict(zip(nodes, pos_)),
                              node_labels=dict(zip(nodes,nodes)),
                              node_label_bbox=dict(fc="lightgreen", ec="black", boxstyle="square", lw=3),
                              node_size=12,
)
paulbrodersen commented 2 years ago

Hi, sorry for the late reply, I was on holiday.

So I wonder if there is a way to get node position so I can refresh the matplotlib drawing?

They are stored as an attribute; in your example: I.node_positions.