【转】用matplotlib绘制十字星光标

2023-11-21 05:40

本文主要是介绍【转】用matplotlib绘制十字星光标,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在matplotlib中绘制类似股票软件里面的十字星光标比较简单,这里我们来看看几种内置的光标:

1、单子图上面的十字星光标

from matplotlib.widgets import Cursor
import matplotlib.pyplot as plt
import numpy as npfig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
t = np.arange(0.0, 2.0, 0.01)
ax1.plot(t, np.sin(2*np.pi*t))
ax2.plot(t, np.sin(4*np.pi*t))cursor = Cursor(ax1, useblit=True, color='r', lw=0.5)
plt.show()

看看效果

 

2、多子图上面同时出现十字星光标

from matplotlib.widgets import Cursor,MultiCursor
import matplotlib.pyplot as plt
import numpy as npfig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
t = np.arange(0.0, 2.0, 0.01)
ax1.plot(t, np.sin(2*np.pi*t))
ax2.plot(t, np.sin(4*np.pi*t))# cursor = Cursor(ax1, useblit=True, color='r', lw=0.5)
cursor = MultiCursor(fig.canvas, (ax1, ax2), useblit=True, horizOn=True, vertOn=True, color='r', lw=0.5)
plt.show()

结果:

3、单+多十字星光标

这种在股票软件里面很常见,比如同花顺里面,同一日期里面的多个技术指标,他们只有一条水平线,而每个子图里面都有垂直线,找了下matplotlib里面没有这种实现,所以自己写了一个,代码见文后,先看看效果。

# from matplotlib.widgets import Cursor,MultiCursor
import matplotlib.pyplot as plt
import numpy as npfig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
t = np.arange(0.0, 2.0, 0.01)
ax1.plot(t, np.sin(2*np.pi*t))
ax2.plot(t, np.sin(4*np.pi*t))# cursor = Cursor(ax1, useblit=True, color='r', lw=0.5)
# cursor = MultiCursor(fig.canvas, (ax1, ax2), useblit=True, horizOn=True, vertOn=True, color='r', lw=0.5)
cursor = SingleMultiCursor(fig.canvas, (ax1, ax2), single=0, color='r', lw=0.5)
plt.show()

看看效果

如果把代码中的子图左右排列,并且single的类型改成1,看看效果

# from matplotlib.widgets import Cursor,MultiCursor
import matplotlib.pyplot as plt
import numpy as npfig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True)
t = np.arange(0.0, 2.0, 0.01)
ax1.plot(t, np.sin(2*np.pi*t))
ax2.plot(t, np.sin(4*np.pi*t))# cursor = Cursor(ax1, useblit=True, color='r', lw=0.5)
# cursor = MultiCursor(fig.canvas, (ax1, ax2), useblit=True, horizOn=True, vertOn=True, color='r', lw=0.5)
cursor = SingleMultiCursor(fig.canvas, (ax1, ax2), single=1, color='r', lw=0.5)
plt.show()

附SingleMultiCursor的实现代码:

class SingleMultiCursor():"""一个用于多个子图(横排或者竖排)的十字星光标,可以在多个子图上同时出现single=0表示仅仅一个子图显示水平线,所有子图显示垂直线,用于竖排的子图single=1表示仅仅一个子图显示垂直线,所有子图显示水平线,用于横排的子图注意:为了能让光标响应事件处理,必须保持对它的引用(比如有个变量保存)用法::import matplotlib.pyplot as pltimport numpy as npfig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)t = np.arange(0.0, 2.0, 0.01)ax1.plot(t, np.sin(2*np.pi*t))ax2.plot(t, np.sin(4*np.pi*t))cursor = SingleMultiCursor(fig.canvas, (ax1, ax2), single=0, color='w', lw=0.5)plt.show()"""def __init__(self, canvas, axes, single=0, **lineprops):self.canvas = canvasself.axes = axesself.single = singleif single not in [0, 1]:raise ValueError('Unrecognized single value: ' + str(single) + ', must be 0 or 1')xmin, xmax = axes[-1].get_xlim()ymin, ymax = axes[-1].get_ylim()xmid = 0.5 * (xmin + xmax)ymid = 0.5 * (ymin + ymax)self.background = Noneself.needclear = Falselineprops['animated'] = True # for bltself.lines = [[ax.axhline(ymid, visible=False, **lineprops) for ax in axes],[ax.axvline(xmid, visible=False, **lineprops) for ax in axes]]self.canvas.mpl_connect('motion_notify_event', self.onmove)self.canvas.mpl_connect('draw_event', self.clear)def clear(self, event):self.background = (self.canvas.copy_from_bbox(self.canvas.figure.bbox))for line in self.lines[0] + self.lines[1]:line.set_visible(False)def onmove(self, event):if event.inaxes is None: returnif not self.canvas.widgetlock.available(self): returnself.needclear = Truefor i in range(len(self.axes)):if event.inaxes == self.axes[i]:if self.single == 0:for line in self.lines[1]:line.set_xdata((event.xdata, event.xdata))line.set_visible(True)line = self.lines[0][i]line.set_ydata((event.ydata, event.ydata))line.set_visible(True)else:for line in self.lines[0]:line.set_ydata((event.ydata, event.ydata))line.set_visible(True)line = self.lines[1][i]line.set_xdata((event.xdata, event.xdata))line.set_visible(True)else:self.lines[self.single][i].set_visible(False)if self.background is not None:self.canvas.restore_region(self.background)for lines in self.lines:for line in lines:if line.get_visible():line.axes.draw_artist(line)self.canvas.blit()

 

这篇关于【转】用matplotlib绘制十字星光标的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/400210

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3