用matplotlib绘制十字星光标

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

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

from matplotlib.widgets import Cursor
import matplotlib.pyplot as plt
import numpy as np

fig, (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 np

fig, (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 np

fig, (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 np

fig, (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 plt
        import numpy as np

        fig, (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 = canvas
        self.axes = axes
        self.single = single
        if 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 = None
        self.needclear = False

        lineprops['animated'] = True # for blt

        self.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: return
        if not self.canvas.widgetlock.available(self): return

        self.needclear = True

        for 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()

转载请注明出处:https://blog.csdn.net/PeakGao/article/details/105646118

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值