Skip to content

Commit

Permalink
remove colorbar if legend_loc=None for continuous colorbars (scverse#…
Browse files Browse the repository at this point in the history
…1821)

* remove colorbar im legend_loc is None for continuous colorbars

* add show_colorbar argument to scatterplots

* Rework arg, add test

* Release note

Co-authored-by: Isaac Virshup <[email protected]>
  • Loading branch information
AnnaChristina and ivirshup authored Mar 31, 2022
1 parent b8c2cf5 commit 19d2ab2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/1.9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
3 changes: 3 additions & 0 deletions scanpy/plotting/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions scanpy/plotting/_tools/scatterplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Binary file added scanpy/tests/_images/master_no_colorbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions scanpy/tests/test_embedding_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 19d2ab2

Please sign in to comment.