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

it2023-05-05  72

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]))
最新回复(0)