可交互绘图——鼠标移到点的上方会显示该点的标签[jupyter notebook]

import matplotlib.pyplot as plt
import numpy as np
import mplcursors
np.random.seed(42)

%matplotlib auto  # 终端运行脚本不用加
plt.scatter(*np.random.random((2, 26)))
crs = mplcursors.cursor(hover=True)
crs.connect("add", lambda sel: sel.annotation.set_text(
    'Point {},{}'.format(sel.target[0], sel.target[1])))
plt.show()
plt.ion() # 终端运行脚本不用加

在这里插入图片描述
hover: True/False 控制是否不用点击就显示标签
如果有自定义的标签:

labels = [...]
crs = mplcursors.cursor(hover=True)
crs.connect(
    "add", lambda sel: sel.annotation.set_text(labels[sel.target.index]))

猜你喜欢

转载自blog.csdn.net/zdx1996/article/details/109182029