matplotlib交互式数据光标实现(mplcursors)


Posted in Python onJanuary 13, 2021

简介

mplcursors包也可以为matplotlib提供交互式的数据光标(弹出式注释框),它的灵感来源于mpldatacursor包,可以认为是基于mpldatacursor包的二次开发。
相对于mpldatacursor包,mplcursors包最大的特点就是提供了一些相对底层的API,这样功能实现更加灵活。

安装

pip install mplcursors

基本应用

mplcursors包的基本应用方法与mpldatacursor包类似,直接应用cursor函数即可。

基本操作方法

  • 鼠标左键单击图表数据元素时会弹出文本框显示最近的数据元素的坐标值。
  • 鼠标右键单击文本框取消显示数据光标。
  • 按d键时切换显示\关闭数据光标。

案例源码

import matplotlib.pyplot as plt
import numpy as np
import mplcursors

data = np.outer(range(10), range(1, 5))

fig, ax = plt.subplots()
lines = ax.plot(data)
ax.set_title("Click somewhere on a line.\nRight-click to deselect.\n"
       "Annotations can be dragged.")

mplcursors.cursor(lines) # or just mplcursors.cursor()

plt.show()

mplcursors自定义应用

mpldatacursor包中自定义功能主要通过向datacursor函数传递实参实现。
mplcursors包中的cursor函数对标mpldatacursor包中的datacursor函数,但是在参数上发生了变化,保留了artistshoverbindingsmultiplehighlight等类似参数。
mplcursors包增加Selection对象(底层为namedtuple)表示选择的数据元素的属性。
当选中某个数据点时,可以通过添加(add)或删除(remove)事件触发、注册回调函数实现功能,回调函数只有一个参数,及选择的数据点。
在注册回调函数时,mplcursors包支持使用装饰器。

mpldatacursor与mplcursors API对比

下面以修改显示文本信息为例对比下mpldatacursormplcursors的不同实现方式。

matplotlib交互式数据光标实现(mplcursors)

mpldatacursor实现方式

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

ax=plt.gca()
labels = ["a", "b", "c"]
for i in range(3):
  ax.plot(i, i,'o', label=labels[i])

datacursor(formatter='{label}'.format)
plt.show()

mplcursors实现方式一

import matplotlib.pyplot as plt
import numpy as np
import mplcursors

ax=plt.gca()
lines = ax.plot(range(3), range(3), "o")
labels = ["a", "b", "c"]
cursor = mplcursors.cursor(lines)
cursor.connect(
  "add", lambda sel: sel.annotation.set_text(labels[sel.target.index]))

plt.show()

mplcursors实现方式二

import matplotlib.pyplot as plt
import numpy as np
import mplcursors

ax=plt.gca()
lines = ax.plot(range(3), range(3), "o")
labels = ["a", "b", "c"]
cursor = mplcursors.cursor(lines)

@cursor.connect("add")
def on_add(sel):
  sel.annotation.set_text(labels[sel.target.index])
plt.show()

结论

mplcursors包实现的功能与mpldatacursor包非常相似。相对而言mplcursors包的API更加灵活,通过connect函数或者装饰器自定义属性耦合性更弱,便于实现绘图与数据光标实现的分离。

参考

https://mplcursors.readthedocs.io/en/stable/
https://github.com/anntzer/mplcursors

到此这篇关于matplotlib交互式数据光标实现(mplcursors)的文章就介绍到这了,更多相关matplotlib交互式光标内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 返回汉字的汉语拼音
Feb 27 Python
python操作ssh实现服务器日志下载的方法
Jun 03 Python
python实现12306火车票查询器
Apr 20 Python
python实战之实现excel读取、统计、写入的示例讲解
May 02 Python
Python中Proxypool库的安装与配置
Oct 19 Python
使用Scrapy爬取动态数据
Oct 21 Python
Python弹出输入框并获取输入值的实例
Jun 18 Python
Django中如何使用sass的方法步骤
Jul 09 Python
python运用pygame库实现双人弹球小游戏
Nov 25 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
Apr 08 Python
Numpy ndarray 多维数组对象的使用
Feb 10 Python
详解Java中一维、二维数组在内存中的结构
Feb 11 Python
Python 生成短8位唯一id实战教程
Jan 13 #Python
python uuid生成唯一id或str的最简单案例
Jan 13 #Python
全网最全python库selenium自动化使用详细教程
Jan 12 #Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
Jan 12 #Python
五分钟学会怎么用python做一个简单的贪吃蛇
Jan 12 #Python
python生成word合同的实例方法
Jan 12 #Python
python中常用的数据结构介绍
Jan 12 #Python
You might like
关于Intype一些小问题的解决办法
2008/03/28 PHP
加强版phplib的DB类
2008/03/31 PHP
PHP 函数执行效率的小比较
2010/10/17 PHP
php文件系统处理方法小结
2016/05/23 PHP
PHP设计模式之观察者模式定义与用法示例
2018/08/04 PHP
在Laravel中使用MongoDB的方法示例
2019/11/11 PHP
js利用Array.splice实现Array的insert/remove
2009/01/13 Javascript
Jquery插件写法笔记整理
2012/09/06 Javascript
JavaScript中圆括号()和方括号[]的特殊用法疑问解答
2013/08/06 Javascript
jquery插件开发之实现google+圈子选择功能
2014/03/10 Javascript
jQuery中serializeArray()与serialize()的区别实例分析
2015/12/09 Javascript
多功能jQuery树插件zTree实现权限列表简单实例
2016/07/12 Javascript
js实现四舍五入完全保留两位小数的方法
2016/08/02 Javascript
Vue.js实现一个自定义分页组件vue-paginaiton
2016/09/05 Javascript
AngularJS验证信息框架的封装插件用法【w5cValidator扩展插件】
2016/11/03 Javascript
JAVA中截取字符串substring用法详解
2017/04/14 Javascript
Vue.js学习教程之列表渲染详解
2017/05/17 Javascript
解决bootstrap中使用modal加载kindeditor时弹出层文本框不能输入的问题
2017/06/05 Javascript
jquery ajax 请求小技巧实例分析
2019/11/11 jQuery
ajax jquery实现页面某一个div的刷新效果
2021/03/04 jQuery
Python批量发送post请求的实现代码
2018/05/05 Python
python生成器与迭代器详解
2019/01/01 Python
Python 如何提高元组的可读性
2019/08/26 Python
Python 实现取多维数组第n维的前几位
2019/11/26 Python
django中的数据库迁移的实现
2020/03/16 Python
基于Python词云分析政府工作报告关键词
2020/06/02 Python
利用html5 canvas破解简单验证码及getImageData接口应用
2013/01/25 HTML / CSS
Topshop法国官网:英国快速时尚品牌
2018/04/08 全球购物
德国内衣、泳装和睡衣网上商店:Bigsize Dessous
2018/07/09 全球购物
乌克兰电子产品和家用电器购物网站:TOUCH
2019/08/09 全球购物
海南地接欢迎词
2014/01/14 职场文书
护士演讲稿优秀范文
2014/04/30 职场文书
三月法制宣传月活动总结
2014/07/03 职场文书
志愿者个人总结
2015/03/03 职场文书
师范生小学见习总结
2015/06/23 职场文书
2016年员工政治思想表现评语
2015/12/02 职场文书