sách gpt4 ai đã đi

python - 将鼠标悬停在多轴上的一个点上时如何使标 checkout 现?

In lại 作者:行者123 更新时间:2023-12-01 12:06:37 31 4
mua khóa gpt4 Nike

我正在尝试在具有多个轴的 matplotlib 上显示悬停标签。

我正在使用 python 3.6.8 和 matplotlib 3.0.3

我的情节有多个轴,我查看了这篇文章中的示例:

Possible to make labels appear when hovering over a point in matplotlib?

但什么也没有发生(看不到标签)。

Đây là mã của tôi:

nhập matplotlib.pyplot dưới dạng plt
import numpy as np; np.random.seed(1)

x = np.sort(np.random.rand(15))
y = np.sort(np.random.rand(15))
y2 = np.sort(np.random.rand(15))

fig = plt.figure()
ax1 = plt.subplot(2, 2, 1)
line, = plt.plot(x,y)
ax1.grid(True)

ax2 = ax1.twinx()
ax2.plot(x, y2, color='green')
ax2.tick_params(axis='y', labelcolor='green')

annot = ax1.annotate("", xy=(0,0), xytext=(-20,20),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)

def update_annot(ind):
x,y = line.get_data()
annot.xy = (x[ind["ind"][0]], y[ind["ind"][0]])
text = "x = {}\ny= {}".format(x[ind["ind"][0]], y[ind["ind"][0]])
annot.set_text(text)
annot.get_bbox_patch().set_alpha(0.4)


def hover(event):
vis = annot.get_visible()
if event.inaxes == ax1:
cont, ind = line.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
khác:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", hover)

plt.hiển thị()

当我禁用(评论)第二个轴( trục2 )时,我可以看到标签。

使用多轴时如何显示悬停标签?

1 Câu trả lời

问题确实是仅针对双轴之一触发事件。因此,如果要标记两个轴的内容,则需要多路标记,即创建两个注释,每个轴一个,并调整代码,使两个注释都可能可见。

这可能如下所示:

nhập matplotlib.pyplot dưới dạng plt
import numpy as np; np.random.seed(1)

x = np.sort(np.random.rand(15))
y = np.sort(np.random.rand(15))
y2 = np.sort(np.random.rand(15))

fig = plt.figure()
ax1 = plt.subplot(2, 2, 1)
line1, = plt.plot(x,y)
ax1.grid(True)

ax2 = ax1.twinx()
line2, = ax2.plot(x, y2, color='green')
ax2.tick_params(axis='y', labelcolor='green')

annots = []
for ax in [ax1, ax2]:
annot = ax1.annotate("", xy=(0,0), xytext=(-20,20),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w", alpha=0.4),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
annots.append(annot)

annot_dic = dict(zip([ax1, ax2], annots))
line_dic = dict(zip([ax1, ax2], [line1, line2]))

def update_annot(line, annot, ind):
x,y = line.get_data()
annot.xy = (x[ind["ind"][0]], y[ind["ind"][0]])
text = "x = {}\ny= {}".format(x[ind["ind"][0]], y[ind["ind"][0]])
annot.set_text(text)

def hover(event):

if event.inaxes in [ax1, ax2]:
for ax in [ax1, ax2]:
cont, ind = line_dic[ax].contains(event)
annot = annot_dic[ax]
if cont:
update_annot(line_dic[ax], annot, ind)
annot.set_visible(True)
fig.canvas.draw_idle()
khác:
if annot.get_visible():
annot.set_visible(False)
fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", hover)

plt.hiển thị()

关于python - 将鼠标悬停在多轴上的一个点上时如何使标 checkout 现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55891285/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com