Skip to content

Commit 2919363

Browse files
committed
Deprecate parameters to colorbar which have no effect.
Per changelog entry. I went for a custom helper in colorbar.py rather than a more general one in cbook as this allowed for a nicer message, and making the general helper handle the deprecation would be a bit unwieldy.
1 parent 9c2ad59 commit 2919363

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ Setting :rc:`text.latex.preamble` or :rc:`pdf.preamble` to non-strings
8888
These rcParams should be set to string values. Support for None (meaning the
8989
empty string) and lists of strings (implicitly joined with newlines) is
9090
deprecated.
91+
92+
Effectless parameters of `.Figure.colorbar` and `matplotlib.colorbar.Colorbar`
93+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
The *cmap* and *norm* parameters of `.Figure.colorbar` and
95+
`matplotlib.colorbar.Colorbar` have no effect because they are always
96+
overridden by the mappable's colormap and norm; they are thus deprecated.
97+
Likewise, passing the *alpha*, *boundaries*, *values*, *extend*, or *filled*
98+
parameters with a `.ContourSet` mappable, or the *alpha* parameter with an
99+
`.Artist` mappable, is deprecated, as the mappable would likewise override
100+
them.

examples/images_contours_and_fields/contour_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
im = ax.imshow(Z, interpolation='bilinear', origin='lower',
8383
cmap=cm.gray, extent=(-3, 3, -2, 2))
8484
levels = np.arange(-1.2, 1.6, 0.2)
85-
CS = ax.contour(Z, levels, origin='lower', cmap='flag',
85+
CS = ax.contour(Z, levels, origin='lower', cmap='flag', extend='both',
8686
linewidths=2, extent=(-3, 3, -2, 2))
8787

8888
# Thicken the zero contour.
@@ -93,7 +93,7 @@
9393
inline=1, fmt='%1.1f', fontsize=14)
9494

9595
# make a colorbar for the contour lines
96-
CB = fig.colorbar(CS, shrink=0.8, extend='both')
96+
CB = fig.colorbar(CS, shrink=0.8)
9797

9898
ax.set_title('Lines with colorbar')
9999

lib/matplotlib/colorbar.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,19 @@ def remove(self):
11551155
self.ax.remove()
11561156

11571157

1158+
def _add_disjoint_kwargs(d, **kwargs):
1159+
"""
1160+
Update dict *d* with entries in *kwargs*, which must be absent from *d*.
1161+
"""
1162+
for k, v in kwargs.items():
1163+
if k in d:
1164+
cbook.warn_deprecated(
1165+
"3.3", message=f"The {k!r} parameter to Colorbar has no "
1166+
"effect because it is overridden by the mappable; it is "
1167+
"deprecated since %(since)s and will be removed %(removal)s.")
1168+
d[k] = v
1169+
1170+
11581171
class Colorbar(ColorbarBase):
11591172
"""
11601173
This class connects a `ColorbarBase` to a `~.cm.ScalarMappable`
@@ -1165,35 +1178,36 @@ class Colorbar(ColorbarBase):
11651178
`.Figure.colorbar` or `.pyplot.colorbar` to create a colorbar.
11661179
"""
11671180

1168-
def __init__(self, ax, mappable, **kw):
1181+
def __init__(self, ax, mappable, **kwargs):
11691182
# Ensure the given mappable's norm has appropriate vmin and vmax set
11701183
# even if mappable.draw has not yet been called.
11711184
if mappable.get_array() is not None:
11721185
mappable.autoscale_None()
11731186

11741187
self.mappable = mappable
1175-
kw['cmap'] = cmap = mappable.cmap
1176-
kw['norm'] = mappable.norm
1188+
_add_disjoint_kwargs(kwargs, cmap=mappable.cmap, norm=mappable.norm)
11771189

11781190
if isinstance(mappable, contour.ContourSet):
1179-
CS = mappable
1180-
kw['alpha'] = mappable.get_alpha()
1181-
kw['boundaries'] = CS._levels
1182-
kw['values'] = CS.cvalues
1183-
kw['extend'] = CS.extend
1184-
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
1185-
kw['filled'] = CS.filled
1186-
ColorbarBase.__init__(self, ax, **kw)
1187-
if not CS.filled:
1188-
self.add_lines(CS)
1191+
cs = mappable
1192+
_add_disjoint_kwargs(
1193+
kwargs,
1194+
alpha=cs.get_alpha(),
1195+
boundaries=cs._levels,
1196+
values=cs.cvalues,
1197+
extend=cs.extend,
1198+
filled=cs.filled,
1199+
)
1200+
kwargs.setdefault(
1201+
'ticks', ticker.FixedLocator(cs.levels, nbins=10))
1202+
ColorbarBase.__init__(self, ax, **kwargs)
1203+
if not cs.filled:
1204+
self.add_lines(cs)
11891205
else:
1190-
if getattr(cmap, 'colorbar_extend', False) is not False:
1191-
kw.setdefault('extend', cmap.colorbar_extend)
1192-
1206+
if getattr(mappable.cmap, 'colorbar_extend', False) is not False:
1207+
kwargs.setdefault('extend', mappable.cmap.colorbar_extend)
11931208
if isinstance(mappable, martist.Artist):
1194-
kw['alpha'] = mappable.get_alpha()
1195-
1196-
ColorbarBase.__init__(self, ax, **kw)
1209+
_add_disjoint_kwargs(kwargs, alpha=mappable.get_alpha())
1210+
ColorbarBase.__init__(self, ax, **kwargs)
11971211

11981212
def on_mappable_changed(self, mappable):
11991213
"""

lib/matplotlib/tests/test_colorbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ def test_colorbar_closed_patch():
243243
# because it is matched to the data range in the image and to
244244
# the number of colors in the LUT.
245245
values = np.linspace(0, 10, 5)
246-
cbar_kw = dict(cmap=cmap, orientation='horizontal', values=values,
247-
ticks=[])
246+
cbar_kw = dict(orientation='horizontal', values=values, ticks=[])
248247

249248
# The wide line is to show that the closed path is being handled
250249
# correctly. See PR #4186.
@@ -264,9 +263,8 @@ def test_colorbar_ticks():
264263
Z = X * Y
265264
clevs = np.array([-12, -5, 0, 5, 12], dtype=float)
266265
colors = ['r', 'g', 'b', 'c']
267-
cs = ax.contourf(X, Y, Z, clevs, colors=colors)
268-
cbar = fig.colorbar(cs, ax=ax, extend='neither',
269-
orientation='horizontal', ticks=clevs)
266+
cs = ax.contourf(X, Y, Z, clevs, colors=colors, extend='neither')
267+
cbar = fig.colorbar(cs, ax=ax, orientation='horizontal', ticks=clevs)
270268
assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs)
271269

272270

0 commit comments

Comments
 (0)