diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 4aebc1696543..1636e201019b 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1253,11 +1253,13 @@ def colorbar( # Store the value of gca so that we can set it back later on. if cax is None: if ax is None: - raise ValueError( + _api.warn_deprecated("3.6", message=( 'Unable to determine Axes to steal space for Colorbar. ' + 'Using gca(), but will raise in the future. ' 'Either provide the *cax* argument to use as the Axes for ' 'the Colorbar, provide the *ax* argument to steal space ' - 'from it, or add *mappable* to an Axes.') + 'from it, or add *mappable* to an Axes.')) + ax = self.gca() current_ax = self.gca() userax = False if (use_gridspec and isinstance(ax, SubplotBase)): diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 149ed4c3d22e..31097c12f061 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -1,10 +1,12 @@ import numpy as np import pytest +from matplotlib import _api from matplotlib import cm import matplotlib.colors as mcolors import matplotlib as mpl + from matplotlib import rc_context from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt @@ -319,7 +321,8 @@ def test_parentless_mappable(): pc = mpl.collections.PatchCollection([], cmap=plt.get_cmap('viridis')) pc.set_array([]) - with pytest.raises(ValueError, match='Unable to determine Axes to steal'): + with pytest.warns(_api.MatplotlibDeprecationWarning, + match='Unable to determine Axes to steal'): plt.colorbar(pc)