Python在3D散点图中注释点

时间:2017-05-15 14:40:49

标签: python scatter-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]...]

arrayy:

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

arrayz:

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

并为图表中的每个点3D提供标签

2 个答案:

答案 0 :(得分:1)

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

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

这会给你以下输出:

3d scatter plot

答案 1 :(得分:1)

借用@ 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()
相关问题