无法将标签添加到3D散点图

时间:2018-11-19 19:58:18

标签: python-3.x matplotlib

我希望在matplotlib中的3D散点图上添加文本标签,但是它要么根本不起作用,要么将轴折叠成一团糟。我已经尝试了所有可以找到的解决方法,但是它们似乎并没有完成任务。这是我到目前为止所做的。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas as pd

data = pd.DataFrame({'x_value': [0.58245011499999999,0.58875,0.58571373199999999,0.57505909399999999], 'y_value': [0.575939804, 0.53125, 0.574922673, 0.56629390800000001], 'z_value': [0.37844187799999995,0.14374999999999999, 0.38144978899999998, 0.4012994879999999], 'text_name':['text_1', 'text_2', 'text_3', 'text_4']})


fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.scatter(data['x_value'], data['y_value'], data['z_value'], c = data.index,  cmap = cm.summer, s=60)

points = []

for i in range(len(data)):
    points.append((data['x_value'][i], data['y_value'][i], 
    data['z_value'][i], data['text_name'][i]))

ax.set_xlabel('X_VALUES')
ax.set_ylabel('Y_VALUES')
ax.set_zlabel('Z_VALUES')

ax.view_init(34, 200)

plt.title('MY PLOT')

for i in range(len(points)):
    ax.text3D(points[i][0], points[i][1], points[i][2], points[i][3], zdir= 
    (1, 1, 1))

plt.show()

但是,此练习的结果为空-看不见点标签:

Plot with no labels

但是,当我在plt.show()上方添加以下行时,我得到了标签,但所有标签都弄乱了,并因此出现了错误消息:

for spine in ax.spines.values():
    spine.set_visible(False) 
### This loop included to forestall known problem with plt.tight_layout() collapsing axes that nevertheless happens anyway.

plt.tight_layout()

Plot bad labels

  

底部和顶部页边距不能足够大以适应   四轴装饰

有人能建议我做错了什么吗?谢谢!

0 个答案:

没有答案