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

I am trying to use the DataCursor approach ( https://stackoverflow.com/a/3/9/ ) to mark points using matplotlib. I have several thousand points, and I would like to see their shortcut on the mouse. However, there are two differences: one, I make a scatter and two, I want to name the names for each point, and not just the x, y coordinates.

here is my code

import os import matplotlib.pyplot as plt class DataCursor(object): text_template = 'x: %0.2f\ny: %0.2f' x, y = 0.0, 0.0 xoffset, yoffset = -20, 20 text_template = 'x: %0.2f\ny: %0.2f' def __init__(self, ax, labels,x,y): self.ax = ax self.xlist = x self.ylist = y self.labels = labels self.annotation = ax.annotate(self.text_template, xy=(self.x, self.y), xytext=(self.xoffset, self.yoffset), textcoords='offset points', ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0') ) self.annotation.set_visible(False) def __call__(self, event): self.event = event xdata, ydata = event.artist.get_data() #self.x, self.y = xdata[event.ind], ydata[event.ind] self.x, self.y = event.mouseevent.xdata, event.mouseevent.ydata self.label = self.labels[self.xlist.index(self.x)] if self.x is not None: self.annotation.xy = self.x, self.y self.annotation.set_text(self.label) self.annotation.set_visible(True) event.canvas.draw() def process(): #code to make ht_dict here # ht_dict has the following format: 'ht1' = [nov14count, nov21count] where each key is a string and each value is a list of two integers print("Start making scatter plot..") hts = [] nov14 = [] nov21 = [] for key in ht_dict.keys(): nov14.append(ht_dict[key][0]) nov21.append(ht_dict[key][1]) hts.append(key) fig = plt.figure() scatter = plt.scatter(nov14, nov21) fig.canvas.mpl_connect('pick_event', DataCursor(plt.gca(), hts, nov14, nov21)) scatter.set_picker(5) plt.show() process() 

I get the following error:

 AttributeError: 'CircleCollection' object has no attribute 'get_data' 

I want to see a line stored in the hts list when I hover over the x and y coordinates stored in lists nov14 and nov21, respectively, with the same indexes. I am not sure what to do with this error and I will be grateful for any help. Another question I have (from checking changes to an existing chart in the DataCursor stream) is that using the index to return the label, as I am doing now, will give me a value that does not exist in the list error, since clicking on the value may not be exactly like that same as the value in the list. Do you have any suggestions on a better way to display any point label / name?

Any directions or pointers to documentation where I could read would be appreciated.

Thanks!

+4
source share
1 answer

Taking the annotation approach using the label shown in the examples section of the mpldatacursor documentation page , you can do something along these lines (build one 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 is rather inefficient, i.e. you can hardly use more than, say, 1000 points.

Image of an example result:

annotated scatter

+2
source

Source: https://habr.com/ru/post/1383223/


All Articles