Repository: mathause/mplotutils

Issue:
`mpu.colorbar` overwrites gca
`mpu.colorbar` creates a new axes and hijacks `plt.gca()` - see if we can avoid this. Probably using `plt.sca()`.


```python
import mplotutils as mpu
import cartopy.crs as ccrs
import xarray as xr
import matplotlib.pyplot as plt

air = xr.tutorial.open_dataset("air_temperature").air
d = air.isel(time=0)

f, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree()})
h = d.plot(ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False)
print(plt.gca())
plt.colorbar(h, ax=ax)
print(plt.gca())
mpu.colorbar(h, ax)
print(plt.gca())

```

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/mathause/mplotutils/pull/136?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.21%. Comparing base [(`52068a6`)](https://app.codecov.io/gh/mathause/mplotutils/commit/52068a62b13eaa1b2a1b3a80627a0d3df4b6a390?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) to head [(`30c2787`)](https://app.codecov.io/gh/mathause/mplotutils/commit/30c2787a9d85b2ab4e0f98171a9c8ac0e92bf385?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #136      +/-   ##
==========================================
+ Coverage   95.19%   95.21%   +0.01%     
==========================================
  Files          10       10              
  Lines         520      522       +2     
==========================================
+ Hits          495      497       +2     
  Misses         25       25              
```

| [Flag](https://app.codecov.io/gh/mathause/mplotutils/pull/136/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/mathause/mplotutils/pull/136/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) | `95.21% <100.00%> (+0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/mathause/mplotutils/pull/136?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8dbf094..cbe9cc7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,10 @@
 - `set_map_layout` now raises an explicit error when the figure contains SubFigure ([#121](https://github.com/mathause/mplotutils/pull/121)).
 - Test upstream dependencies and fix compatibility with the upcoming pandas v3  ([#133](https://github.com/mathause/mplotutils/pull/133)).
 
+### Bug fixes
+
+- Ensure the current axes (`plt.gca()`) is not changed by calling `mpu.colorbar(...)` ([#136](https://github.com/mathause/mplotutils/pull/136)).
+
 ## v0.5.0 (27.03.2024)
 
 Version v0.5.0 aligns passing multiple axes to `colorbar` with matplotlib.
diff --git a/mplotutils/_colorbar.py b/mplotutils/_colorbar.py
index 185358d..4d16424 100644
--- a/mplotutils/_colorbar.py
+++ b/mplotutils/_colorbar.py
@@ -211,7 +211,10 @@ def colorbar(
     if not all(f == ax.get_figure() for ax in axs):
         raise TypeError("All passed axes must belong to the same figure")
 
+    gca = plt.gca()
     cbax = _get_cbax(f)
+    # ensure mpu.colorbar does not change the current axes
+    plt.sca(gca)
 
     cbar = plt.colorbar(mappable, orientation=orientation, cax=cbax, **kwargs)
 


Test patch:
diff --git a/mplotutils/tests/test_colorbar.py b/mplotutils/tests/test_colorbar.py
index d258a2d..4bb8ad7 100644
--- a/mplotutils/tests/test_colorbar.py
+++ b/mplotutils/tests/test_colorbar.py
@@ -235,6 +235,23 @@ def test_colorbar_2d_array():
         assert_position(cbar, expected)
 
 
+def test_gca_is_same():
+
+    with figure_context():
+        h, ax = create_figure_subplots(1, 1)
+        mpu.colorbar(h, ax, size=0.2, pad=0)
+
+        gca = plt.gca()
+        assert gca is ax
+
+    with figure_context():
+        h, axs = create_figure_subplots(1, 2)
+        mpu.colorbar(h, axs, size=0.2, pad=0, orientation="horizontal")
+
+        gca = plt.gca()
+        assert gca is axs[1]
+
+
 def test_colorbar_vertical_aspect():
     with figure_context(figsize=(5, 5)):
         # test pad=0, aspect=5