Repository: scverse/scanpy

Issue:
PCA color fails when return_fig = True and annotate_var_explained = True
### Please make sure these conditions are met

- [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.
- [X] (optional) I have confirmed this bug exists on the master branch of scanpy.

### What happened?

When using `sc.pl.pca` with a combination of:

- Enabled `annotate_var_explained`
- Enabled `return_fig`
- A colored-by column

The plotting function fails because it is trying to set the name on color bars. Specifically, [this loop](https://github.com/scverse/scanpy/blob/master/scanpy/plotting/_tools/scatterplots.py#L893) will encounter both, the actual plot as well as the colorbar. When it does hit the colorbar, it tries to look it up in the `label_dict`, which will fail (those axes are labeled `<colorbar>`).

### Minimal code sample

```python
import scanpy as sc
import anndata as ad
import pandas as pd
import numpy as np

obs = pd.DataFrame(np.arange(100), 
                   columns=['a'], 
                   index=['cell{}'.format(i) for i in range(100)])

X = np.random.rand(100, 5)
adata = ad.AnnData(X=X, obs=obs)
sc.tl.pca(adata)
sc.pl.pca(adata, color='a', return_fig=True, annotate_var_explained=True)
```


### Error output

```pytb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../scanpy/plotting/_tools/scatterplots.py", line 894, in pca
    ax.set_xlabel(label_dict[ax.xaxis.get_label().get_text()])
KeyError: ''
```


### Versions

<details>

```
-----
anndata     0.8.0
scanpy      1.10.0.dev128+g616d5803
-----
PIL                 9.2.0
beta_ufunc          NA
binom_ufunc         NA
colorama            0.4.6
cycler              0.10.0
cython_runtime      NA
dateutil            2.8.2
h5py                3.7.0
hypergeom_ufunc     NA
joblib              1.2.0
kiwisolver          1.4.4
llvmlite            0.39.1
matplotlib          3.6.2
mpl_toolkits        NA
natsort             8.2.0
nbinom_ufunc        NA
ncf_ufunc           NA
numba               0.56.3
numpy               1.23.4
packaging           21.3
pandas              1.5.1
pkg_resources       NA
pynndescent         0.5.8
pyparsing           3.0.9
pytz                2022.2.1
scipy               1.9.3
session_info        1.0.0
setuptools          65.5.1
six                 1.16.0
sklearn             1.1.3
threadpoolctl       3.1.0
typing_extensions   NA
zoneinfo            NA
-----
Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:38:29) [Clang 13.0.1 ]
macOS-<redacted>-arm64-arm-64bit
-----
Session information updated at 2023-10-11 14:45
```

</details>



Discussion:
See #2682 for a putative fix. Might need a unit test though.

Resolving patch:
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
@@ -886,8 +886,10 @@ def pca(
             # edit axis labels in returned figure
             fig = embedding(adata, 'pca', return_fig=return_fig, **kwargs)
             for ax in fig.axes:
-                ax.set_xlabel(label_dict[ax.xaxis.get_label().get_text()])
-                ax.set_ylabel(label_dict[ax.yaxis.get_label().get_text()])
+                if xlabel := label_dict.get(ax.xaxis.get_label().get_text()):
+                    ax.set_xlabel(xlabel)
+                if ylabel := label_dict.get(ax.yaxis.get_label().get_text()):
+                    ax.set_ylabel(ylabel)
             return fig
 
         else:


Test patch: