I am making an interactive UMAP plot, where you can hover the mouse over a datapoint to view the sample ID.
My code works, showing a plot colour coded by class and with the hover feature working
However, I can't figure out how to get it to show a legend for the classes and their colour. This was added automatically when I made non-interative UMAP plots.
mapper = umap.UMAP(n_neighbors=50, min_dist=0.5).fit(pretrained_df.iloc[:,2:130])
hover_data = pd.DataFrame({'index':np.arange(len(pretrained_df)),
'label':pretrained_df.sampleID[:len(pretrained_df)]})
umap.plot.output_notebook()
#uses the 'sampleID' column as the label
p = umap.plot.interactive(mapper, labels=pretrained_df.sampleID, hover_data=hover_data, theme = 'fire',
point_size=1) ### MAKE POINT SIZE BIGGER TO HOVER MORE EASILY
umap.plot.show(p)
I am plotting a df called pretrained_df where the frst column is the sample ID, the second is the samples class, and the ramaining columns are features.
Image of UMAP interactive plot with legend color-coded by label name
I've been stuck on this too so hopefully this helps. They do suggest in the UMAP docs to default to bokeh. For reference this is bokeh 2.4.3 and umap 0.5.3 compiled with Python 3.7. My input is like yours: a dataframe of sample names, sample classes and features. For context, I first set up my UMAP embedding, and turn it into a dataframe:
Then this is the bokeh portion (please ignore the legend click policy, I'm still working on it!):
Finally, my bokeh imports. Not all are used, this is WIP: