python - 使用 Python 显示 3D 散点图中点旁边的坐标

标签 python matplotlib 3d label scatter-plot

我想简单地显示此 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()

谢谢。这么长的篇幅只是为了更简单地介绍代码的情况。任何有关缩短它的意见也会有所帮助。

最佳答案

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

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

关于python - 使用 Python 显示 3D 散点图中点旁边的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561919/

相关文章:

python - 使用 x 和 y 的数据框通过 Matplotlib 生成直方图

java - ArrayList 中的顶点缓冲区对象?

c++ - 如何沿所有轴正确旋转四元数?

python - 修改 python nltk.word_tokenize 以排除 "#"作为分隔符

python - 在文本小部件中搜索单词/字母 (tkinter)

python - Pandas 在数据框中组合稀疏列

python - 如何绘制 seaborn 中的前 5 个值

python - 迭代子目录中的文本文件

python - Pyplot注释: roman numerals

ios - ARKit 3D Modell 没有纹理