1818is a thin wrapper over :meth:`~matplotlib.figure.Figure.colorbar`.
1919
2020'''
21+ import warnings
2122
2223import numpy as np
2324import matplotlib as mpl
@@ -207,6 +208,7 @@ def __init__(self, ax, cmap=None,
207208 filled = True ,
208209 ):
209210 self .ax = ax
211+ self ._patch_ax ()
210212 if cmap is None : cmap = cm .get_cmap ()
211213 if norm is None : norm = colors .Normalize ()
212214 self .alpha = alpha
@@ -239,6 +241,13 @@ def __init__(self, ax, cmap=None,
239241 # The rest is in a method so we can recalculate when clim changes.
240242 self .draw_all ()
241243
244+ def _patch_ax (self ):
245+ def _warn (* args , ** kw ):
246+ warnings .warn ("Use the colorbar set_ticks() method instead." )
247+
248+ self .ax .set_xticks = _warn
249+ self .ax .set_yticks = _warn
250+
242251 def draw_all (self ):
243252 '''
244253 Calculate any free parameters based on the current cmap and norm,
@@ -253,6 +262,50 @@ def draw_all(self):
253262 self ._add_solids (X , Y , C )
254263 self ._set_label ()
255264
265+ def update_ticks (self ):
266+ """
267+ Force the update of the ticks and ticklabels. This must be
268+ called whenever the tick locator and/or tick formatter changes.
269+ """
270+ ax = self .ax
271+ ticks , ticklabels , offset_string = self ._ticker ()
272+ if self .orientation == 'vertical' :
273+ ax .xaxis .set_ticks ([])
274+ ax .yaxis .set_label_position ('right' )
275+ ax .yaxis .set_ticks_position ('right' )
276+ ax .yaxis .set_ticks (ticks )
277+ ax .set_yticklabels (ticklabels )
278+ ax .yaxis .get_major_formatter ().set_offset_string (offset_string )
279+
280+ else :
281+ ax .yaxis .set_ticks ([])
282+ ax .xaxis .set_label_position ('bottom' )
283+ ax .xaxis .set_ticks (ticks )
284+ ax .set_xticklabels (ticklabels )
285+ ax .xaxis .get_major_formatter ().set_offset_string (offset_string )
286+
287+ def set_ticks (self , ticks , update_ticks = True ):
288+ """
289+ set tick locations. Tick locations are updated immediately unless update_ticks is
290+ *False*. To manually update the ticks, call *update_ticks* method explicitly.
291+ """
292+ self .locator = ticker .FixedLocator (ticks , nbins = len (ticks ))
293+ if update_ticks :
294+ self .update_ticks ()
295+
296+ def set_ticklabels (self , ticklabels , update_ticks = True ):
297+ """
298+ set tick labels. Tick labels are updated immediately unless update_ticks is
299+ *False*. To manually update the ticks, call *update_ticks* method explicitly.
300+ """
301+ if isinstance (self .locator , ticker .FixedLocator ):
302+ self .formatter = ticker .FixedFormatter (ticklabels )
303+ if update_ticks :
304+ self .update_ticks ()
305+ else :
306+ warnings .warn ("set_ticks() must have been called." )
307+
308+
256309 def _config_axes (self , X , Y ):
257310 '''
258311 Make an axes patch and outline.
@@ -275,21 +328,9 @@ def _config_axes(self, X, Y):
275328 linewidth = 0.01 ,
276329 zorder = - 1 )
277330 ax .add_artist (self .patch )
278- ticks , ticklabels , offset_string = self ._ticker ()
279- if self .orientation == 'vertical' :
280- ax .set_xticks ([])
281- ax .yaxis .set_label_position ('right' )
282- ax .yaxis .set_ticks_position ('right' )
283- ax .set_yticks (ticks )
284- ax .set_yticklabels (ticklabels )
285- ax .yaxis .get_major_formatter ().set_offset_string (offset_string )
286331
287- else :
288- ax .set_yticks ([])
289- ax .xaxis .set_label_position ('bottom' )
290- ax .set_xticks (ticks )
291- ax .set_xticklabels (ticklabels )
292- ax .xaxis .get_major_formatter ().set_offset_string (offset_string )
332+ self .update_ticks ()
333+
293334
294335 def _set_label (self ):
295336 if self .orientation == 'vertical' :
0 commit comments