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

Commiteed2fe8

Browse files
committed
BUG: fix normalizing image data contained in np.ndarray subclass
1 parent09eab5b commiteed2fe8

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

‎lib/matplotlib/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
461461
vmid=np.float64(self.norm.vmin)+dv/2
462462
fact=1e7ifscaled_dtype==np.float64else1e4
463463
newmin=vmid-dv*fact
464-
ifnewmin<a_min:
464+
ifnewmin<np.float64(a_min):
465465
newmin=None
466466
else:
467467
a_min=np.float64(newmin)
468468
newmax=vmid+dv*fact
469-
ifnewmax>a_max:
469+
ifnewmax>np.float64(a_max):
470470
newmax=None
471471
else:
472472
a_max=np.float64(newmax)

‎lib/matplotlib/tests/test_colorbar.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,3 +1238,48 @@ def test_colorbar_format_string_and_old():
12381238
plt.imshow([[0,1]])
12391239
cb=plt.colorbar(format="{x}%")
12401240
assertisinstance(cb._formatter,StrMethodFormatter)
1241+
1242+
1243+
deftest_colorbar_log_units():
1244+
classFakeQuantity(np.ndarray):
1245+
# this is a self-contained version of astropy.units.Quantity
1246+
# reduced to a bare minimum to reproduce
1247+
# https://github.com/astropy/astropy/issues/11306
1248+
1249+
def__new__(cls,value):
1250+
returnnp.array(value).view(cls)
1251+
1252+
def__array_ufunc__(self,function,method,*inputs,**kwargs):
1253+
defto_value(q):
1254+
value=q.view(np.ndarray)
1255+
ifvalue.shape:
1256+
returnvalue
1257+
else:
1258+
returnvalue[()]
1259+
1260+
arrays= [to_value(q)forqininputs]
1261+
result=super().__array_ufunc__(function,method,*arrays,**kwargs)
1262+
iffunctionnotin (np.minimum,np.maximum):
1263+
returnresult
1264+
else:
1265+
returnself._new_view(result)
1266+
1267+
def_new_view(self,obj):
1268+
obj=np.array(obj,copy=False,subok=True)
1269+
view=obj.view(FakeQuantity)
1270+
returnview
1271+
1272+
def__ne__(self,other):
1273+
returnNotImplemented
1274+
1275+
def__float__(self):
1276+
raiseRuntimeError("boom")
1277+
1278+
defitem(self,*args):
1279+
returnself._new_view(super().item(*args))
1280+
1281+
data=FakeQuantity([[1,2], [3,4]])
1282+
fig,ax=plt.subplots()
1283+
im=ax.imshow(data,norm=LogNorm())
1284+
fig.colorbar(im)
1285+
fig.canvas.draw()

‎lib/matplotlib/transforms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,15 +2853,15 @@ def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
28532853
if (notnp.isfinite(vmin))or (notnp.isfinite(vmax)):
28542854
return-expander,expander
28552855

2856+
# Expand vmin, vmax to float: if they were integer types, they can wrap
2857+
# around in abs (abs(np.int8(-128)) == -128) and vmax - vmin can overflow.
2858+
vmin,vmax=map(np.float64, (vmin,vmax))
2859+
28562860
swapped=False
28572861
ifvmax<vmin:
28582862
vmin,vmax=vmax,vmin
28592863
swapped=True
28602864

2861-
# Expand vmin, vmax to float: if they were integer types, they can wrap
2862-
# around in abs (abs(np.int8(-128)) == -128) and vmax - vmin can overflow.
2863-
vmin,vmax=map(float, [vmin,vmax])
2864-
28652865
maxabsvalue=max(abs(vmin),abs(vmax))
28662866
ifmaxabsvalue< (1e6/tiny)*np.finfo(float).tiny:
28672867
vmin=-expander

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp