Skip to content

plot_scatter throws error when sparse layers used for color #700

Closed

Description

@ivirshup

If you try to color a scatter plot by gene expression in a layer, but the layer is sparse, an error is thrown. This occurs on both current release and master.

Code to reproduce:

import scanpy as sc

pbmc = sc.datasets.pbmc68k_reduced()
pbmc.layers["sparse"] = pbmc.raw.X
sc.pl.pca(pbmc, color=["HES4"], layer="sparse") 
Traceback:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-89244dc07987> in <module>
      3 pbmc = sc.datasets.pbmc68k_reduced()
      4 pbmc.layers["sparse"] = pbmc.raw.X
----> 5 sc.pl.pca(pbmc, color=["HES4"], layer="sparse")

~/miniconda3/envs/scanpy-conda/lib/python3.7/site-packages/scanpy/plotting/_tools/scatterplots.py in pca(adata, **kwargs)
    410     If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
    411     """
--> 412     return plot_scatter(adata, 'pca', **kwargs)
    413 
    414 

~/miniconda3/envs/scanpy-conda/lib/python3.7/site-packages/scanpy/plotting/_tools/scatterplots.py in plot_scatter(adata, basis, color, gene_symbols, use_raw, sort_order, edges, edges_width, edges_color, arrows, arrows_kwds, groups, components, layer, projection, color_map, palette, size, frameon, legend_fontsize, legend_fontweight, legend_loc, ncols, hspace, wspace, title, show, save, ax, return_fig, **kwargs)
    199                 _data_points[:, 0], _data_points[:, 1],
    200                 marker=".", c=color_vector, rasterized=settings._vector_friendly,
--> 201                 **kwargs,
    202             )
    203 

~/miniconda3/envs/scanpy-conda/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1587     def inner(ax, *args, data=None, **kwargs):
   1588         if data is None:
-> 1589             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1590 
   1591         bound = new_sig.bind(ax, *args, **kwargs)

~/miniconda3/envs/scanpy-conda/lib/python3.7/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
   4444             self._parse_scatter_color_args(
   4445                 c, edgecolors, kwargs, xshape, yshape,
-> 4446                 get_next_color_func=self._get_patches_for_fill.get_next_color)
   4447 
   4448         if plotnonfinite and colors is None:

~/miniconda3/envs/scanpy-conda/lib/python3.7/site-packages/matplotlib/axes/_axes.py in _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape, get_next_color_func)
   4249                 isinstance(c, str) or
   4250                 (isinstance(c, collections.abc.Iterable) and
-> 4251                      len(c) > 0 and
   4252                      isinstance(cbook.safe_first_element(c), str))):
   4253             c_array = None

~/miniconda3/envs/scanpy-conda/lib/python3.7/site-packages/scipy/sparse/base.py in __len__(self)
    294     # non-zeros is more important.  For now, raise an exception!
    295     def __len__(self):
--> 296         raise TypeError("sparse matrix length is ambiguous; use getnnz()"
    297                         " or shape[0]")
    298 

TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]

Activity

  1. fidelram commented on Jun 24, 2019

    @fidelram
    Collaborator

    If I remember correctly, problem is caused by this scverse/anndata#145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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