3

I'd like to simply display the coordinates of, and next to, each point on this 3D scatter. I've seen this: Matplotlib: Annotating a 3D scatter plot, but need to know how to easily get and display the COORDINATES.

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')


x = [1, 1, 2]
y = [1, 1, 2]
z = [1, 2, 2]

a = []
b = []
c = []
for item in x:
    a.append(float(item))
for item in y:
    b.append(float(item))
for item in z:
    c.append(float(item))
print(a, b, c)

r = np.array(a)
s = np.array(b)
t = np.array(c)

print(r, s, t)

ax.set_xlabel("x axis")
ax.set_ylabel("y axis")
ax.set_zlabel("z axis")

ax.scatter(r,s,zs = t, s=200, label='True Position')
plt.show()

Thanks. It is this long to provide a simpler presentation of what's going on with the code. Any opinions on shortening it would be helpful, as well.

Community
  • 1
  • 1
peer
  • 162
  • 5
  • 17
  • What do you intend to do with a, b, c and r, s, t? – Lucas Jan 10 '17 at 05:51
  • That was simply a step-by-step to get from a regular list to a floating point number array. – peer Jan 10 '17 at 05:58
  • 2
    Possible duplicate of [Matplotlib: Annotating a 3D scatter plot](http://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot) – Julien Jan 10 '17 at 06:06
  • 1
    you can define your data more concisely as `x = np.array([1,1,2], dtype=float)` or `x = np.array([1.,1.,2.])` – Julien Jan 10 '17 at 06:09

1 Answers1

2

Following the example of the mpl gallery:

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# You can convert to float array in several ways
r = np.array([1, 1, 2], dtype=np.float)
s = np.array([float(i) for i in [1, 1, 2]])
t = np.array([1, 2, 2]) * 1.0

ax.scatter(r,s,zs = t, s=200, label='True Position')
for x, y, z in zip(r, s, t):
    text = str(x) + ', ' + str(y) + ', ' + str(z)
    ax.text(x, y, z, text, zdir=(1, 1, 1))

ax.set_xlabel("x axis")
ax.set_ylabel("y axis")
ax.set_zlabel("z axis")

plt.show()

text_in_3d_example

You can change zdir to give different directions to the text.

Lucas
  • 6,869
  • 5
  • 29
  • 44