scverse / spatialdata-plot

Static plotting for spatialdata
BSD 3-Clause "New" or "Revised" License
27 stars 13 forks source link

Unable to set vmin vmax when plotting vector data #324

Open wangjiawen2013 opened 4 weeks ago

wangjiawen2013 commented 4 weeks ago

Hi, I want set the vmin and vmax of the color, but failed. This is the first method:

norm = matplotlib.colors.Normalize(vmin=-1.0, vmax=0.4)
sdata.pl.render_shapes("cell_circles", norm=norm, color="NOTCH2", method="matplotlib").pl.show(coordinate_systems= "physical", figsize=(10,4))

The resulting plot is here: image

This is the second method: sdata.pl.render_shapes("cell_circles", vmin=-1, vmax=1, color="NOTCH2", method="matplotlib").pl.show(coordinate_systems= "physical", figsize=(10,4)) The resulting plot is here: image

vmin and vmax didn't work in both cases.

LucaMarconato commented 4 weeks ago

Hi, this is related to this https://github.com/scverse/spatialdata-plot/issues/303, please try using clip=True.

wangjiawen2013 commented 4 weeks ago

@LucaMarconato vmin and max only works for pl.render_images, but don't work for pl.render_shapes. And, even for pl.render_images, there is no colorbar at all ! As you can see from the spatialdata xenium tutorial: image

Here is my dataset, there is still no colorbar !

output

LucaMarconato commented 3 weeks ago

Thanks for the observation. I have changed the title to emphasize that the issue is also open for vector data (the linked issue is indeed for raster data). Regarding the missing colorbar, I already reported the issue here: https://github.com/scverse/spatialdata-plot/issues/304, so we can follow up there. The developer assigned to the issue is temporarily not available for the next 14 days because of a pressing deadline.

olivermccallion commented 1 week ago

Running into the same problem with spatialdata-plot version 0.2.6.dev3+gffd4a1f where:

region.pl.render_shapes(
    "cell_boundaries", color = "CD3E", vmax = 11, clip=True).pl.show(title="CD3", coordinate_systems="global", ax=axes[1])
region.pl.render_shapes(
    "cell_boundaries", color = "CD3E", vmax = 11).pl.show(title="CD3", coordinate_systems="global", ax=axes[1])

and

norm = matplotlib.colors.Normalize(vmin=0.0, vmax=2000)
region.pl.render_shapes(
    "cell_boundaries", color = "CD3E", norm=norm, clip=True).pl.show(title="CD3", coordinate_systems="global", ax=axes[1])

do not alter the colorbar limits / shape shading. Is there any update on the best way to fix this?

wangjiawen2013 commented 1 week ago

@olivermccallion Here is a workaround, spatialdata doesn't support vmin and vmax generally: https://github.com/scverse/spatialdata-plot/issues/304

olivermccallion commented 1 week ago

Thanks @wangjiawen2013

I'm trying to achieve the same behaviour as your opening post, where you were looking to specify min and max values to the render_shapes() colorbar when the shapes are shaded by a particular feature -- not so worried about having a color bar for raster data / render_images(). #304 seems to be a work around for this second issue. Is there a solution/workaround for the first?

wangjiawen2013 commented 1 week ago

Unfortunately not. https://github.com/scverse/spatialdata-plot/issues/304#issuecomment-2227415168

wangjiawen2013 commented 5 days ago

@timtreis @olivermccallion I have tested the latest spatialdata-plot (v0.2.6) vmin and vmax works only in pl.render_images, but still don't work in pl.render_shapes .

image

As you can see, vmin and vmax changed the min and max number on the colorbar, but the color in the figures was not changed at all ! It is expected to be more brighter when decreasing vmax, which is the case in pl.render_images.

timtreis commented 5 days ago

I'm surprised it even works in render_images :D Thought I had removed it. Could you try creating a matplotlib Normalise object with your desired vmin/vmax values and pass that to norm? That should work

LucaMarconato commented 4 days ago

@timtreis I would either remove vmin vmax, or create a Normalise object if vmin vmax are passed.

timtreis commented 4 days ago

Will check later but I do think we did remove them when migrating to the norm-based logic. I'd be tentatively against accepting both as input because then we have a lot of ambiguity beween cmap, norm and whatever other things the user specifies. If we just take in a matplotlib.colors.Normalise object, it's definite.

wangjiawen2013 commented 4 days ago

I think you should keep vmin and vmax. We can use these two parameters to clip/truncate the colorbar to make the figure more saturated. It's different from norm. We use norm to normalize the colorbar, but don't clip/truncate it. So vmin/vmax have different functions with norm. Sometimes we need to use vmin/vmax to beautify the figure, but sometimes we need norm.

wangjiawen2013 commented 4 days ago

I'm surprised it even works in render_images :D Thought I had removed it. Could you try creating a matplotlib Normalise object with your desired vmin/vmax values and pass that to norm? That should work

We created a matplotlib Normalise object with the desired vmin/vmax values and pass that to norm, but this is not what we need. What we need is to clip/truncate the colorbar, not normalization. Maybe you can refer to scanpy (such as help(sc.pl.umap) for vmin and vmax.

timtreis commented 3 days ago

Hey @wangjiawen2013,

if I understand correctly, this is your usecase?

image

Or did I misunderstand something?

timtreis commented 3 days ago

The current norm based approach allows you to make use of the other mpl.colors Normalisation methods, like f.e. this:

image
wangjiawen2013 commented 3 days ago

@timtreis Yes, this is what I mean ! I am not familiar with matplotlib norm and don't know the clip parameter. I tested my data and found this works in pl.render_images, while don't work for pl.render_shapes, could you extend norm to pl.render_shapes` ?

timtreis commented 2 days ago

Oh weird, I'd think it should also work for render_shapes. I'll investigate and ping you