我已经使用3个主要成分(PC1,PC2,PC3)对我的数据运行了sklearn-主成分分析。数据如下所示(这是一个熊猫DataFrame):
这是用于绘制主要成分的代码:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
%matplotlib
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_title('3D Scatter Plot')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
ax.view_init(elev=12, azim=40) # elevation and angle
ax.dist=10 # distance
ax.scatter(
data_df_3dx['PC1'], data_df_3dx['PC2'], data_df_3dx['PC3'], # data
#color='purple', # marker colour
#marker='o', # marker shape
s=60 # marker size
)
plt.show()
我的问题是,如何在标签上添加标签(例如“ GER,中号”)?希望可以有人帮帮我 :)
一种方法是在for循环内分别绘制每个点,这样您就可以知道每个点的坐标并可以向其中添加文本。
for i in range(len(data_df_3dx)):
x, y, z = data_df_3dx.iloc[i]['PC1'], data_df_3dx.iloc[i]['PC2'], data_df_3dx.iloc[i]['PC3']
ax.scatter(x, y, z)
#now that you have the coordinates you can apply whatever text you need. I'm
#assuming you want the index, but you could also pass a column name if needed
ax.text(x, y, z, '{0}'.format(data_df_3dx.index[i]), size=5)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句