Search code examples
python-3.xmatplotlibmplcursors

problems with displaying the coordinates of a point in matplotlib python


I've found a solution to this problem when I use a 2D plot, but I'm working with 3D plots. The current code a have is:

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import mplcursors
import numpy as np

x = np.array([0,1,2,3,4,5,6,7,8,9])
y = np.array([9,8,7,6,5,4,3,2,1,0])
z = np.array([0,9,1,8,2,7,3,6,4,5])

fig=plt.figure()

ax=fig.gca(projection='3d')

ax.scatter(x,y,z,zdir='z',s=20,c=None, depthshade=True)

mplcursors.cursor(hover=True)

plt.show()

I get coordinates with this program but they change if I rotate my graph. how can I fix this? The easiest way I think is to somehow recall the coordinates from the point over which I hover. but how can I do that?


Solution

  • I've fixed it:

    zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
    
    for xc, yc, zc in zip(x, y, z):
        label = '(%d, %d, %d)' % (xc, yc, zc)
        ax.text(xc, yc, zc, label)
    

    If this bit of code is added you get the coordinates displayed next to the points.