0

I'm trying to plot tSNE 2d results,

plt.scatter(x=S_embedded[:,0], y=S_embedded[:,1], c=labels_sample, cmap='tab20')
handles = []
for i, name in clusters_dict.items():
    handles.append(mpatches.Patch(**color=???**, label=name))

plt.legend(handles=handles, bbox_to_anchor=(1.5,0.5), loc="right")
plt.show()

labels_sample is an array that contains the labels of the points, S_embedded is the 2d tSNE projection results. clusters_dict is a dictionary, the keys are the possible values in labels_sample, and the valued are the name for each label ({0: 'a', 1:'c',...}).

My question:

  • how can I specify the right color when adding the patches (to be consistent with the colors in the scatter plot)?
obar
  • 357
  • 6
  • 17
  • 1
    Searching with `matplotlib legend outside of plot area` has a number of results. Your question is probably a duplicate of one of them. Pick one and we'll mark yours as a duplicate. – wwii Oct 04 '20 at 18:07
  • 1
    Looks like you asked three questions - that's a bit too broad, please focus on one at a time. – wwii Oct 04 '20 at 18:08
  • 1
    Does this answer your question? [Getting individual colors from a color map in matplotlib](https://stackoverflow.com/questions/25408393/getting-individual-colors-from-a-color-map-in-matplotlib). - You specified a `c` and `cmap` argument for .scatter. You stated `clusters_dict` has the `labels_sample` values which you used for the `c` argument. Use those values to get the color from the colormap. – wwii Oct 04 '20 at 18:19
  • I think you are able to vote to close your own question as a duplicate. You can try it if you want. – wwii Oct 04 '20 at 18:26
  • https://matplotlib.org/tutorials/index.html#tutorials-colors – wwii Oct 04 '20 at 18:28

0 Answers0