diff --git a/docs/release-notes/1.9.0.md b/docs/release-notes/1.9.0.md index 8a116d2e47..f58d8bc4b8 100644 --- a/docs/release-notes/1.9.0.md +++ b/docs/release-notes/1.9.0.md @@ -9,6 +9,7 @@ - `_choose_representation` now subsets the provided representation to n_pcs, regardless of the name of the provided representation (should affect mostly {func}`~scanpy.pp.neighbors`) {pr}`2179` {smaller}`I Virshup` {smaller}`PG Majev` - Embedding plots now have a `dimensions` argument, which lets users select which dimensions of their embedding to plot and uses the same broadcasting rules as other arguments {pr}`1538` {smaller}`I Virshup` - Number of variables plotted with {func}`~scanpy.pl.pca_loadings` can now be controlled with `n_points` argument. Additionally, variables are no longer repeated if the anndata has less than 30 variables {pr}`2075` {smaller}`Yves33` +- Embedding plots can now pass `colorbar_loc` to specify the location of colorbar legend, or pass `None` to not show a colorbar {pr}`1821` {smaller}`A Schaar` {smaller}`I Virshup` ```{rubric} Experimental module ``` diff --git a/scanpy/plotting/_docs.py b/scanpy/plotting/_docs.py index 947ed17bf3..e447c34870 100644 --- 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 index ce79f80cfa..060705345e 100644 --- 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 diff --git a/scanpy/tests/_images/master_no_colorbar.png b/scanpy/tests/_images/master_no_colorbar.png new file mode 100644 index 0000000000..487d9adbde 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 index 62eb9f6e0d..43c5906e66 100644 --- 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