spatialdata-plot icon indicating copy to clipboard operation
spatialdata-plot copied to clipboard

Unable to set vmin vmax when plotting vector data

Open wangjiawen2013 opened this issue 1 year ago • 3 comments
trafficstars

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.

wangjiawen2013 avatar Aug 16 '24 08:08 wangjiawen2013

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

LucaMarconato avatar Aug 16 '24 12:08 LucaMarconato

@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

wangjiawen2013 avatar Aug 17 '24 08:08 wangjiawen2013

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.

LucaMarconato avatar Aug 19 '24 09:08 LucaMarconato

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?

olivermccallion avatar Sep 01 '24 14:09 olivermccallion

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

wangjiawen2013 avatar Sep 02 '24 05:09 wangjiawen2013

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?

olivermccallion avatar Sep 02 '24 10:09 olivermccallion

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

wangjiawen2013 avatar Sep 03 '24 01:09 wangjiawen2013

@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.

wangjiawen2013 avatar Sep 09 '24 07:09 wangjiawen2013

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

timtreis avatar Sep 09 '24 11:09 timtreis

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

LucaMarconato avatar Sep 09 '24 11:09 LucaMarconato

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.

timtreis avatar Sep 09 '24 18:09 timtreis

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 avatar Sep 10 '24 01:09 wangjiawen2013

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.

wangjiawen2013 avatar Sep 10 '24 01:09 wangjiawen2013

Hey @wangjiawen2013,

if I understand correctly, this is your usecase?

image

Or did I misunderstand something?

timtreis avatar Sep 10 '24 15:09 timtreis

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

timtreis avatar Sep 10 '24 15:09 timtreis

@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` ?

wangjiawen2013 avatar Sep 11 '24 01:09 wangjiawen2013

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

timtreis avatar Sep 12 '24 01:09 timtreis

Picking this up today as my first PR getting back to development hehe. Will report back

melonora avatar Oct 03 '24 11:10 melonora

I take it that we should perhaps also add a notebook explaining the matplotlib norm.

melonora avatar Oct 03 '24 12:10 melonora

Agree. We can add this to the static plot workshop notebook and maybe a few extra notebooks in the spatialdata repo.

LucaMarconato avatar Oct 03 '24 12:10 LucaMarconato

ok fixed it, just testing now, but here already with vmin / vmax / clip and without: image

image

melonora avatar Oct 03 '24 16:10 melonora

@wangjiawen2013 if you would like you can check #368 to see whether it fixes the issue for you. This only fixes it for the matplotlib render. Once the solution is in I will also create a fix for the datashader.

melonora avatar Oct 04 '24 09:10 melonora

@melonora Hi, I am trying to test it. Is this fixes released in current spatialplot version ?

wangjiawen2013 avatar Oct 17 '24 02:10 wangjiawen2013

Hi! This is not in the current release yet I believe. Thanks for your willingness to test! If you want you can test from main.

melonora avatar Oct 21 '24 07:10 melonora

Hey @wangjiawen2013, it's not yet released but we'll have one this week that also includes a bunch of datashader fixes :)

timtreis avatar Oct 21 '24 14:10 timtreis