Skip to content

Commit 3163fba

Browse files
authored
colorbar: ensure same gca (#136)
* colorbar: ensure same gca * changelog and comment
1 parent 52068a6 commit 3163fba

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
- `set_map_layout` now raises an explicit error when the figure contains SubFigure ([#121](https://github.com/mathause/mplotutils/pull/121)).
3131
- Test upstream dependencies and fix compatibility with the upcoming pandas v3 ([#133](https://github.com/mathause/mplotutils/pull/133)).
3232

33+
### Bug fixes
34+
35+
- Ensure the current axes (`plt.gca()`) is not changed by calling `mpu.colorbar(...)` ([#136](https://github.com/mathause/mplotutils/pull/136)).
36+
3337
## v0.5.0 (27.03.2024)
3438

3539
Version v0.5.0 aligns passing multiple axes to `colorbar` with matplotlib.

mplotutils/_colorbar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ def colorbar(
211211
if not all(f == ax.get_figure() for ax in axs):
212212
raise TypeError("All passed axes must belong to the same figure")
213213

214+
gca = plt.gca()
214215
cbax = _get_cbax(f)
216+
# ensure mpu.colorbar does not change the current axes
217+
plt.sca(gca)
215218

216219
cbar = plt.colorbar(mappable, orientation=orientation, cax=cbax, **kwargs)
217220

mplotutils/tests/test_colorbar.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ def test_colorbar_2d_array():
235235
assert_position(cbar, expected)
236236

237237

238+
def test_gca_is_same():
239+
240+
with figure_context():
241+
h, ax = create_figure_subplots(1, 1)
242+
mpu.colorbar(h, ax, size=0.2, pad=0)
243+
244+
gca = plt.gca()
245+
assert gca is ax
246+
247+
with figure_context():
248+
h, axs = create_figure_subplots(1, 2)
249+
mpu.colorbar(h, axs, size=0.2, pad=0, orientation="horizontal")
250+
251+
gca = plt.gca()
252+
assert gca is axs[1]
253+
254+
238255
def test_colorbar_vertical_aspect():
239256
with figure_context(figsize=(5, 5)):
240257
# test pad=0, aspect=5

0 commit comments

Comments
 (0)