spatialdata-plot
spatialdata-plot copied to clipboard
Unable to set vmin vmax when plotting vector data
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:
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:
vmin and vmax didn't work in both cases.
Hi, this is related to this https://github.com/scverse/spatialdata-plot/issues/303, please try using clip=True.
@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:
Here is my dataset, there is still no colorbar !
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.
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 Here is a workaround, spatialdata doesn't support vmin and vmax generally: https://github.com/scverse/spatialdata-plot/issues/304
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?
Unfortunately not. https://github.com/scverse/spatialdata-plot/issues/304#issuecomment-2227415168
@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 .
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.
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 I would either remove vmin vmax, or create a Normalise object if vmin vmax are passed.
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.
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.
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.
Hey @wangjiawen2013,
if I understand correctly, this is your usecase?
Or did I misunderstand something?
The current norm based approach allows you to make use of the other mpl.colors Normalisation methods, like f.e. this:
@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` ?
Oh weird, I'd think it should also work for render_shapes. I'll investigate and ping you
Picking this up today as my first PR getting back to development hehe. Will report back
I take it that we should perhaps also add a notebook explaining the matplotlib norm.
Agree. We can add this to the static plot workshop notebook and maybe a few extra notebooks in the spatialdata repo.
ok fixed it, just testing now, but here already with vmin / vmax / clip and without:
@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 Hi, I am trying to test it. Is this fixes released in current spatialplot version ?
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.
Hey @wangjiawen2013, it's not yet released but we'll have one this week that also includes a bunch of datashader fixes :)