Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb573664

Browse files
committed
FIX: use set_box_aspect rather than set_aspect
1 parentc635fda commitb573664

File tree

3 files changed

+83
-40
lines changed

3 files changed

+83
-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
returnoffset
580582

581583

‎lib/matplotlib/colorbar.py

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
importmatplotlib.artistasmartist
2323
importmatplotlib.patchesasmpatches
2424
importmatplotlib.pathasmpath
25+
importmatplotlib.scaleasmscale
2526
importmatplotlib.spinesasmspines
2627
importmatplotlib.transformsasmtransforms
2728
frommatplotlibimportdocstring
@@ -230,23 +231,13 @@ def draw(self, renderer):
230231

231232
class_ColorbarAxesLocator:
232233
"""
233-
Shrink the axes if there are vertical extends.
234+
Shrink the axes if there are vertical extends.
234235
"""
235236
def__init__(self,cbar):
236237
self._cbar=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-
scale=self._cbar._long_axis().get_scale()
243-
try:
244-
self._cbar._short_axis()._set_scale(scale)
245-
exceptTypeError:
246-
pass
247-
lim=self._cbar._long_axis().get_view_interval()
248-
self._cbar._short_axis().set_view_interval(*lim)
249-
250241
ifself._orig_locatorisnotNone:
251242
pos=self._orig_locator(ax,renderer)
252243
else:
@@ -272,11 +263,12 @@ def __call__(self, ax, renderer):
272263
# extend tri/rectangles.
273264
ifself._cbar.orientation=='vertical':
274265
ifaspect:
275-
ax.set_aspect(aspect*shrink)
266+
self._cbar.set_aspect(aspect*shrink)
276267
pos=pos.shrunk(1,shrink).translated(0,offset*pos.height)
277268
else:
269+
self._cbar.ax.set_ylim(0,1)
278270
ifaspect:
279-
ax.set_aspect(aspect/shrink)
271+
ax._cbar.set_aspect(aspect*shrink)
280272
pos=pos.shrunk(shrink,1).translated(offset*pos.width,0)
281273
returnpos
282274

@@ -549,8 +541,12 @@ def draw_all(self):
549541
# also adds the outline path to self.outline spine:
550542
self._do_extends(extendlen)
551543

552-
self.ax.set_xlim(self.vmin,self.vmax)
553-
self.ax.set_ylim(self.vmin,self.vmax)
544+
ifself.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)
554550

555551
# set up the tick locators and formatters. A bit complicated because
556552
# boundary norms + uniform spacing requires a manual locator.
@@ -915,6 +911,59 @@ def set_alpha(self, alpha):
915911
"""Set the transparency between 0 (transparent) and 1 (opaque)."""
916912
self.alpha=alpha
917913

914+
defset_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+
ifself.orientation=='vertical':
941+
self.ax.set_yscale(scale,**kwargs)
942+
else:
943+
self.ax.set_xscale(scale,**kwargs)
944+
ifisinstance(scale,mscale.ScaleBase):
945+
self.__scale=scale.name
946+
else:
947+
self.__scale=scale
948+
949+
defget_scale(self):
950+
"""
951+
Return the colorbar's axis scale as a str.
952+
"""
953+
ifself.orientation=='vertical':
954+
returnself.ax.get_yscale()
955+
else:
956+
returnself.ax.get_xscale()
957+
958+
defset_aspect(self,aspect):
959+
"""
960+
Set ratio of the long axis to short axis.
961+
"""
962+
ifself.orientation=='horizontal':
963+
aspect=1/aspect
964+
self.ax.set_box_aspect(aspect)
965+
self.ax.set_aspect('auto')
966+
918967
defremove(self):
919968
"""
920969
Remove this colorbar from the figure.
@@ -1037,11 +1086,10 @@ def _mesh(self):
10371086
(self.__scale=='manual')):
10381087
# if a norm doesn't have a named scale, or we are not using a norm:
10391088
dv=self.vmax-self.vmin
1040-
x=x*dv+self.vmin
1089+
#x = x * dv + self.vmin
10411090
y=y*dv+self.vmin
10421091
else:
10431092
y=norm.inverse(y)
1044-
x=norm.inverse(x)
10451093
self._y=y
10461094
X,Y=np.meshgrid(x,y)
10471095
ifself.orientation=='vertical':
@@ -1071,34 +1119,25 @@ def _reset_locator_formatter_scale(self):
10711119
self.locator=None
10721120
self.minorlocator=None
10731121
self.formatter=None
1122+
longax=self._long_axis()
10741123
if (self.boundariesisnotNoneor
10751124
isinstance(self.norm,colors.BoundaryNorm)):
10761125
ifself.spacing=='uniform':
10771126
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)
10811128
elifself.spacing=='proportional':
1082-
self.__scale='linear'
1083-
self.ax.set_xscale('linear')
1084-
self.ax.set_yscale('linear')
1129+
self.set_scale('linear')
10851130
elifhasattr(self.norm,'_scale')andself.norm._scaleisnotNone:
10861131
# 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)
10901133
eliftype(self.norm)iscolors.Normalize:
10911134
# plain Normalize:
1092-
self.ax.set_xscale('linear')
1093-
self.ax.set_yscale('linear')
1094-
self.__scale='linear'
1135+
self.set_scale('linear')
10951136
else:
10961137
# norm._scale is None or not an attr: derive the scale from
10971138
# the Norm:
10981139
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)
11021141

11031142
def_locate(self,x):
11041143
"""
@@ -1336,7 +1375,9 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
13361375
aspect=aspect,
13371376
pad=pad)
13381377
# 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')
13401381

13411382
returncax,kw
13421383

@@ -1437,7 +1478,9 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
14371478

14381479
fig=parent.get_figure()
14391480
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')
14411484
cax._colorbar_info=dict(
14421485
location=location,
14431486
parents=[parent],

‎lib/matplotlib/tests/test_colorbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@ def test_colorbar_lognorm_extension(extend):
460460
# Test that colorbar with lognorm is extended correctly
461461
f,ax=plt.subplots()
462462
cb=Colorbar(ax,norm=LogNorm(vmin=0.1,vmax=1000.0),
463-
orientation='vertical',extend=extend)
463+
orientation='vertical',extend=extend)
464464
assertcb._values[0]>=0.0
465465

466466

467467
deftest_colorbar_powernorm_extension():
468468
# Test that colorbar with powernorm is extended correctly
469469
f,ax=plt.subplots()
470470
cb=Colorbar(ax,norm=PowerNorm(gamma=0.5,vmin=0.0,vmax=1.0),
471-
orientation='vertical',extend='both')
471+
orientation='vertical',extend='both')
472472
assertcb._values[0]>=0.0
473473

474474

@@ -725,12 +725,10 @@ def test_colorbar_change_lim_scale():
725725
pc=ax[0].pcolormesh(np.arange(100).reshape(10,10)+1)
726726
cb=fig.colorbar(pc,ax=ax[0],extend='both')
727727
cb.ax.set_yscale('log')
728-
cb.ax.set_xscale('log')
729728

730729
pc=ax[1].pcolormesh(np.arange(100).reshape(10,10)+1)
731730
cb=fig.colorbar(pc,ax=ax[1],extend='both')
732731
cb.ax.set_ylim([20,90])
733-
cb.ax.set_xlim([20,90])
734732

735733

736734
@check_figures_equal(extensions=["png"])
@@ -745,7 +743,7 @@ def test_axes_handles_same_functions(fig_ref, fig_test):
745743
caxx=cax
746744
else:
747745
caxx=cb.ax
748-
caxx.set_yticks(np.arange(20))
746+
caxx.set_yticks(np.arange(0,20))
749747
caxx.set_yscale('log')
750748
caxx.set_position([0.92,0.1,0.02,0.7])
751749

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp