Delete nodes with mouse, networkX, python 2.7

I wrote a program in Python 2.7 using networkX that 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

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 on them with the mouse. How can i do this?

Later, my plan is that 2 players alternately alternate, removing leaves or root in their color (black and white). That is, player 1 can only remove black leaves or black roots, and the second player can only remove white leaves and white roots.

I found these links that may be useful, but cannot make it work:

, , ?

+3
1

! "" "" ( , ?). /, onClick, , ('button_press_event').

, "fig" "ax" /:

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

,

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()
+3

Source: https://habr.com/ru/post/1599235/


All Articles