python画十字_matplotlib绘制鼠标的十字光标的实现(内置方式)
相对于echarts等基于JavaScript的图表库,matplotlib的交互能力相对较差。
在实际应用中,我们经常想使用十字光标来定位数据坐标,matplotlib内置提供支持。
官方示例
matplotlib提供了官方示例https://matplotlib.org/gallery/widgets/cursor.html
from matplotlib.widgets import Cursor
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, facecolor='#FFFFCC')
x, y = 4*(np.random.rand(2, 100) - .5)
ax.plot(x, y, 'o')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
# Set useblit=True on most backends for enhanced performance.
cursor = Cursor(ax, useblit=True, color='red', linewidth=2)
plt.show()
原理
由源码可知,实现十字光标的关键在于widgets模块中的Cursor类。
class matplotlib.widgets.Cursor(ax, horizOn=True, vertOn=True, useblit=False, **lineprops)
ax:参数类型matplotlib.axes.Axes,即需要添加十字光标的子图。
horizOn:布尔值,是否显示十字光标中的横线,默认值为显示。
vertOn:布尔值,是否显示十字光标中的竖线,默认值为显示。
useblit:布尔值,是否使用优化模式,默认值为否,优化模式需要后端支持。
**lineprops:十字光标线形属性, 参见axhline函数支持的属性,https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axhline.html#matplotlib.axes.Axes.axhline。
简化案例
光标改为灰色竖虚线,线宽为1。
from matplotlib.widgets import Cursor
import matplotlib.pyplot as plt
ax = plt.gca()
cursor = Cursor(ax, horizOn=False, vertOn= True, useblit=False, color='grey', linewidth=1,linestyle='--')
plt.show()
## Cursor类源码
class Cursor(AxesWidget):
"""
A crosshair cursor that spans the axes and moves with mouse cursor.
For the cursor to remain responsive you must keep a reference to it.
Parameters
----------
ax : `matplotlib.axes.Axes`
The `~.axes.Axes` to attach the cursor to.
horizOn : bool, default: True
Whether to draw the horizontal line.
vertOn : bool, default: True
Whether to draw the vertical line.
useblit : bool, default: False
Use blitting for faster drawing if supported by the backend.
Other Parameters
----------------
**lineprops
`.Line2D` properties that control the appearance of the lines.
See also `~.Axes.axhline`.
Examples
--------
See :doc:`/gallery/widgets/cursor`.
"""
def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,
**lineprops):
AxesWidget.__init__(self, ax)
self.connect_event('motion_notify_event', self.onmove)
self.connect_event('draw_event', self.clear)
self.visible = True
self.horizOn = horizOn
self.vertOn = vertOn
self.useblit = useblit and self.canvas.supports_blit
if self.useblit:
lineprops['animated'] = True
self.lineh = ax.axhline(ax.get_ybound()[0], visible=False, **lineprops)
self.linev = ax.axvline(ax.get_xbound()[0], visible=False, **lineprops)
self.background = None
self.needclear = False
def clear(self, event):
"""Internal event handler to clear the cursor."""
if self.ignore(event):
return
if self.useblit:
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
self.linev.set_visible(False)
self.lineh.set_visible(False)
def onmove(self, event):
"""Internal event handler to draw the cursor when the mouse moves."""
if self.ignore(event):
return
if not self.canvas.widgetlock.available(self):
return
if event.inaxes != self.ax:
self.linev.set_visible(False)
self.lineh.set_visible(False)
if self.needclear:
self.canvas.draw()
self.needclear = False
return
self.needclear = True
if not self.visible:
return
self.linev.set_xdata((event.xdata, event.xdata))
self.lineh.set_ydata((event.ydata, event.ydata))
self.linev.set_visible(self.visible and self.vertOn)
self.lineh.set_visible(self.visible and self.horizOn)
self._update()
def _update(self):
if self.useblit:
if self.background is not None:
self.canvas.restore_region(self.background)
self.ax.draw_artist(self.linev)
self.ax.draw_artist(self.lineh)
self.canvas.blit(self.ax.bbox)
else:
self.canvas.draw_idle()
return False
到此这篇关于matplotlib绘制鼠标的十字光标的实现(内置方式)的文章就介绍到这了,更多相关matplotlib 十字光标内容请搜索得牛网以前的文章或继续浏览下面的相关文章希望大家以后多多支持得牛网!
python画十字_matplotlib绘制鼠标的十字光标的实现(内置方式)相关推荐
- python的类里的属性是否可以为列表_是否有Python方法可以访问类的所有非私有和非内置属性?...
我想调用一种方法给我一个所有"非私有"的词典(我在这里使用"私有"一词,因为它在 Python中并不存在)和非内置属性(即那些在类上不要以单个或双下划线开头.像 ...
- 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要2
适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章 运算符.表达式与内置对象 2.4 Python常用内置函数用法精要 2.4.4 排序与逆序 2 ...
- 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要4
适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章 运算符.表达式与内置对象 2.4 Python常用内置函数用法精要 2.4.7 range() ...
- 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要3
适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章 运算符.表达式与内置对象 2.4 Python常用内置函数用法精要 2.4.6 map().r ...
- 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要1
适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章 运算符.表达式与内置对象 2.4 Python常用内置函数用法精要 2.4.1 类型转换与类型 ...
- python不允许使用关键字作为变量名、允许使用内置函数_【判断题】Python不允许使用关键字作为变量名,但是允许使用内置函数名作为变量名,不过这会改变函数名的含义,所以不建议这样做...
[判断题]Python不允许使用关键字作为变量名,但是允许使用内置函数名作为变量名,不过这会改变函数名的含义,所以不建议这样做 更多相关问题 [单选,A2型题,A1/A2型题] <十四经发挥&g ...
- python不允许使用关键字作为变量名、允许使用内置函数_Python不允许使用关键字作为变量名,允许使用内置函数名作为变量名,但这会改变函数名的含义。...
Python不允许使用关键字作为变量名,允许使用内置函数名作为变量名,但这会改变函数名的含义. 答:对 对于检查出的无效MAC帧,以太网负责重传. 答:× 哪一年开始中国成为世界第二大经济体? 答:2 ...
- matplotlib绘制鼠标的十字光标(内置方式)
相对于echarts等基于JavaScript的图表库,matplotlib的交互能力相对较差. 在实际应用中,我们经常想使用十字光标来定位数据坐标,matplotlib内置提供支持. 官方示例 ma ...
- turtle python tkinter_【案例】 什么?idle 中竟然有内置 turtle 样例?(paint)
案例介绍 我打算开启一个新的方向-- turtle 库案例. 在我们下载安装完毕 Python3 后,在搜索(查找)框中输入 idle.exe,就能够打开系统内置的 Python 开发环境了.不知道有 ...
最新文章
- 【模拟】不高兴的津津
- 数字信号处理实验三用fft对信号作频谱分析_机器学习中的音频特征:理解Mel频谱图...
- 【C 语言】指针间接赋值 ( 指针作为 函数参数 的意义 | 间接赋值 代码示例 )
- TensorFlow2简单入门-三维张量
- 冲刺阶段——Day2
- leetcode18. 四数之和
- 又拍云,音视频CDN加速利器
- Python对json数据的操作(香烟示例)
- 我的java web之路(安装)
- clion jiqiao
- React [Umi] history(API) 路由监听
- 电子设计大赛应该准备什么
- 安卓开发中wifi连接打印机打印图片
- java 打印机_JAVA实现调用打印机打印PDF
- 直播平台源代码快速搭建视频直播平台
- delphi 各版本的特性
- 网络安全笔记-26-Linux-基础
- 月均数据_三季度前20强券商私募资管月均规模下降逾7000亿元 这5家主动规模占比已超50%...
- iar stm32_STM32强大的生态,在这里一起总结!
- (玩转zabbix)硬盘硬件健康状态监控,部件寿命监控
热门文章
- 交流信号叠加直流偏置_T型偏置器与隔直器,二者应用之对比
- python字典示例简单代码_python学习笔记:字典的使用示例详解
- linux赋高权命令有哪些,Linux命令中的Z原来是这个意思
- HALCON 21.11:深度学习笔记---术语表(7)
- 涤纶针织物用分散染料染色时,为什么小样与大样不符?
- Java MVC 1.0规范开始进入公开评审阶段
- Swing 添加超链接 打开页面
- MVC4 数据验证、特性、自动属性总结
- 终于找到了。 图标搜索、UI设计、移动开发集中导航
- struts2:表单标签续(datetimepicker标签)