Python注释三维散点p

2024-09-28 22:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想给数据中的每个点(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()

我的意见:

阵列X:

^{pr2}$

阵列:

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

阵列:

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

并给图形中的每个点3D标注


Tags: theto数据inforrangeplt标签
2条回答

借用@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()

您可以为每个点添加文本,如下所示:

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

相关问题 更多 >