python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互
实现鼠标交互
1. 实现鼠标点击节点高亮
直接上代码:
# -*- 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: # 获取鼠标事件
an1.set_bbox(dict(facecolor='red', alpha= 0.5, boxstyle= 'round'))
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)
主要就是实现回调函数onclick。通过点击test1,实现背景高亮。
注意
切记要加上fig.canvas.draw(),否则回调函数中的参数无法更新。这个bug找了好久,都是泪。
2. 实现鼠标悬停在节点,显示节点信息。
# -*- 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: # 获取鼠标事件
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() # 切记要加上这句,否则,回调函数中的参数无法更新
# check mouse enter
def mouse_enter(event):
visible_change_flag = False
visible_flag = an1.contains(event)[0] #注意,这里检查节点an1是否包含鼠标,而不是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)
效果图
注意
这里检查节点an1是否包含鼠标,而不是tag1
鼠标悬停事件是motion_notify_event,而不是button_press_event
python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互相关推荐
- python 节点关系图_python可视化节点关系(三):matplotlib(1)
接下来,开始记录使用matplotlib这个号称python使用最广泛的图形库,所遇到的种种神坑. 一. 画节点图: 1. 画节点和箭头 查阅api文档,觉得annotate类封装的不错. impor ...
- python 节点关系图_python 可视化节点关系(一):networkx
前言 工作需要将各个类之间的关系用网络图描述出来. 查阅相关资料,主要有如下方式: networkx qtgraph matplotlib 一.networkx networkx是用Python语言开 ...
- python数据可视化开发(1):Matplotlib库基础知识
文章目录 前言 01.工具栏组件 02.图表数据 03.设置字体字典 全局字体样式 常用中文字体对应名称 查询当前系统所有字体 04.图像配置实例 配置格式 参数说明 官方文档:[matplotlib ...
- python和arduino的关系_Python与Arduino进行交互
Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 什么是Arduino? Arduino是一款拥有多个输入输出端的开源开放式硬件可编程控制器,下图展示的是A ...
- 第十九章 python 数据可视化 Matplotlib Pygal
python 数据可视化 一.使用Matplotlib生成数据图 1. matplotlib画图中文乱码小方框的解决方法 2. 知识点 3. 安装Matplotlib包 4. Matplotlib数据 ...
- python数据可视化开发(2):pandas读取Excel的数据格式处理(数据读取、指定列数据、DataFrame转json、数学运算、透视表运算输出)
系列文章目录 python开发低代码数据可视化大屏:pandas.read_excel读取表格 python实现直接读取excle数据实现的百度地图标注 python数据可视化开发(1):Matplo ...
- python 知乎关系图谱_5000行python代码+可视化60W数据,告诉你知乎用户不为人知的事...
一次完整的python分析+可视化展示,是什么样的? 比如我想知道知乎用户的学历,是否都是985呢?我还想知道知乎最受关注的话题都是些什么?高端人士都喜欢看什么书呢?"人在XX,刚下飞机?& ...
- 《Python数据可视化之matplotlib实践》配套代码
向AI转型的程序员都关注了这个号???????????? 机器学习AI算法工程 公众号:datayx <Python数据可视化之matplotlib实践> 借助matplotlib讲解 ...
- Python的数据科学函数包(三)——matplotlib(plt)
Matplotlib是Python最著名的2D绘图库 c opencv要比PIL, plt的速度更快一些 matplotlib中一张图的具体构造 如果将Matplotlib绘图和我们平常画画相类比,可 ...
最新文章
- 2021个人北美秋招总结
- ACM的java 傻瓜式教学
- BZOJ 1084: [SCOI2005]最大子矩阵【DP】
- makefile例子
- linux三个命令模式切换,ubuntu16.04命令行模式和图形界面互相切换的两种解决办法,...
- 2018年终盘点:“年度爆款”的区块链真的结束了吗?
- c# 以太坊代币_C代币
- 2019牛客多校第四场B xor(线性基求交)题解
- ODAC Windows 安装
- 最新版PandoraBox潘多拉安装adbyby去广告插件图文详细教程!!
- python合并相同内容单元格_快速合并单元格相同项的内容
- Slider网站欣赏
- 费马大定理与费马小定理
- IM学习-认识即时通讯IM(一)
- 图片批量OCR识别--支持各种图片
- 租车汽车租赁管理系统
- jlinkV8指示灯不亮 usb无法识别的问题
- .py文件与.ipynb文件互相转换
- 如何keil4下面创建工程?
- python的一些语句的意思