我正在尝试为3D图创建图例。在制作图例时,我不完全理解手柄的使用。
我已经关注了两个先前发布的问题Matplotlib: Annotating a 3D scatter plot和Matplotlib scatter plot legend
我不了解的是在使用数据框时如何重新创建其工作流程。
#Creating the Graph:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# %matplotlib notebook
threedee = plt.figure(figsize = (10,10)).gca(projection='3d')
threedee.scatter(df_pca_test["PC1"],
df_pca_test["PC2"],
df_pca_test["PC3"],
c = y["y_num"] )
threedee.set_xlabel('PC1', fontsize = 12)
threedee.set_ylabel('PC2', fontsize = 12)
threedee.set_zlabel('PC3', fontsize = 12)
plt.show()
#My data:
[df_pca_test] # Contains three columns of PCA results
y[y_num] # Contains my labels in numerical format (1,2,3,4,0)
#First thing I tried:
plt.legend() # Returned: No handles with labels found to put in legend
#Second thing I tried:
plt.legend((1, 2, 3, 4, 0),
('A', 'B', 'C', 'Test', 'G'),
scatterpoints=1,
loc='lower left',
fontsize=8)
# Returned: No handles with labels found to put in legend
理想情况下,我希望图形具有图例,该图例具有“ A”,“ B”,“ C”,“ Test”和“ G”,而不是具有数字1、2、3、4的y_num ,0。