mplcursors icon indicating copy to clipboard operation
mplcursors copied to clipboard

label customized text on ax.scatter 3D graph, the index is wrong

Open shuyuan-wang opened this issue 3 years ago • 5 comments

When using image The tested index is wrong, the order is wrong.

for example, in the following graph: image the content in the table is correct, when id is 3, value should be 230, but in the graph, the annotated text for id 3 is the value of id 1. The order is messed up

shuyuan-wang avatar May 27 '22 09:05 shuyuan-wang

Please provide a short, self-contained code snippet to reproduce the bug, ideally allowing a simple copy and paste to reproduce. Please do your best to reduce the code snippet to the minimum required. (https://matplotlib.org/stable/devel/contributing.html#submitting-a-bug-report)

anntzer avatar May 27 '22 10:05 anntzer

did not get the exact result, but reproduced a similar error that has something wrong with indexing:

import matplotlib.pyplot as plt
import mplcursors
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
labels = ["a", "b", "c", "d", "e"]
x = [0,1,2,3,4]
y = [0,1,2,3,4]
z = [0,1,2,3,4]

color_list = ['red','orange','blue','blue','green']
ax.scatter(x,y,z, c=color_list, alpha=1)


# line, = ax.plot(x, x, "ro")
mplcursors.cursor(ax, hover=True).connect(
    "add", lambda sel: sel.annotation.set_text(labels[sel.index]))

plt.show()

The result should be a, b, c, d, e shown when clicking from (0,0), (1,1), (2,2), (3,3),(4,4), but if you rotate the graph a little bit, the annotated text will be different Before rotating: image

after rotating image

shuyuan-wang avatar May 27 '22 11:05 shuyuan-wang

Ah, I see, thanks for the report. I can reproduce the problem, but AFAICT this needs to be solved on Matplotlib's side, specifically via something like https://github.com/matplotlib/matplotlib/issues/23155.

anntzer avatar May 27 '22 12:05 anntzer

Thank you , while this is being solved, are there any alternatives? For example, can I use a dictionary with (x,y,z) as key and tag as value? How can I get x, y, z from the lambda sel?

shuyuan-wang avatar May 30 '22 01:05 shuyuan-wang

Unfortunately I do not think this is possible right now; if you check the matplotlib issue tracker iirc there are a few other open issues about the difficulty to retrieve 3d coordinates from interactive events.

anntzer avatar May 30 '22 03:05 anntzer

I believe this is fixed by https://github.com/matplotlib/matplotlib/pull/24491 (which will be present in the future matplotlib 3.7 release).

anntzer avatar Dec 05 '22 09:12 anntzer