Using matplotlib to label points on a scatter plot on mouse over with some label other than x,y coordinates

老子叫甜甜 提交于 2019-12-05 14:31:00

Adopting the approach for annotating using the label, given in the examples section on the documentation page of the mpldatacursor, you could do something along these lines (plot a single point with each scatter plot to be able to set an individual label for each point):

import matplotlib.pyplot as plt
from mpldatacursor import datacursor
import random

fig, ax = plt.subplots()
ax.set_title('Click on a dot to display its label')

# Plot a number of random dots
for i in range(1, 1000):
    ax.scatter([random.random()], [random.random()], label='$ID: {}$'.format(i))

# Use a DataCursor to interactively display the label for a selected line...
datacursor(formatter='{label}'.format)

plt.show()

Unfortunately it's rather inefficient, i.e., hardly usable with more than, say, 1000 points.

Result example image:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!