gpt4 book ai didi

python - 如何在所有轴上垂直显示光标,但仅在鼠标指针位于顶部的轴上水平显示光标

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:38 25 4
gpt4 key购买 nike

我希望光标在所有轴上垂直可见,但仅在鼠标指针所在的轴上水平可见。

这是我目前正在使用的代码集。

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这就是我所拥有的。我想要的是让水平光标仅对鼠标指针所在的轴可见,但垂直方向对两者都可见。

最佳答案

所以我想到了一个解决方案。我使用封装在类中的鼠标事件制作了自己的自定义光标。效果很好。您可以将轴添加到数组/列表中。

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

关于python - 如何在所有轴上垂直显示光标,但仅在鼠标指针位于顶部的轴上水平显示光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35414003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com