如何像上图那样为我的 Axes3D 图添加命名?我的图片如下:
这是我的代码:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X_train['width'], X_train['height'], X_train['color_score'], c=y_train, marker='o', s=100)
ax.set_xlabel('width')
ax.set_ylabel('height')
ax.set_zlabel('color_score')
plt.show()
我必须添加什么才能使底部图形的名称排在顶部? Matplotlib 对于新手来说有点困惑,因此显示哪一行代码和什么格式会非常有帮助。
请您参考如下方法:
我认为 ax.text() 正在做您想要的。参见 here举几个例子。
要根据给定的数据稳健地推断文本的良好(易于阅读)位置,可能具有挑战性。一个天真的方法可能如下所示(没有测试代码):
offset = [0, 0, 0.05]
for label in y_train.unique():
idx = (y_train==label)
posX = X['width'][idx].mean() + offset[0]
posY = X['height'][idx].mean() + offset[1]
posZ = X['color_score'][idx].mean() + offset[2]
ax.text(x=posX, y=posY, z=posZ, s=label, zdir=None)
如果您使用的是 pandas,代码可能类似于以下内容:
X['labels'] = y_train
grouping = X.groupby('labels')
for label, group in grouping:
center = group[['width', 'height', 'color_score']].mean(axis=0).values
center += np.asarray(offset)
ax.text(x=center[0], y=center[1], z=center[2], s=label, zdir=None)
