Skip to content

sdata.pl.render_shapes/render_labels how to define the cell color based on cell type annotation #374

@liuyang2006

Description

@liuyang2006

Hi,

I am trying to assign different color to different cell type annotation in 'leiden' field of my Scanpy Object, however, the plotting function below always not working:

Script

sdata.pl.render_shapes(
    "cell_circles", color='leiden'
).pl.show(title=f"cell types", coordinate_systems="global", figsize=(6, 6))

Error message:

[/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/spatialdata_plot/pl/utils.py:772](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/spatialdata_plot/pl/utils.py#line=771): FutureWarning: The default value of 'ignore' for the `na_action` parameter in pandas.Categorical.map is deprecated and will be changed to 'None' in a future version. Please set na_action to the desired value to avoid seeing this warning
  color_vector = color_source_vector.map(color_mapping)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[60], line 1
----> 1 sdata.pl.render_shapes(
      2     "cell_circles", color='leiden'
      3 ).pl.show(title=f"cell types", coordinate_systems="global", figsize=(6, 6))

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/spatialdata_plot/pl/basic.py:903](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/spatialdata_plot/pl/basic.py#line=902), in PlotAccessor.show(self, coordinate_systems, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, na_in_legend, colorbar, wspace, hspace, ncols, frameon, figsize, dpi, fig, title, share_extent, pad_extent, ax, return_ax, save)
    898     wanted_elements, wanted_shapes_on_this_cs, wants_shapes = _get_wanted_render_elements(
    899         sdata, wanted_elements, params_copy, cs, "shapes"
    900     )
    902     if wanted_shapes_on_this_cs:
--> 903         _render_shapes(
    904             sdata=sdata,
    905             render_params=params_copy,
    906             coordinate_system=cs,
    907             ax=ax,
    908             fig_params=fig_params,
    909             scalebar_params=scalebar_params,
    910             legend_params=legend_params,
    911         )
    913 elif cmd == "render_points" and has_points:
    914     wanted_elements, wanted_points_on_this_cs, wants_points = _get_wanted_render_elements(
    915         sdata, wanted_elements, params_copy, cs, "points"
    916     )

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/spatialdata_plot/pl/render.py:191](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/spatialdata_plot/pl/render.py#line=190), in _render_shapes(sdata, render_params, coordinate_system, ax, fig_params, scalebar_params, legend_params)
    189 # in case we are coloring by a column in table
    190 if col_for_color is not None and col_for_color not in sdata_filt.shapes[element].columns:
--> 191     sdata_filt.shapes[element][col_for_color] = (
    192         color_vector if color_source_vector is None else color_source_vector
    193     )
    194 # Render shapes with datashader
    195 color_by_categorical = col_for_color is not None and color_source_vector is not None

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/geopandas/geodataframe.py:1819](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/geopandas/geodataframe.py#line=1818), in GeoDataFrame.__setitem__(self, key, value)
   1814     except TypeError:
   1815         warnings.warn(
   1816             "Geometry column does not contain geometry.",
   1817             stacklevel=2,
   1818         )
-> 1819 super().__setitem__(key, value)

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/frame.py:4299](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/frame.py#line=4298), in DataFrame.__setitem__(self, key, value)
   4296     self._setitem_array([key], value)
   4297 else:
   4298     # set column
-> 4299     self._set_item(key, value)

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/frame.py:4512](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/frame.py#line=4511), in DataFrame._set_item(self, key, value)
   4502 def _set_item(self, key, value) -> None:
   4503     """
   4504     Add series to DataFrame in specified column.
   4505 
   (...)
   4510     ensure homogeneity.
   4511     """
-> 4512     value, refs = self._sanitize_column(value)
   4514     if (
   4515         key in self.columns
   4516         and value.ndim == 1
   4517         and not isinstance(value.dtype, ExtensionDtype)
   4518     ):
   4519         # broadcast across multiple columns if necessary
   4520         if not self.columns.is_unique or isinstance(self.columns, MultiIndex):

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/frame.py:5253](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/frame.py#line=5252), in DataFrame._sanitize_column(self, value)
   5250     return _reindex_for_setitem(value, self.index)
   5252 if is_list_like(value):
-> 5253     com.require_length_match(value, self.index)
   5254 arr = sanitize_array(value, self.index, copy=True, allow_2d=True)
   5255 if (
   5256     isinstance(value, Index)
   5257     and value.dtype == "object"
   (...)
   5260     # TODO: Remove kludge in sanitize_array for string mode when enforcing
   5261     # this deprecation

File [/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/common.py:571](https://ondemand.carc.usc.edu/project/sli68423_1316/users/yang/conda/envs/py39/lib/python3.9/site-packages/pandas/core/common.py#line=570), in require_length_match(data, index)
    567 """
    568 Check the length of data matches the length of the index.
    569 """
    570 if len(data) != len(index):
--> 571     raise ValueError(
    572         "Length of values "
    573         f"({len(data)}) "
    574         "does not match length of index "
    575         f"({len(index)})"
    576     )

ValueError: Length of values (154472) does not match length of index (162254)

Thank you!

Best,
Yang

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions