【问题标题】:Displaying Coordinates next to points in a 3D Scatter Plot with Python使用 Python 在 3D 散点图中显示点旁边的坐标
【发布时间】:2017-05-24 12:49:55
【问题描述】:

我想简单地显示这个 3D 散点图上每个点的坐标和旁边的坐标。我看过这个:Matplotlib: Annotating a 3D scatter plot,但需要知道如何轻松获取和显示坐标。

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()

谢谢。提供一个更简单的代码演示文稿就是这么长的时间。任何关于缩短它的意见都会有所帮助。

【问题讨论】:

  • 你打算用a、b、c和r、s、t做什么?
  • 这只是从常规列表到浮点数数组的一步一步。
  • 您可以更简洁地将数据定义为x = np.array([1,1,2], dtype=float) 或x = np.array([1.,1.,2.])

标签: python matplotlib 3d label scatter-plot


【解决方案1】:

以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()

您可以更改zdir 来为文本指定不同的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多