scanpy icon indicating copy to clipboard operation
scanpy copied to clipboard

`scanpy.pl.scatter` fails if list is provided for `color`

Open mumichae opened this issue 4 years ago • 3 comments

  • [x] I have checked that this issue has not already been reported.
  • [x] I have confirmed this bug exists on the latest version of scanpy.
  • [ ] (optional) I have confirmed this bug exists on the master branch of scanpy.

Minimal code sample

import numpy as np
import scanpy as sc

adata = sc.datasets.paul15()
adata.obs['batch'] = np.random.randint(3, size=adata.n_obs)
adata.obs['X'] = adata.X[:,1]
adata.obs['Y'] = adata.X[:,2]

sc.pl.scatter(adata, x='X', y='Y', color='batch')  # works
sc.pl.scatter(adata, x='X', y='Y', color=['paul15_clusters', 'batch']) # fails
Traceback (most recent call last):
  File "/home/mumichae/miniconda3/envs/opsca/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-52-9844c466c985>", line 1, in <module>
    sc.pl.scatter(adata, x='X', y='Y', color=['paul15_clusters', 'batch'])
  File "/home/mumichae/miniconda3/envs/opsca/lib/python3.8/site-packages/scanpy/plotting/_anndata.py", line 131, in scatter
    and (color is None or color in adata.obs.keys() or color in adata.var.index)
  File "/home/mumichae/miniconda3/envs/opsca/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 4572, in __contains__
    hash(key)
TypeError: unhashable type: 'list'

Versions

-----
anndata     0.7.6
scanpy      1.8.1
sinfo       0.3.1
-----
IPython             7.25.0
PIL                 8.3.1
anndata             0.7.6
backcall            0.2.0
backend_interagg    NA
beta_ufunc          NA
binom_ufunc         NA
cffi                1.14.6
colorama            0.4.4
console_thrift      NA
cycler              0.10.0
cython_runtime      NA
datalore            NA
dateutil            2.8.1
decorator           4.4.2
defusedxml          0.7.1
h5py                2.10.0
igraph              0.9.6
ipython_genutils    0.2.0
jedi                0.18.0
joblib              1.0.1
kiwisolver          1.3.1
llvmlite            0.36.0
louvain             0.7.0
matplotlib          3.4.2
mpl_toolkits        NA
natsort             7.1.1
nbinom_ufunc        NA
numba               0.53.1
numexpr             2.7.3
numpy               1.18.1
packaging           20.9
pandas              1.3.1
parso               0.8.2
pexpect             4.8.0
pickleshare         0.7.5
pkg_resources       NA
prompt_toolkit      3.0.19
psutil              5.8.0
ptyprocess          0.7.0
pycparser           2.20
pydev_console       NA
pydev_ipython       NA
pydevconsole        NA
pydevd_file_utils   NA
pydevd_plugins      NA
pydevd_tracing      NA
pygments            2.9.0
pynndescent         0.5.4
pyparsing           2.4.7
pytz                2021.1
scanpy              1.8.1
scipy               1.7.0
sinfo               0.3.1
sitecustomize       NA
six                 1.16.0
sklearn             0.24.2
sphinxcontrib       NA
tables              3.6.1
texttable           1.6.4
tqdm                4.61.2
traitlets           5.0.5
umap                0.5.1
wcwidth             0.2.5
-----
Python 3.8.10 | packaged by conda-forge | (default, May 11 2021, 07:01:05) [GCC 9.3.0]
Linux-5.13.10-200.fc34.x86_64-x86_64-with-glibc2.10
16 logical CPU cores
-----
Session information updated at 2021-08-26 10:01

mumichae avatar Aug 26 '21 08:08 mumichae

import seaborn as sns sns.scatterplot(x='X', y='Y', data=adata.obs, hue='paul15_clusters')

guensen0 avatar Aug 30 '21 15:08 guensen0

@mumichae Thanks for letting us know about the error. For now if you need multiple scatter plots on a single figure you can do something like import matplotlib.pyplot as plt f, axs = plt.subplots(2,2) sc.pl.scatter(..., ax=axs[0,0]) sc.pl.scatter(..., ax=axs[1,0]) ...

Koncopd avatar Sep 09 '21 19:09 Koncopd

Interestingly enough, _scatter_obs() supports it so this should be a very quick fix to account for the fact that color is not necessarily a string in scatter_obs().

gtca avatar Oct 17 '24 00:10 gtca