Repository: scverse/scanpy

Issue:
Better document `legend_loc=None` for embedding plots
<!-- What kind of feature would you like to request? -->
- [x ] Additional function parameters / changed functionality / changed defaults?
- [ ] New analysis tool: A simple analysis tool you have been using and are missing in `sc.tools`?
- [ ] New plotting function: A kind of plot you would like to seein `sc.pl`?
- [ ] External tools: Do you know an existing package that should go into `sc.external.*`?
- [ ] Other?

<!-- Please describe your wishes below: -->
It is a lovely tool, but it would be great to be able to not plot figure legends in, e.g. sc.pl.umap, so that all plots (regardless of what annotation is being colored) are exactly the same size. Perhaps there is already a way to do this, but I've yet to find it. 

Thanks!



Discussion:
You can pass `legend_loc=None`.

Thanks for noting this. Looking at the docs, I can definitely see how this isn't clear.

```
legend_loc : str, optional (default: 'right margin')
    Location of legend, either `'on data'`, `'right margin'` or a valid keyword
    for the `loc` parameter of :class:`~matplotlib.legend.Legend`.
```
Tada! Works perfectly. 
Thanks for the tip, and may this GitHub comment serve as substrate to all future google searches on this issue. 
So one small update here -- it works like a charm for categorical variables, but not for continuous variables.
e.g.
> sc.pl.umap(testData, save = fileName, color='CCL5',s=50,frameon=False,legend_loc = None)

Still gives something like a legend:
![image](https://user-images.githubusercontent.com/10536275/99786010-40234a80-2b1e-11eb-83ab-77c9341dab05.png)

Presumably this is because the color strip on the right is not actually a legend in the underlying matplotlib?

Resolving patch:
diff --git a/scanpy/plotting/_docs.py b/scanpy/plotting/_docs.py
--- a/scanpy/plotting/_docs.py
+++ b/scanpy/plotting/_docs.py
@@ -73,6 +73,9 @@
 legend_fontoutline
     Line width of the legend font outline in pt. Draws a white outline using
     the path effect :class:`~matplotlib.patheffects.withStroke`.
+colorbar_loc
+    Where to place the colorbar for continous variables. If `None`, no colorbar
+    is added.
 size
     Point size. If `None`, is automatically computed as 120000 / n_cells.
     Can be a sequence containing the size for each cell. The order should be
diff --git a/scanpy/plotting/_tools/scatterplots.py b/scanpy/plotting/_tools/scatterplots.py
--- a/scanpy/plotting/_tools/scatterplots.py
+++ b/scanpy/plotting/_tools/scatterplots.py
@@ -89,6 +89,7 @@ def embedding(
     legend_fontweight: Union[int, _FontWeight] = 'bold',
     legend_loc: str = 'right margin',
     legend_fontoutline: Optional[int] = None,
+    colorbar_loc: Optional[str] = "right",
     vmax: Union[VBound, Sequence[VBound], None] = None,
     vmin: Union[VBound, Sequence[VBound], None] = None,
     vcenter: Union[VBound, Sequence[VBound], None] = None,
@@ -447,9 +448,10 @@ def embedding(
                 na_in_legend=na_in_legend,
                 multi_panel=bool(grid),
             )
-        else:
-            # TODO: na_in_legend should have some effect here
-            pl.colorbar(cax, ax=ax, pad=0.01, fraction=0.08, aspect=30)
+        elif colorbar_loc is not None:
+            pl.colorbar(
+                cax, ax=ax, pad=0.01, fraction=0.08, aspect=30, location=colorbar_loc
+            )
 
     if return_fig is True:
         return fig


Test patch:
diff --git a/scanpy/tests/_images/master_no_colorbar.png b/scanpy/tests/_images/master_no_colorbar.png
new file mode 100644
Binary files /dev/null and b/scanpy/tests/_images/master_no_colorbar.png differ
diff --git a/scanpy/tests/test_embedding_plots.py b/scanpy/tests/test_embedding_plots.py
--- a/scanpy/tests/test_embedding_plots.py
+++ b/scanpy/tests/test_embedding_plots.py
@@ -12,6 +12,7 @@
 import scanpy as sc
 
 from scanpy.tests.test_plotting import ROOT, FIGS, HERE
+import scanpy.tests._data._cached_datasets as datasets
 
 MISSING_VALUES_ROOT = ROOT / "embedding-missing-values"
 MISSING_VALUES_FIGS = FIGS / "embedding-missing-values"
@@ -272,6 +273,15 @@ def test_dimensions_same_as_components(adata, tmpdir, check_same_image):
     check_same_image(dims_pth, comp_pth, tol=5)
 
 
+def test_embedding_colorbar_location(image_comparer):
+    save_and_compare_images = image_comparer(ROOT, FIGS, tol=15)
+    adata = datasets.pbmc3k_processed().raw.to_adata()
+
+    sc.pl.pca(adata, color="LDHB", colorbar_loc=None)
+
+    save_and_compare_images("master_no_colorbar")
+
+
 # Spatial specific