Skip to content

Commit c38205a

Browse files
dstansbyQuLogic
authored andcommitted
Move float() casting in Rectangle patch (#8938)
* Remove float() from Rectangle Make sure rotation x/y are cast to floats * Add uint8 test * Fix __nonzero__ for python 3 Fix non-zero for py2 * Added test for units with Rectangle Update test_units_patches.py Update test_units_patches.py * Move unit rectangle test to test_patches * Remove image test for uint8 bar
1 parent c45678e commit c38205a

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

lib/matplotlib/patches.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,10 @@ def __init__(self, xy, width, height, angle=0.0, **kwargs):
688688

689689
Patch.__init__(self, **kwargs)
690690

691-
self._x = float(xy[0])
692-
self._y = float(xy[1])
693-
self._width = float(width)
694-
self._height = float(height)
691+
self._x = xy[0]
692+
self._y = xy[1]
693+
self._width = width
694+
self._height = height
695695
self.angle = float(angle)
696696
# Note: This cannot be calculated until this is added to an Axes
697697
self._rect_transform = transforms.IdentityTransform()

lib/matplotlib/testing/jpl_units/UnitDbl.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ def __nonzero__( self ):
116116
= RETURN VALUE
117117
- Returns true if the value is non-zero.
118118
"""
119-
return self._value.__nonzero__()
119+
if six.PY3:
120+
return self._value.__bool__()
121+
else:
122+
return self._value.__nonzero__()
120123

121124
if six.PY3:
122125
__bool__ = __nonzero__

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,13 @@ def test_ls_ds_conflict():
49464946
plt.plot(range(32), linestyle='steps-pre:', drawstyle='steps-post')
49474947

49484948

4949+
def test_bar_uint8():
4950+
xs = [0, 1, 2, 3]
4951+
b = plt.bar(np.array(xs, dtype=np.uint8), [2, 3, 4, 5])
4952+
for (patch, x) in zip(b.patches, xs):
4953+
assert patch.xy[0] == x
4954+
4955+
49494956
@image_comparison(baseline_images=['date_timezone_x'], extensions=['png'])
49504957
def test_date_timezone_x():
49514958
# Tests issue 5575

lib/matplotlib/tests/test_patches.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,16 @@ def test_adding_rectangle_patch_with_polar_projection():
351351
)
352352
)
353353
ax.set_rmax(2)
354+
355+
356+
@image_comparison(baseline_images=['units_rectangle'], extensions=['png'])
357+
def test_units_rectangle():
358+
import matplotlib.testing.jpl_units as U
359+
U.register()
360+
361+
p = mpatches.Rectangle((5*U.km, 6*U.km), 1*U.km, 2*U.km)
362+
363+
fig, ax = plt.subplots()
364+
ax.add_patch(p)
365+
ax.set_xlim([4*U.km, 7*U.km])
366+
ax.set_ylim([5*U.km, 9*U.km])

lib/matplotlib/transforms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,8 @@ def rotate_deg_around(self, x, y, degrees):
19081908
calls to :meth:`rotate`, :meth:`rotate_deg`, :meth:`translate`
19091909
and :meth:`scale`.
19101910
"""
1911+
# Cast to float to avoid wraparound issues with uint8's
1912+
x, y = float(x), float(y)
19111913
return self.translate(-x, -y).rotate_deg(degrees).translate(x, y)
19121914

19131915
def translate(self, tx, ty):

0 commit comments

Comments
 (0)