22
22
import matplotlib .artist as martist
23
23
import matplotlib .patches as mpatches
24
24
import matplotlib .path as mpath
25
+ import matplotlib .scale as mscale
25
26
import matplotlib .spines as mspines
26
27
import matplotlib .transforms as mtransforms
27
28
from matplotlib import docstring
@@ -230,23 +231,13 @@ def draw(self, renderer):
230
231
231
232
class _ColorbarAxesLocator :
232
233
"""
233
- Shrink the axes if there are vertical extends.
234
+ Shrink the axes if there are vertical extends.
234
235
"""
235
236
def __init__ (self , cbar ):
236
237
self ._cbar = cbar
237
238
self ._orig_locator = cbar .ax ._axes_locator
238
239
239
240
def __call__ (self , ax , renderer ):
240
-
241
- # make sure that lims and scales are the same
242
- scale = self ._cbar ._long_axis ().get_scale ()
243
- try :
244
- self ._cbar ._short_axis ()._set_scale (scale )
245
- except TypeError :
246
- pass
247
- lim = self ._cbar ._long_axis ().get_view_interval ()
248
- self ._cbar ._short_axis ().set_view_interval (* lim )
249
-
250
241
if self ._orig_locator is not None :
251
242
pos = self ._orig_locator (ax , renderer )
252
243
else :
@@ -272,11 +263,12 @@ def __call__(self, ax, renderer):
272
263
# extend tri/rectangles.
273
264
if self ._cbar .orientation == 'vertical' :
274
265
if aspect :
275
- ax .set_aspect (aspect * shrink )
266
+ self . _cbar .set_aspect (aspect * shrink )
276
267
pos = pos .shrunk (1 , shrink ).translated (0 , offset * pos .height )
277
268
else :
269
+ self ._cbar .ax .set_ylim (0 , 1 )
278
270
if aspect :
279
- ax .set_aspect (aspect / shrink )
271
+ ax ._cbar . set_aspect (aspect * shrink )
280
272
pos = pos .shrunk (shrink , 1 ).translated (offset * pos .width , 0 )
281
273
return pos
282
274
@@ -549,8 +541,12 @@ def draw_all(self):
549
541
# also adds the outline path to self.outline spine:
550
542
self ._do_extends (extendlen )
551
543
552
- self .ax .set_xlim (self .vmin , self .vmax )
553
- self .ax .set_ylim (self .vmin , self .vmax )
544
+ if self .orientation == 'vertical' :
545
+ self .ax .set_xlim (0 , 1 )
546
+ self .ax .set_ylim (self .vmin , self .vmax )
547
+ else :
548
+ self .ax .set_ylim (0 , 1 )
549
+ self .ax .set_xlim (self .vmin , self .vmax )
554
550
555
551
# set up the tick locators and formatters. A bit complicated because
556
552
# boundary norms + uniform spacing requires a manual locator.
@@ -915,6 +911,59 @@ def set_alpha(self, alpha):
915
911
"""Set the transparency between 0 (transparent) and 1 (opaque)."""
916
912
self .alpha = alpha
917
913
914
+ def set_scale (self , scale , ** kwargs ):
915
+ """
916
+ Set the colorbar long axis scale.
917
+
918
+ Parameters
919
+ ----------
920
+ value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
921
+ The axis scale type to apply.
922
+
923
+ **kwargs
924
+ Different keyword arguments are accepted, depending on the scale.
925
+ See the respective class keyword arguments:
926
+
927
+ - `matplotlib.scale.LinearScale`
928
+ - `matplotlib.scale.LogScale`
929
+ - `matplotlib.scale.SymmetricalLogScale`
930
+ - `matplotlib.scale.LogitScale`
931
+ - `matplotlib.scale.FuncScale`
932
+
933
+ Notes
934
+ -----
935
+ By default, Matplotlib supports the above mentioned scales.
936
+ Additionally, custom scales may be registered using
937
+ `matplotlib.scale.register_scale`. These scales can then also
938
+ be used here.
939
+ """
940
+ if self .orientation == 'vertical' :
941
+ self .ax .set_yscale (scale , ** kwargs )
942
+ else :
943
+ self .ax .set_xscale (scale , ** kwargs )
944
+ if isinstance (scale , mscale .ScaleBase ):
945
+ self .__scale = scale .name
946
+ else :
947
+ self .__scale = scale
948
+
949
+ def get_scale (self ):
950
+ """
951
+ Return the colorbar's axis scale as a str.
952
+ """
953
+ if self .orientation == 'vertical' :
954
+ return self .ax .get_yscale ()
955
+ else :
956
+ return self .ax .get_xscale ()
957
+
958
+ def set_aspect (self , aspect ):
959
+ """
960
+ Set ratio of the long axis to short axis.
961
+ """
962
+ if self .orientation == 'horizontal' :
963
+ aspect = 1 / aspect
964
+ self .ax .set_box_aspect (aspect )
965
+ self .ax .set_aspect ('auto' )
966
+
918
967
def remove (self ):
919
968
"""
920
969
Remove this colorbar from the figure.
@@ -1037,11 +1086,10 @@ def _mesh(self):
1037
1086
(self .__scale == 'manual' )):
1038
1087
# if a norm doesn't have a named scale, or we are not using a norm:
1039
1088
dv = self .vmax - self .vmin
1040
- x = x * dv + self .vmin
1089
+ # x = x * dv + self.vmin
1041
1090
y = y * dv + self .vmin
1042
1091
else :
1043
1092
y = norm .inverse (y )
1044
- x = norm .inverse (x )
1045
1093
self ._y = y
1046
1094
X , Y = np .meshgrid (x , y )
1047
1095
if self .orientation == 'vertical' :
@@ -1071,34 +1119,25 @@ def _reset_locator_formatter_scale(self):
1071
1119
self .locator = None
1072
1120
self .minorlocator = None
1073
1121
self .formatter = None
1122
+ longax = self ._long_axis ()
1074
1123
if (self .boundaries is not None or
1075
1124
isinstance (self .norm , colors .BoundaryNorm )):
1076
1125
if self .spacing == 'uniform' :
1077
1126
funcs = (self ._forward_boundaries , self ._inverse_boundaries )
1078
- self .ax .set_xscale ('function' , functions = funcs )
1079
- self .ax .set_yscale ('function' , functions = funcs )
1080
- self .__scale = 'function'
1127
+ self .set_scale ('function' , functions = funcs )
1081
1128
elif self .spacing == 'proportional' :
1082
- self .__scale = 'linear'
1083
- self .ax .set_xscale ('linear' )
1084
- self .ax .set_yscale ('linear' )
1129
+ self .set_scale ('linear' )
1085
1130
elif hasattr (self .norm , '_scale' ) and self .norm ._scale is not None :
1086
1131
# use the norm's scale:
1087
- self .ax .set_xscale (self .norm ._scale )
1088
- self .ax .set_yscale (self .norm ._scale )
1089
- self .__scale = self .norm ._scale .name
1132
+ self .set_scale (self .norm ._scale )
1090
1133
elif type (self .norm ) is colors .Normalize :
1091
1134
# plain Normalize:
1092
- self .ax .set_xscale ('linear' )
1093
- self .ax .set_yscale ('linear' )
1094
- self .__scale = 'linear'
1135
+ self .set_scale ('linear' )
1095
1136
else :
1096
1137
# norm._scale is None or not an attr: derive the scale from
1097
1138
# the Norm:
1098
1139
funcs = (self .norm , self .norm .inverse )
1099
- self .ax .set_xscale ('function' , functions = funcs )
1100
- self .ax .set_yscale ('function' , functions = funcs )
1101
- self .__scale = 'function'
1140
+ self .set_scale ('function' , functions = funcs )
1102
1141
1103
1142
def _locate (self , x ):
1104
1143
"""
@@ -1336,7 +1375,9 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1336
1375
aspect = aspect ,
1337
1376
pad = pad )
1338
1377
# and we need to set the aspect ratio by hand...
1339
- cax .set_aspect (aspect , anchor = anchor , adjustable = 'box' )
1378
+ cax .set_anchor (anchor )
1379
+ cax .set_box_aspect (aspect )
1380
+ cax .set_aspect ('auto' )
1340
1381
1341
1382
return cax , kw
1342
1383
@@ -1437,7 +1478,9 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
1437
1478
1438
1479
fig = parent .get_figure ()
1439
1480
cax = fig .add_subplot (ss_cb , label = "<colorbar>" )
1440
- cax .set_aspect (aspect , anchor = loc_settings ["anchor" ], adjustable = 'box' )
1481
+ cax .set_anchor (anchor )
1482
+ cax .set_box_aspect (aspect )
1483
+ cax .set_aspect ('auto' )
1441
1484
cax ._colorbar_info = dict (
1442
1485
location = location ,
1443
1486
parents = [parent ],
0 commit comments