8000 FIX: use set_box_aspect rather than set_aspect · matplotlib/matplotlib@182a488 · GitHub
[go: up one dir, main page]

Skip to content

Commit 182a488

Browse files
committed
FIX: use set_box_aspect rather than set_aspect
1 parent d09ae79 commit 182a488

File tree

2 files changed

+79
-40
lines changed

2 files changed

+79
-40
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,9 @@ def _reposition_colorbar(cbax, renderer, *, offset=None):
575575
pbcb = trans_fig_to_subfig.transform_bbox(pbcb)
576576
cbax.set_transform(fig.transSubfigure)
577577
cbax._set_position(pbcb)
578-
cbax.set_aspect(aspect, anchor=anchor, adjustable='box')
578+
cbax.set_anchor(anchor)
579+
cbax.set_box_aspect(aspect)
580+
cbax.set_aspect('auto')
579581
return offset
580582

581583

lib/matplotlib/colorbar.py

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import matplotlib.artist as martist
2323
import matplotlib.patches as mpatches
2424
import matplotlib.path as mpath
25+
import matplotlib.scale as mscale
2526
import matplotlib.spines as mspines
2627
import matplotlib.transforms as mtransforms
2728
from matplotlib import docstring
@@ -237,28 +238,14 @@ def __init__(self, cbar):
237238
self._orig_locator = cbar.ax._axes_locator
238239

239240
def __call__(self, ax, renderer):
240-
241-
# make sure that lims and scales are the same
242-
if 0:
243-
scale = self._cbar._long_axis()._scale
244-
try:
245-
self._cbar._short_axis()._set_scale(scale)
246-
except TypeError:
247-
pass
248-
lim = self._cbar._long_axis().get_view_interval()
249-
print('lim', lim, scale, self._cbar.ax.get_xlim(), self._cbar.ax.get_ylim(),
250-
self._cbar.orientation)
251-
self._cbar.ax.set_xlim(lim)
252-
self._cbar.ax.set_ylim(lim)
253-
254-
241+
print(ax == self._cbar.ax)
242+
print('lim',self._cbar.ax.get_xlim(), self._cbar.ax.get_ylim(),
243+
self._cbar.orientation, self._cbar.ax.yaxis._scale)
255244
if self._orig_locator is not None:
256245
pos = self._orig_locator(ax, renderer)
257246
else:
258247
pos = ax.get_position(original=True)
259248
if self._cbar.extend == 'neither':
260-
print('pos0', pos)
261-
262249
return pos
263250

264251
y, extendlen = self._cbar._proportional_y()
@@ -277,17 +264,15 @@ def __call__(self, ax, renderer):
277264
aspect = False
278265
# now shrink and/or offset to take into account the
279266
# extend tri/rectangles.
280-
print('aspect', aspect, shrink)
281 E864 -
print('pos0', pos)
282267
if self._cbar.orientation == 'vertical':
283268
if aspect:
284-
ax.set_box_aspect(aspect*shrink)
269+
self._cbar.set_aspect(aspect*shrink)
285270
pos = pos.shrunk(1, shrink).translated(0, offset * pos.height)
286271
else:
272+
self._cbar.ax.set_ylim(0, 1)
287273
if aspect:
288-
ax.set_box_aspect(aspect/shrink)
274+
ax._cbar.set_aspect(aspect*shrink)
289275
pos = pos.shrunk(shrink, 1).translated(offset * pos.width, 0)
290-
print('pos', pos)
291276
return pos
292277

293278
def get_subplotspec(self):
@@ -577,6 +562,7 @@ def draw_all(self):
577562
if self._extend_upper():
578563
ind = ind[:-1]
579564
self._add_solids(X, Y, self._values[ind, np.newaxis])
565+
print('Done draw all')
580566

581567
def _add_solids(self, X, Y, C):
582568
"""Draw the colors; optionally add separators."""
@@ -929,6 +915,59 @@ def set_alpha(self, alpha):
929915
"""Set the transparency between 0 (transparent) and 1 (opaque)."""
930916
self.alpha = alpha
931917

918+
def set_scale(self, scale, **kwargs):
919+
"""
920+
Set the colorbar long axis scale.
921+
922+
Parameters
923+
----------
924+
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
925+
The axis scale type to apply.
926+
927+
**kwargs
928+
Different keyword arguments are accepted, depending on the scale.
929+
See the respective class keyword arguments:
930+
931+
- `matplotlib.scale.LinearScale`
932+
- `matplotlib.scale.LogScale`
933+
- `matplotlib.scale.SymmetricalLogScale`
934+
- `matplotlib.scale.LogitScale`
935+
- `matplotlib.scale.FuncScale`
936+
937+
Notes
938+
-----
939+
By default, Matplotlib supports the above mentioned scales.
940+
Additionally, custom scales may be registered using
941+
`matplotlib.scale.register_scale`. These scales can then also
942+
be used here.
943+
"""
944+
if self.orientation == 'vertical':
945+
self.ax.set_yscale(scale, **kwargs)
946+
else:
947+
self.ax.set_xscale(scale, **kwargs)
948+
if isinstance(scale, mscale.ScaleBase):
949+
self.__scale = scale.name
950+
else:
951+
self.__scale = scale
952+
953+
def get_scale(self):
954+
"""
955+
Return the colorbar's axis scale as a str.
956+
"""
957+
if self.orientation == 'vertical':
958+
return self.ax.get_yscale()
959+
else:
960+
return self.ax.get_xscale()
961+
962+
def set_aspect(self, aspect):
963+
"""
964+
Set ratio of the long axis to short axis.
965+
"""
966+
if self.orientation == 'horizontal':
967+
aspect = 1 / aspect
968+
self.ax.set_box_aspect(aspect)
969+
self.ax.set_aspect('auto')
970+
932971
def remove(self):
933972
"""
934973
Remove this colorbar from the figure.
@@ -1075,44 +1114,38 @@ def _inverse_boundaries(self, x):
10751114
b = self._boundaries
10761115
return np.interp(x, np.linspace(0, b[-1], len(b)), b)
10771116

1117+
10781118
def _reset_locator_formatter_scale(self):
10791119
"""
10801120
Reset the locator et al to defaults. Any user-hardcoded changes
10811121
need to be re-entered if this gets called (either at init, or when
10821122
the mappable normal gets changed: Colorbar.update_normal)
10831123
"""
1124+
print('Yee haw', self.norm._scale)
1125+
10841126
self._process_values()
10851127
self.locator = None
10861128
self.minorlocator = None
10871129
self.formatter = None
1130+
longax = self._long_axis()
10881131
if (self.boundaries is not None or
10891132
isinstance(self.norm, colors.BoundaryNorm)):
10901133
if self.spacing == 'uniform':
10911134
funcs = (self._forward_boundaries, self._inverse_boundaries)
1092-
self.ax.set_xscale('function', functions=funcs)
1093-
self.ax.set_yscale('function', functions=funcs)
1094-
self.__scale = 'function'
1135+
self.set_scale('function', functions=funcs)
10951136
elif self.spacing == 'proportional':
1096-
self.__scale = 'linear'
1097-
self.ax.set_xscale('linear')
1098-
self.ax.set_yscale('linear')
1137+
self.set_scale('linear')
10991138
elif hasattr(self.norm, '_scale') and self.norm._scale is not None:
11001139
# use the norm's scale:
1101-
self.ax.set_xscale(self.norm._scale)
1102-
self.ax.set_yscale(self.norm._scale)
1103-
self.__scale = self.norm._scale.name
1140+
self.set_scale(self.norm._scale)
11041141
elif type(self.norm) is colors.Normalize:
11051142
# plain Normalize:
1106-
self.ax.set_xscale('linear')
1107-
self.ax.set_yscale('linear')
1108-
self.__scale = 'linear'
1143+
self.set_scale('linear')
11091144
else:
11101145
# norm._scale is None or not an attr: derive the scale from
11111146
# the Norm:
11121147
funcs = (self.norm, self.norm.inverse)
1113-
self.ax.set_xscale('function', functions=funcs)
1114-
self.ax.set_yscale('function', functions=funcs)
1115-
self.__scale = 'function'
1148+
self.set_scale('function', functions=funcs)
11161149

11171150
def _locate(self, x):
11181151
"""
@@ -1350,7 +1383,9 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
13501383
aspect=aspect,
13511384
pad=pad)
13521385
# and we need to set the aspect ratio by hand...
1353-
cax.set_box_aspect(aspect, anchor=anchor)
1386+
cax.set_anchor(anchor)
1387+
cax.set_box_aspect(aspect)
1388+
cax.set_aspect('auto')
13541389

13551390
return cax, kw
13561391

@@ -1451,7 +1486,9 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
14511486

14521487
fig = parent.get_figure()
14531488
cax = fig.add_subplot(ss_cb, label="<colorbar>")
1454-
cax.set_box_aspect(aspect,)
1489+
cax.set_anchor(anchor)
1490+
cax.set_box_aspect(aspect)
1491+
cax.set_aspect('auto')
14551492
cax._colorbar_info = dict(
14561493
location=location,
14571494
parents=[parent],

0 commit comments

Comments
 (0)
0