[英]Plotting networkx.Graph: how to change node position instead of resetting every node?
我正在做一個項目,我需要創建一個nx.Graph()
的預覽,它允許改變節點的位置,用鼠標拖動它們。 如果單擊特定節點,我當前的代碼能夠在鼠標每次移動后立即重繪整個圖形。 但是,這會顯着增加延遲。 如何僅更新需要的藝術家,即單擊節點、其標簽文本和相鄰邊緣,而不是刷新plt.subplots()
每個藝術家? 我至少可以得到所有需要搬遷的藝術家的參考嗎?
我從在networkx
中顯示圖形的標准方式開始:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import scipy.spatial
def refresh(G):
plt.axis((-4, 4, -1, 3))
nx.draw_networkx_labels(G, pos = nx.get_node_attributes(G, 'pos'),
bbox = dict(fc="lightgreen", ec="black", boxstyle="square", lw=3))
nx.draw_networkx_edges(G, pos = nx.get_node_attributes(G, 'pos'), width=1.0, alpha=0.5)
plt.show()
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()
# IG = InteractiveGraph(G) #>>>>> add this line in the next step
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.set_node_attributes(G, dict(zip(G.nodes(), pos.astype(float))), 'pos')
fig, ax = plt.subplots()
# fig.canvas.mpl_connect('button_press_event', lambda event: IG.on_press(event))
# fig.canvas.mpl_connect('motion_notify_event', lambda event: IG.on_motion(event))
# fig.canvas.mpl_connect('button_release_event', lambda event: IG.on_release(event))
refresh(G) # >>>>> replace it with IG.refresh() in the next step
在下一步中,我更改了前一個腳本的 5 行(4 行未注釋,1 行被替換)加上使用InteractiveGraph
實例使其具有交互性:
class InteractiveGraph:
def __init__(self, G, node_pressed=None, xydata=None):
self.G = G
self.node_pressed = node_pressed
self.xydata = xydata
def refresh(self, show=True):
plt.clf()
nx.draw_networkx_labels(self.G, pos = nx.get_node_attributes(self.G, 'pos'),
bbox = dict(fc="lightgreen", ec="black", boxstyle="square", lw=3))
nx.draw_networkx_edges(self.G, pos = nx.get_node_attributes(self.G, 'pos'), width=1.0, alpha=0.5)
plt.axis('off')
plt.axis((-4, 4, -1, 3))
fig.patch.set_facecolor('white')
if show:
plt.show()
def on_press(self, event):
if event.inaxes is not None and len(self.G.nodes()) > 0:
nodelist, coords = zip(*nx.get_node_attributes(self.G, 'pos').items())
kdtree = scipy.spatial.KDTree(coords)
self.xydata = np.array([event.xdata, event.ydata])
close_idx = kdtree.query_ball_point(self.xydata, np.sqrt(0.1))
i = close_idx[0]
self.node_pressed = nodelist[i]
def on_motion(self, event):
if event.inaxes is not None and self.node_pressed:
new_xydata = np.array([event.xdata, event.ydata])
self.xydata += new_xydata - self.xydata
#print(d_xy, self.G.nodes[self.node_pressed])
self.G.nodes[self.node_pressed]['pos'] = self.xydata
self.refresh(show=False)
event.canvas.draw()
def on_release(self, event):
self.node_pressed = None
相關來源:
為了擴展我上面的評論,在netgraph
,您的示例可以使用
import numpy as np
import matplotlib.pyplot as plt; plt.ion()
import networkx as nx
import netgraph
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)
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,
)
# move stuff with mouse
關於您編寫的代碼,如果您擁有所有藝術家的句柄,則不需要 kd-tree。 一般來說,matplotlib 藝術家有一個contains
方法,這樣當您記錄按鈕按下事件時,您可以簡單地檢查artist.contains(event)
以找出按鈕按下是否發生在藝術家artist.contains(event)
。 當然,如果您使用 networkx 進行繪圖,則無法以漂亮的、可查詢的形式獲得句柄( ax.get_children()
兩者都不是),因此這是不可能的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.