umap icon indicating copy to clipboard operation
umap copied to clipboard

plt.colorbar support for umap.plot.points

Open lucharo opened this issue 4 years ago • 5 comments

Hello, umap.plot.points does not seem to support a colour bar when colouring the points with continuous values, is this a design choice?

I've tried

umap.plot.points(umap_object, values = array_of_numbers)
plt.colorbar()

With no success.

I think it'd be quite handy to have such functionality

lucharo avatar Aug 21 '20 18:08 lucharo

It wasn't an explicit design choice to avoid it. I would have hoped that the plt.colorbar would have worked. My matplotlib is only good enough to do some bits however. It might be possible to add colorbar functionality into the API so it does work properly. I'll see if I can get to that at some point. Admittedly the umap.plot was never meant to be a comprehensive plotting solution -- just a simple and easy to use one. Worst case you can certainly just break out raw matplotlib to get the job done with a few extra lines of code.

lmcinnes avatar Aug 21 '20 19:08 lmcinnes

I understand, matplotlib is often a pain to work with. I have a short function that mostly emulates umap.plot.points with colorbar and subplots support. I’ll pass it on here soon.

I also appreciate that the umap.plot is there for more than the points() function. The connectivity and diagnostic functions seem very useful though I haven’t gotten to use them yet!

Luis Chaves


From: Leland McInnes [email protected] Sent: Friday, August 21, 2020 9:52:49 PM To: lmcinnes/umap [email protected] Cc: Chaves, Luis [email protected]; Author [email protected] Subject: Re: [lmcinnes/umap] plt.colorbar support for umap.plot.points (#485)

This email from [email protected] originates from outside Imperial. Do not click on links and attachments unless you recognise the sender. If you trust the sender, add them to your safe senders listhttps://spam.ic.ac.uk/SpamConsole/Senders.aspx to disable email stamping for this address.

It wasn't an explicit design choice to avoid it. I would have hoped that the plt.colorbar would have worked. My matplotlib is only good enough to do some bits however. It might be possible to add colorbar functionality into the API so it does work properly. I'll see if I can get to that at some point. Admittedly the umap.plot was never meant to be a comprehensive plotting solution -- just a simple and easy to use one. Worst case you can certainly just break out raw matplotlib to get the job done with a few extra lines of code.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/lmcinnes/umap/issues/485#issuecomment-678464142, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALNMCQY4N2VX6LTOWG3UKCDSB3GBDANCNFSM4QHR34IA.

lucharo avatar Aug 21 '20 21:08 lucharo

@lc5415, would you mind showing your emulating function?

jondo avatar Dec 22 '20 16:12 jondo

Hi @lucharo , not sure if your issue is still outstanding, but I recently ran into a similar problem myself. I implemented a quick workaround which I will share below.

First create a pcolormesh for the series which you will be coloring with umap:

psm = plt.pcolormesh([data_series, data_series], cmap=mpl.cm.get_cmap('viridis'))

Then create the umap points plot as usual, ensuring the data series and color theme are the same:

ax = umap.plot.points(mapper, values=data_series, theme='viridis')

Now create the colorbar from the pcolormesh and draw alongside the umap figure:

cb = plt.colorbar(psm, ax=ax)

From here you can edit the colorbar object as you please to change the label size, tick colors, etc ...

Hope this helps!

zachschillaci27 avatar Mar 07 '22 09:03 zachschillaci27

I tried the above but was having issues due to the colormesh causing a strange layout / colliding data. This is much neater, faster and just as easy.

fig, ax = plt.subplots()
cmap = "rainbow"
# create a scalar colour map for values
norm = colors.Normalize(values.min(), values.max())
scalar_map = cm.ScalarMappable(norm=norm, cmap=cmap)  # type: ignore
# plot using umaps helper function
plot.points(mapper, values=values, ax=ax, cmap=cmap)
# create a colorbar
cbar = fig.colorbar(scalar_map, ax=ax)  # type: ignore

m4gpi avatar Jul 27 '22 14:07 m4gpi