如何将标签注释到3D matplotlib散点图? [英] How to annotated labels to a 3D matplotlib scatter plot?

查看:932
本文介绍了如何将标签注释到3D matplotlib散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对具有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)

I have run a sklearn - Principal Component Analysis on my data with 3 principal components (PC1, PC2, PC3). The data looks like this (it's a pandas DataFrame):

Here is the code for plotting the principle components:

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

My problem, how do I add labels (like 'GER, medium') to the points? Hope someone can help me :)

解决方案

One way would be to plot each point individually inside of a for loop, that way you know the coordinates of each point and can add text to it.

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)

这篇关于如何将标签注释到3D matplotlib散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆