umap icon indicating copy to clipboard operation
umap copied to clipboard

Making a subplot figure with umap.plot objects

Open lucharo opened this issue 4 years ago • 3 comments

I know that umap.plot return matplotlib.pyplot axes but I cannot get my head around making a subplot (as in a 2D array of umap.plots)

I am trying:

import matplotlib.pyplot as plt
import umap.plot
import umap
import sklearn.datasets

pendigits = sklearn.datasets.load_digits()

fig, axs = plt.subplots(2,2,figsize = (10,10))
for r in range(1):
    for c in range(1):
        mapper = umap.UMAP(n_neighbors = ((r+c) + 1)*5, spread = ((r+c)+1)*2).fit(pendigits.data)
        axs[r,c] = umap.plot.points(mapper, labels = pendigits.target)

plt.show()
fig.savefig('fig.png')

I am simply getting one umap.plot and an empty 2x2 grid, what am I doing wrong?

lucharo avatar Aug 19 '20 16:08 lucharo

I think we actually need to change the plot API to accept an axis argument to make this work easily in the manner you are trying here. I suspect, since it returns an axis, that there is a way to coerce matplotlib to do what you want, but it is probably non-obvious. You may want to try the matplotlib support options and see if they have a way to integrate a returned axis into a subplot structure.

lmcinnes avatar Aug 21 '20 13:08 lmcinnes

In the end I went for a homemade solution and re-wrote a UMAP.plot function that takes in an ax argument. I may look into the code to add that functionality once I am done with my current project next week

lucharo avatar Aug 21 '20 18:08 lucharo

I just forked the repository planning to add this functionality (I was using an older version) -- but I discovered it was already done in 050b73766959a6c1ef591b3142fef01d6f3139f3.

So, I guess you can already close this one! :)

Thanks for all you do!

celinesin avatar Dec 30 '21 19:12 celinesin