Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - how can I display cursor on all axes vertically but only on horizontally on axes with mouse pointer on top

I would like the cursor to be visible across all axes vertically but only visible horizontally for the axis that the mouse pointer is on.

This is a code exert of what I am using at the moment.

import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor

fig = plt.figure(facecolor='#07000d')

ax1 = plt.subplot2grid((2,4), (0,0), rowspan=1,colspan=4, axisbg='#aaaaaa')
ax2 = plt.subplot2grid((2,4), (1,0), rowspan=1,colspan=4, axisbg='#aaaaaa')

multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=.5, horizOn=True, vertOn=True)
plt.show()

enter image description here This is what I have. What I would like is to have the horizontal cursor to be only visible for the axis that mouse pointer is on but have the vertical visible for both.

like image 367
bandito40 Avatar asked Sep 26 '22 09:09

bandito40


1 Answers

So I came up with a solution. I made my own custom cursor using mouse events wrapped in a class. Works great. You can add your axes in an array/list.

import numpy as np
import matplotlib.pyplot as plt

class CustomCursor(object):

def __init__(self, axes, col, xlimits, ylimits, **kwargs):
    self.items = np.zeros(shape=(len(axes),3), dtype=np.object)
    self.col = col
    self.focus = 0
    i = 0
    for ax in axes:
        axis = ax
        axis.set_gid(i)
        lx = ax.axvline(ymin=ylimits[0],ymax=ylimits[1],color=col)
        ly = ax.axhline(xmin=xlimits[0],xmax=xlimits[1],color=col)
        item = list([axis,lx,ly])
        self.items[i] = item
        i += 1
def show_xy(self, event):
    if event.inaxes:
        self.focus = event.inaxes.get_gid()
        for ax in self.items[:,0]:
            self.gid = ax.get_gid()                                     
            for lx in self.items[:,1]:
                lx.set_xdata(event.xdata)
            if event.inaxes.get_gid() == self.gid:
                self.items[self.gid,2].set_ydata(event.ydata)
                self.items[self.gid,2].set_visible(True)
    plt.draw()

def hide_y(self, event):
    for ax in self.items[:,0]:
        if self.focus == ax.get_gid():
            self.items[self.focus,2].set_visible(False)




if __name__ == '__main__':
fig = plt.figure(facecolor='#07000d')
ax1 = plt.subplot2grid((2,4), (0,0), rowspan=1,colspan=4, axisbg='#aaaaaa')
ax2 = plt.subplot2grid((2,4), (1,0), rowspan=1,colspan=4, axisbg='#aaaaaa')

cc = CustomCursor([ax1,ax2], col='red', xlimits=[0,100], ylimits=[0,100], markersize=30,)
fig.canvas.mpl_connect('motion_notify_event', cc.show_xy)
fig.canvas.mpl_connect('axes_leave_event', cc.hide_y)


plt.tight_layout()
plt.show()

enter image description here

like image 138
bandito40 Avatar answered Oct 11 '22 12:10

bandito40