0

I am trying to create a legend for my 3D plot. I do not completely understand the use of a handle when it comes to making a legend.

I have followed two previously posted questions Matplotlib: Annotating a 3D scatter plot and Matplotlib scatter plot legend

What I do not understand is how to recreate their workflow when using a Dataframe.

 #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

Ideally, I would like my graph to have a legend that has 'A', 'B', 'C', 'Test', and 'G' instead of y_num which has the numbers 1, 2, 3, 4, 0.

Pythoner
  • 460
  • 6
  • 23
  • I don't see the difference between using a dataframe or using a numpy array or a list. So existing answers should work. – ImportanceOfBeingErnest Aug 09 '19 at 14:46
  • Neither do I, but I am not able to get it to work for some reason...any thoughts as to why? – Pythoner Aug 09 '19 at 15:06
  • As I see it, you haven't tried any of the solutions used in those answers. That would explain why it doesn't work here. – ImportanceOfBeingErnest Aug 09 '19 at 15:28
  • As I see it, I have attempted both unsuccessfully which is why I asked for assistance on understanding it better. – Pythoner Aug 09 '19 at 15:41
  • What I mean is that you are not showing those attemps in the quesiton (but rather some attempts that are not shown in those questions). This makes it hard to see where your problem comes from. If instead of having someone explain the matter you would just like to see a solution, consider that people need a [mcve] to be motivated to play the code-writing-service here. – ImportanceOfBeingErnest Aug 09 '19 at 16:50
  • Also, check [this question](https://stackoverflow.com/questions/21654635/scatter-plots-in-pandas-pyplot-how-to-plot-by-category) which has a lot more high-quality answers. – ImportanceOfBeingErnest Aug 09 '19 at 16:52

0 Answers0