python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互
實(shí)現(xiàn)鼠標(biāo)交互
1. 實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊節(jié)點(diǎn)高亮
直接上代碼:
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
fig, ax = plt.subplots(figsize=(10,10))
node_pos = [(1, 0), (0, 1), (2,1), (1,2)]
bbox_args = dict(boxstyle="round", fc="0.8")
arrow_args = dict(arrowstyle="->")
an1 = plt.annotate(s="Test 1\n(node1,node2)\n(node3,node4,node5)\n\n\n", xy=node_pos[0], xycoords="data",
va="center", ha="center",
bbox=dict(boxstyle="round", fc="w", facecolor='green'))
an2 = plt.annotate(s="Test 2\n(node1,node2)", xy=node_pos[1], xycoords="data",
va="center", ha="center",
bbox=dict(boxstyle="round", fc="w", facecolor='green'))
arrow1 = plt.annotate('', xy=(0, 0), xycoords=an1,
xytext=(0, 0), textcoords=an2, # an1 -> an2
size=20, ha="center", va="center",
bbox=bbox_args,
arrowprops=dict(patchA=an2,
patchB=an1,
connectionstyle="arc3,rad=0.2",
#color='red',
#linewidth=5,
**arrow_args))
arrow2 = plt.annotate('', xy=(0, 0), xycoords=an2,
xytext=(0, 0), textcoords=an1, # an1 -> an2
size=20, ha="center", va="center",
bbox=bbox_args,
arrowprops=dict(patchA=an1,
patchB=an2,
connectionstyle="arc3,rad=0.2",
#color='red',
#linewidth=5,
**arrow_args))
# callback fun
def mouse_click(event):
if an1.contains(event)[0] == True: # 獲取鼠標(biāo)事件
an1.set_bbox(dict(facecolor='red', alpha= 0.5, boxstyle= 'round'))
fig.canvas.draw() # 切記要加上這句,否則,回調(diào)函數(shù)中的參數(shù)無法更新
axes = plt.gca()
axes.set_xlim([-2,2])
axes.set_ylim([-2,2])
fig.canvas.mpl_connect('button_press_event', mouse_click)
主要就是實(shí)現(xiàn)回調(diào)函數(shù)onclick。通過點(diǎn)擊test1,實(shí)現(xiàn)背景高亮。
注意
切記要加上fig.canvas.draw(),否則回調(diào)函數(shù)中的參數(shù)無法更新。這個(gè)bug找了好久,都是淚。
2. 實(shí)現(xiàn)鼠標(biāo)懸停在節(jié)點(diǎn),顯示節(jié)點(diǎn)信息。
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
fig, ax = plt.subplots(figsize=(10,10))
node_pos = [(1, 0), (0, 1), (2,1), (1,2)]
bbox_args = dict(boxstyle="round", fc="0.8")
arrow_args = dict(arrowstyle="->")
an1 = plt.annotate(s="Test 1\n(node1,node2)\n(node3,node4,node5)\n\n\n", xy=node_pos[0], xycoords="data",
va="center", ha="center",
bbox=bbox_args)
an2 = plt.annotate(s="Test 2\n(node1,node2)", xy=node_pos[1], xycoords="data",
va="center", ha="center",
bbox=bbox_args)
arrow1 = plt.annotate('', xy=(0, 0), xycoords=an1,
xytext=(0, 0), textcoords=an2, # an1 -> an2
size=20, ha="center", va="center",
bbox=bbox_args,
arrowprops=dict(patchA=an2,
patchB=an1,
connectionstyle="arc3,rad=0.2",
#color='red',
#linewidth=5,
**arrow_args))
arrow2 = plt.annotate('', xy=(0, 0), xycoords=an2,
xytext=(0, 0), textcoords=an1, # an1 -> an2
size=20, ha="center", va="center",
bbox=bbox_args,
arrowprops=dict(patchA=an1,
patchB=an2,
connectionstyle="arc3,rad=0.2",
#color='red',
#linewidth=5,
**arrow_args))
#fig.canvas.draw()
node_pos = an1.get_position()
tag1 = plt.annotate(s='hello node1', xy=node_pos, xycoords='data',
color='red',
xytext=(node_pos[0] + 0.3, node_pos[1] + 0.3),
textcoords='data', horizontalalignment="left",
bbox=bbox_args,
annotation_clip=True
)
tag1.set_visible(False)
# callback fun
def mouse_click(event):
if an1.contains(event)[0] == True: # 獲取鼠標(biāo)事件
if an1.get_color() != 'red':
an1.set_color('red') # set font color
an1.set_bbox(dict(facecolor='yellow', alpha=1, boxstyle='round'))
else:
an1.set_color('black') # set font color
an1.set_bbox(bbox_args)
fig.canvas.draw() # 切記要加上這句,否則,回調(diào)函數(shù)中的參數(shù)無法更新
# check mouse enter
def mouse_enter(event):
visible_change_flag = False
visible_flag = an1.contains(event)[0] #注意,這里檢查節(jié)點(diǎn)an1是否包含鼠標(biāo),而不是tag1
if visible_flag != tag1.get_visible():
visible_change_flag = True
tag1.set_visible(visible_flag)
if visible_change_flag:
fig.canvas.draw()
axes = plt.gca()
axes.set_xlim([-2,2])
axes.set_ylim([-2,2])
fig.canvas.mpl_connect('button_press_event', mouse_click)
fig.canvas.mpl_connect('motion_notify_event', mouse_enter)
效果圖
注意
這里檢查節(jié)點(diǎn)an1是否包含鼠標(biāo),而不是tag1
鼠標(biāo)懸停事件是motion_notify_event,而不是button_press_event
總結(jié)
以上是生活随笔為你收集整理的python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据库初识实训总结_MySQL
- 下一篇: h3c交换机配置远程管理_H3C S31