Python 在 3D 散点图中标注点

Python annotating points in a 3D scattter plot

我想为数据中的每个点 (3D) 和标签(标签是字典中的键)添加标签:

l = list(dictionary.keys())
#transform the array to a list
arrayx=arrayx.tolist()
arrayy=arrayy.tolist()
arrayz=arrayz.tolist()
#arrayx contains my x coordinates
ax.scatter(arrayx, arrayy, arrayz)
#give the labels to each point
for  label in enumerate(l):
    ax.annotate(label, ([arrayx[i] for i in range(27)],[arrayy[i]for i in range(27)],[arrayz[i] for i in range(27)]))
plt.title("Data")
plt.show()

我的输入:

arrayx:

[[0.7], [7.1], [7.5], [0.6], [0.5], [0.00016775708773695687]...]

数组:

[[0.1], [2], [3], [6], [5], [16775708773695687]...]

arrayz:

[1], [2], [3], [4], [5], [6]...]

并给图中每个3D点一个标签

您可以按如下方式为每个点添加文本:

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

ax3d = plt.figure().gca(projection='3d')

arrayx = np.array([[0.7], [7.1], [7.5], [0.6], [0.5], [0.00016775708773695687]])
arrayy = np.array([[0.1], [2], [3], [6], [5], [16775708773695687]])
arrayz = np.array([[1], [2], [3], [4], [5], [6]])

labels = ['one', 'two', 'three', 'four', 'five', 'six']

arrayx = arrayx.flatten()
arrayy = arrayy.flatten()
arrayz = arrayz.flatten()

ax3d.scatter(arrayx, arrayy, arrayz)

#give the labels to each point
for x, y, z, label in zip(arrayx, arrayy, arrayz, labels):
    ax3d.text(x, y, z, label)

plt.title("Data")
plt.show()

这将为您提供以下输出:

借用@martin-evans 对代码的回答,但使用 zip

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

ax3d = plt.figure().gca(projection='3d')

arrayx = np.array([[0.7], [7.1], [7.5], [0.6], [0.5], [0.00016775708773695687]])
arrayy = np.array([[0.1], [2], [3], [6], [5], [16775708773695687]])
arrayz = np.array([[1], [2], [3], [4], [5], [6]])

labels = ['one', 'two', 'three', 'four', 'five', 'six']

arrayx = arrayx.flatten()
arrayy = arrayy.flatten()
arrayz = arrayz.flatten()

ax3d.scatter(arrayx, arrayy, arrayz)

#give the labels to each point
for x_label, y_label, z_label, label in zip(arrayx, arrayy, arrayz, labels):
    ax3d.text(x_label, y_label, z_label, label)

plt.title("Data")
plt.show()