使用鼠标单击,networkX,python 2.7删除节点 [英] Remove nodes with mouse click, networkX, python 2.7

查看:189
本文介绍了使用鼠标单击,networkX,python 2.7删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Python 2.7用networkX编写了一个程序,该程序绘制了一个带有黑白节点的树.这是一个最小的示例:

I have written a program in Python 2.7 with networkX which draws a tree with black and white nodes. Here is a minimal example:

import networkx as nx
import matplotlib.pyplot as plt
import numpy

T = nx.Graph()

### Nodes
white, black = [1, 4, 5, 6, 7], [2, 3]
allNodes = white+black

for node in allNodes:      T.add_node(node)

### Edges
T.add_edge(1, 2)
T.add_edge(1, 3)
T.add_edge(2, 4)
T.add_edge(2, 5)
T.add_edge(3, 6)
T.add_edge(3, 7)

### Positions of the nodes
pos={}
pos[1]=numpy.array([ 0,0])
pos[2]=numpy.array([-2,1])
pos[3]=numpy.array([ 2,1])
pos[4]=numpy.array([-3,2])
pos[5]=numpy.array([-1,2])
pos[6]=numpy.array([ 1,2])
pos[7]=numpy.array([ 3,2])

### Draw nodes and edges
nx.draw_networkx_nodes(T, pos, nodelist=black, node_color='k',     node_size=400,     alpha=0.8)
nx.draw_networkx_nodes(T, pos, nodelist=white, node_color='w',     node_size=400,     alpha=0.8)
nx.draw_networkx_edges(T,pos,width=1.0, alpha=0.5)

plt.axis('off')     # Remove the axes
plt.show()          # Show the tree

代码创建一个带有一棵小树的窗口,其中包含7个节点和6个边缘.现在,当我用鼠标单击节点时,我希望这些节点消失.我该怎么办?

The code creates a window with a small tree containing 7 nodes and 6 edges. Now I want the nodes to disappear when I click at them with the mouse. How can I do this?

我后来的计划是2个玩家轮流通过消除叶子或根部颜色(黑色和白色)来轮流交替. IE.玩家1只能移除黑叶或黑根,而玩家2只能移除白叶和白根.

Later my plan is that 2 players alternate in turn by removing leafs or the root in their color (black and white). I.e. player 1 can only remove black leaves or black roots, and player two can only remove white leafs and white roots.

我发现这些链接可能有用,但无法正常工作

I found these links that could be helpful but couldn't get it to work:

  • https://gist.github.com/Zulko/7629965
  • https://gist.github.com/smathot/2011427
  • Matplotlib / python clickable points

任何知道如何做到这一点或有一些提示的人吗?

Anyone who knows how to accomplish this or have some tips?

推荐答案

我设法做到了!我必须从图中保存"fig"和"ax"(我认为这是图形和轴吗?).然后,我可以将称为onClick的方法/函数连接到对鼠标单击有反应的图形("button_press_event").

I managed to do it! I had to save "fig" and "ax" from the plot (I think it is figure and axes?). Then I could connect a method/function, which I call onClick, to the figure which reacts to mouse clicks ('button_press_event').

这里是如何从图中获取"fig"和"ax"并连接函数/方法:

Here how to get "fig" and "ax" from the plot and connecting the function/method:

fig, ax = plt.subplots()
fig.canvas.mpl_connect('button_press_event', onClick)

这是从图形和绘图中删除节点的方法

And here is the method that removes the nodes from the graph and the plot

def onClick(event):
    (x,y)   = (event.xdata, event.ydata)

    for i in allNodes:            
        node = pos[i]
        distance = pow(x-node[0],2)+pow(y-node[1],2)
        if distance < 0.1:
            T.remove_node(i)
            if i in black: black.remove(i)
            if i in white: white.remove(i)
            allNodes.remove(i)
            refreshGraph()    

整个代码:

import networkx as nx
import matplotlib.pyplot as plt
import numpy
import numpy as np
import pylab

def refreshGraph():
    plt.clf()
    nx.draw_networkx_nodes(T, pos, nodelist=black, node_color='k', node_size=400, alpha=0.8)
    nx.draw_networkx_nodes(T, pos, nodelist=white, node_color='w', node_size=400, alpha=0.8)
    nx.draw_networkx_edges(T,pos,width=1.0, alpha=0.5)
    plt.axis('off')
    plt.axis((-4,4,-1,3))
    fig.patch.set_facecolor('white')
    plt.show()

def onClick(event):
    (x,y)   = (event.xdata, event.ydata)

    for i in allNodes:            
        node = pos[i]
        distance = pow(x-node[0],2)+pow(y-node[1],2)
        if distance < 0.1:
            T.remove_node(i)
            if i in black: black.remove(i)
            if i in white: white.remove(i)
            allNodes.remove(i)
            refreshGraph()

fig, ax = plt.subplots()
fig.canvas.mpl_connect('button_press_event', onClick)

T = nx.Graph()

### Nodes
white, black = [1, 4, 5, 6, 7], [2, 3]
allNodes = white+black

for node in allNodes:      T.add_node(node)

### Edges
T.add_edge(1, 2)
T.add_edge(1, 3)
T.add_edge(2, 4)
T.add_edge(2, 5)
T.add_edge(3, 6)
T.add_edge(3, 7)

### Positions of the nodes
pos={}
pos[1]=numpy.array([ 0,0])
pos[2]=numpy.array([-2,1])
pos[3]=numpy.array([ 2,1])
pos[4]=numpy.array([-3,2])
pos[5]=numpy.array([-1,2])
pos[6]=numpy.array([ 1,2])
pos[7]=numpy.array([ 3,2])

### Draw nodes and edges
refreshGraph()

这篇关于使用鼠标单击,networkX,python 2.7删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆