小编jua*_*blo的帖子

当鼠标悬停时使用 matplotlib 注释绘图的线条

我想在用鼠标悬停时在绘图上注释不同的线,这里用点做同样的方式可以使标签在将鼠标悬停在 matplotlib 中的点上时出现吗?

我尝试按如下方式调整此代码:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
y = np.random.rand(4,15)
x = [np.arange(15) for i in range(len(y))]
names = np.array(list("ABCD"))
fig, ax = plt.subplots()
lines = []
for i in range(len(names)):
    lines.append(ax.plot(x[i],y[i]))

annot = ax.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):
    pos = line.get_offsets()[ind["ind"][0]]
    annot.xy = pos
    text = "{}, {}".format(" ".join(list(map(str,ind["ind"]))), 
                           " ".join([names[n] for n in ind["ind"]]))
    annot.set_text(text)
    annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]])))
    annot.get_bbox_patch().set_alpha(0.4)

def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax: …
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib hover

3
推荐指数
1
解决办法
8112
查看次数

标签 统计

hover ×1

matplotlib ×1

plot ×1

python ×1