1

Below is the code for scatter plot.

  for_tsne = np.hstack((X_embedding, y.values.reshape(-1,1)))
  for_tsne_df = pd.DataFrame(data=for_tsne, columns= 
                ['Dimension_x','Dimension_y','Labels'])
  colors = {0:'red', 1:'blue', 2:'yellow'}
  #colors = ['red','blue']
  plt.scatter(for_tsne_df['Dimension_x'], 
  for_tsne_df['Dimension_y'],c=for_tsne_df['Labels'].apply(lambda x: 
                               colors[x]))
  plt.title("TSNE with BOW encoding of project_title")
  plt.xlabel("Dimension_x")
  plt.ylabel("Dimension_y")
  plt.legend()
  plt.show()`

How can I add legend? Above code is displaying only one label as Dimension_y.

Tanvir
  • 154
  • 14
nkm
  • 53
  • 1
  • 8

1 Answers1

1

One option is to assign a label to plt.scatter(). The legend will only appear if you plot the data with a label:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random(size=(100))
y = np.random.random(size=(100))
x1 = np.random.random(size=(100))
y1 = np.random.random(size=(100))

plt.scatter(x,y, label='sample 1')
plt.scatter(x1,y1, label='sample 2')
plt.title("TSNE with BOW encoding of project_title")
plt.xlabel("Dimension_x")
plt.ylabel("Dimension_y")
plt.legend()
plt.show()

Plot with legend

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
ml4294
  • 2,559
  • 5
  • 24
  • 24