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和Ruby中each循环引用变量问题(一个隐秘BUG?)
Jun 04 Python
Python创建模块及模块导入的方法
May 27 Python
python itchat实现微信自动回复的示例代码
Aug 14 Python
Python变量赋值的秘密分享
Apr 03 Python
python验证码识别教程之利用滴水算法分割图片
Jun 05 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
Sep 03 Python
详解Python发送email的三种方式
Oct 18 Python
对pandas通过索引提取dataframe的行方法详解
Feb 01 Python
python 用户交互输入input的4种用法详解
Sep 24 Python
pycharm解决关闭flask后依旧可以访问服务的问题
Apr 03 Python
python中使用input()函数获取用户输入值方式
May 03 Python
pytorch 实现在测试的时候启用dropout
May 27 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
PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ...
2016/02/14 PHP
将PHP的session数据存储到数据库中的代码实例
2016/06/24 PHP
php脚本守护进程原理与实现方法详解
2017/07/20 PHP
PHP将数据导出Excel表中的实例(投机型)
2017/07/31 PHP
PHP设计模式(四)原型模式Prototype实例详解【创建型】
2020/05/02 PHP
JS 的应用开发初探(mootools)
2009/12/19 Javascript
JavaScript的parseInt 取整使用
2011/05/09 Javascript
php读取sqlite数据库入门实例代码
2014/06/25 Javascript
Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
2015/01/01 NodeJs
jQuery中extend函数详解
2015/07/13 Javascript
Angular4学习笔记之新建项目的方法
2017/07/18 Javascript
js实现鼠标单击Tab表单切换效果
2018/05/16 Javascript
Javascript实现动态时钟效果
2018/11/17 Javascript
新手简单了解vue
2019/05/29 Javascript
小程序调用微信支付的方法
2019/09/26 Javascript
微信小程序添加插屏广告并设置显示频率(一天一次)
2019/12/06 Javascript
JS实现关闭小广告特效
2021/01/29 Javascript
Electron 打包问题:electron-builder 下载各种依赖出错(推荐)
2020/07/09 Javascript
react的hooks的用法详解
2020/10/12 Javascript
[01:13]2015国际邀请赛线下观战现场
2015/08/08 DOTA
关于numpy数组轴的使用详解
2019/12/05 Python
python基于property()函数定义属性
2020/01/22 Python
python实现梯度法 python最速下降法
2020/03/24 Python
spyder 在控制台(console)执行python文件,debug python程序方式
2020/04/20 Python
李维斯法国官网:Levi’s法国
2019/07/13 全球购物
加拿大著名的奢侈品购物网站:SSENSE(支持中文)
2020/06/25 全球购物
应届生服装设计自我评价
2013/09/20 职场文书
后勤人员自我鉴定
2013/10/20 职场文书
自荐信结尾
2013/10/27 职场文书
新学期家长寄语
2014/01/19 职场文书
经济贸易系毕业生求职信
2014/05/31 职场文书
小学安全汇报材料
2014/08/14 职场文书
财务人员岗位职责
2015/02/03 职场文书
工厂清洁工岗位职责
2015/02/14 职场文书
2015年营业员工作总结
2015/04/23 职场文书
Python 处理表格进行成绩排序的操作代码
2021/07/26 Python