【发布时间】:2015-07-29 03:21:28
【问题描述】:
我在 Python 2.7 中使用 networkX 编写了一个程序,它绘制了一棵带有黑白节点的树。这是一个最小的例子:
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 条边的小树。现在,当我用鼠标单击它们时,我希望节点消失。我该怎么做?
稍后我的计划是让 2 名玩家轮流交替,移除颜色(黑色和白色)中的叶子或根。 IE。玩家1只能去除黑叶或黑根,玩家2只能去除白叶和白根。
我发现这些链接可能会有所帮助,但无法使用:
- https://gist.github.com/Zulko/7629965
- https://gist.github.com/smathot/2011427
- Matplotlib / python clickable points
谁知道如何做到这一点或有一些提示?
【问题讨论】:
-
恐怕我帮不上什么忙,但要澄清你的问题:你想让他们从情节中消失吗?还是从图表中删除?
-
从情节上看,它们也可能从树上消失。
标签: python-2.7 matplotlib networkx interactive