Skip to content

Commit 019d956

Browse files
committed
Use np.round for consistent behavior across py2/3
1 parent 39d9e9a commit 019d956

File tree

14 files changed

+46
-45
lines changed

14 files changed

+46
-45
lines changed

examples/api/custom_projection_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def __init__(self, round_to=1.0):
294294
self._round_to = round_to
295295

296296
def __call__(self, x, pos=None):
297-
degrees = round(np.degrees(x) / self._round_to) * self._round_to
297+
degrees = np.round(np.degrees(x) / self._round_to) * self._round_to
298298
# \u00b0 : degree symbol
299299
return "%d\u00b0" % degrees
300300

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,8 +2342,8 @@ def draw(self, renderer=None, inframe=False):
23422342
for z, im in zorder_images]
23432343

23442344
l, b, r, t = self.bbox.extents
2345-
width = int(mag * ((round(r) + 0.5) - (round(l) - 0.5)))
2346-
height = int(mag * ((round(t) + 0.5) - (round(b) - 0.5)))
2345+
width = int(mag * ((np.round(r) + 0.5) - (np.round(l) - 0.5)))
2346+
height = int(mag * ((np.round(t) + 0.5) - (np.round(b) - 0.5)))
23472347
im = mimage.from_images(height,
23482348
width,
23492349
ims)

lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
176176

177177
xd = descent * sin(radians(angle))
178178
yd = descent * cos(radians(angle))
179-
x = round(x + ox + xd)
180-
y = round(y - oy + yd)
179+
x = np.round(x + ox + xd)
180+
y = np.round(y - oy + yd)
181181
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)
182182

183183
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):

lib/matplotlib/backends/backend_cairo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def set_clip_rectangle(self, rectangle):
359359
if not rectangle: return
360360
x,y,w,h = rectangle.bounds
361361
# pixel-aligned clip-regions are faster
362-
x,y,w,h = round(x), round(y), round(w), round(h)
362+
x,y,w,h = np.round(x), np.round(y), np.round(w), np.round(h)
363363
ctx = self.ctx
364364
ctx.new_path()
365365
ctx.rectangle (x, self.renderer.height - h - y, w, h)

lib/matplotlib/backends/backend_gdk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
9191
for polygon in polygons:
9292
# draw_polygon won't take an arbitrary sequence -- it must be a list
9393
# of tuples
94-
polygon = [(int(round(x)), int(round(y))) for x, y in polygon]
94+
polygon = [(int(np.round(x)), int(np.round(y))) for x, y in polygon]
9595
if rgbFace is not None:
9696
saveColor = gc.gdkGC.foreground
9797
gc.gdkGC.foreground = gc.rgb_to_gdk_color(rgbFace)
@@ -281,7 +281,7 @@ def _get_pango_layout(self, s, prop):
281281
return value
282282

283283
size = prop.get_size_in_points() * self.dpi / 96.0
284-
size = round(size)
284+
size = np.round(size)
285285

286286
font_str = '%s, %s %i' % (prop.get_name(), prop.get_style(), size,)
287287
font = pango.FontDescription(font_str)
@@ -387,7 +387,7 @@ def set_dashes(self, dash_offset, dash_list):
387387
self.gdkGC.line_style = gdk.LINE_SOLID
388388
else:
389389
pixels = self.renderer.points_to_pixels(np.asarray(dash_list))
390-
dl = [max(1, int(round(val))) for val in pixels]
390+
dl = [max(1, int(np.round(val))) for val in pixels]
391391
self.gdkGC.set_dashes(dash_offset, dl)
392392
self.gdkGC.line_style = gdk.LINE_ON_OFF_DASH
393393

@@ -413,7 +413,7 @@ def set_linewidth(self, w):
413413
self.gdkGC.line_width = 0
414414
else:
415415
pixels = self.renderer.points_to_pixels(w)
416-
self.gdkGC.line_width = max(1, int(round(pixels)))
416+
self.gdkGC.line_width = max(1, int(np.round(pixels)))
417417

418418

419419
def new_figure_manager(num, *args, **kwargs):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def cvt(length, upe=font.units_per_EM, nearest=True):
824824
"Convert font coordinates to PDF glyph coordinates"
825825
value = length / upe * 1000
826826
if nearest:
827-
return round(value)
827+
return np.round(value)
828828
# Perhaps best to round away from zero for bounding
829829
# boxes and the like
830830
if value < 0:

lib/matplotlib/dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def __init__(self, t, fmt, tz=None):
609609

610610
def __call__(self, x, pos=0):
611611
'Return the label for time *x* at position *pos*'
612-
ind = int(round(x))
612+
ind = int(np.round(x))
613613
if ind >= len(self.t) or ind <= 0:
614614
return ''
615615

lib/matplotlib/image.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,10 @@ def make_image(self, magnification=1.0):
650650
im.apply_translation(tx, ty)
651651

652652
l, b, r, t = self.axes.bbox.extents
653-
widthDisplay = ((round(r*magnification) + 0.5) -
654-
(round(l*magnification) - 0.5))
655-
heightDisplay = ((round(t*magnification) + 0.5) -
656-
(round(b*magnification) - 0.5))
653+
widthDisplay = ((np.round(r*magnification) + 0.5) -
654+
(np.round(l*magnification) - 0.5))
655+
heightDisplay = ((np.round(t*magnification) + 0.5) -
656+
(np.round(b*magnification) - 0.5))
657657

658658
# resize viewport to display
659659
rx = widthDisplay / numcols
@@ -773,8 +773,8 @@ def make_image(self, magnification=1.0):
773773

774774
x0, y0, v_width, v_height = self.axes.viewLim.bounds
775775
l, b, r, t = self.axes.bbox.extents
776-
width = (round(r) + 0.5) - (round(l) - 0.5)
777-
height = (round(t) + 0.5) - (round(b) - 0.5)
776+
width = (np.round(r) + 0.5) - (np.round(l) - 0.5)
777+
height = (np.round(t) + 0.5) - (np.round(b) - 0.5)
778778
width *= magnification
779779
height *= magnification
780780
im = _image.pcolor(self._Ax, self._Ay, A,
@@ -897,11 +897,11 @@ def make_image(self, magnification=1.0):
897897
bg = mcolors.colorConverter.to_rgba(fc, 0)
898898
bg = (np.array(bg)*255).astype(np.uint8)
899899
l, b, r, t = self.axes.bbox.extents
900-
width = (round(r) + 0.5) - (round(l) - 0.5)
901-
height = (round(t) + 0.5) - (round(b) - 0.5)
900+
width = (np.round(r) + 0.5) - (np.round(l) - 0.5)
901+
height = (np.round(t) + 0.5) - (np.round(b) - 0.5)
902902
# The extra cast-to-int is only needed for python2
903-
width = int(round(width * magnification))
904-
height = int(round(height * magnification))
903+
width = int(np.round(width * magnification))
904+
height = int(np.round(height * magnification))
905905
if self._rgbacache is None:
906906
A = self.to_rgba(self._A, bytes=True)
907907
self._rgbacache = A
@@ -932,8 +932,8 @@ def draw(self, renderer, *args, **kwargs):
932932
gc.set_clip_path(self.get_clip_path())
933933
gc.set_alpha(self.get_alpha())
934934
renderer.draw_image(gc,
935-
round(self.axes.bbox.xmin),
936-
round(self.axes.bbox.ymin),
935+
np.round(self.axes.bbox.xmin),
936+
np.round(self.axes.bbox.ymin),
937937
im)
938938
gc.restore()
939939
self.stale = False
@@ -1093,7 +1093,7 @@ def draw(self, renderer, *args, **kwargs):
10931093
gc.set_clip_rectangle(self.figure.bbox)
10941094
gc.set_clip_path(self.get_clip_path())
10951095
gc.set_alpha(self.get_alpha())
1096-
renderer.draw_image(gc, round(self.ox), round(self.oy), im)
1096+
renderer.draw_image(gc, np.round(self.ox), np.round(self.oy), im)
10971097
gc.restore()
10981098
self.stale = False
10991099

@@ -1212,8 +1212,8 @@ def make_image(self, renderer, magnification=1.0):
12121212
im.set_resample(self._resample)
12131213

12141214
l, b, r, t = self.get_window_extent(renderer).extents # bbox.extents
1215-
widthDisplay = abs(round(r) - round(l))
1216-
heightDisplay = abs(round(t) - round(b))
1215+
widthDisplay = abs(np.round(r) - np.round(l))
1216+
heightDisplay = abs(np.round(t) - np.round(b))
12171217
widthDisplay *= magnification
12181218
heightDisplay *= magnification
12191219

@@ -1245,7 +1245,7 @@ def draw(self, renderer, *args, **kwargs):
12451245

12461246
l = np.min([x0, x1])
12471247
b = np.min([y0, y1])
1248-
renderer.draw_image(gc, round(l), round(b), im)
1248+
renderer.draw_image(gc, np.round(l), np.round(b), im)
12491249
gc.restore()
12501250
self.stale = True
12511251

lib/matplotlib/mathtext.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
import matplotlib.colors as mcolors
6262
import matplotlib._png as _png
63+
6364
####################
6465

6566

@@ -2120,10 +2121,10 @@ def hlist_out(self, box):
21202121
if glue_sign == 1: # stretching
21212122
if glue_spec.stretch_order == glue_order:
21222123
cur_glue += glue_spec.stretch
2123-
cur_g = round(clamp(float(box.glue_set) * cur_glue))
2124+
cur_g = np.round(clamp(float(box.glue_set) * cur_glue))
21242125
elif glue_spec.shrink_order == glue_order:
21252126
cur_glue += glue_spec.shrink
2126-
cur_g = round(clamp(float(box.glue_set) * cur_glue))
2127+
cur_g = np.round(clamp(float(box.glue_set) * cur_glue))
21272128
rule_width += cur_g
21282129
self.cur_h += rule_width
21292130
self.cur_s -= 1
@@ -2176,10 +2177,10 @@ def vlist_out(self, box):
21762177
if glue_sign == 1: # stretching
21772178
if glue_spec.stretch_order == glue_order:
21782179
cur_glue += glue_spec.stretch
2179-
cur_g = round(clamp(float(box.glue_set) * cur_glue))
2180+
cur_g = np.round(clamp(float(box.glue_set) * cur_glue))
21802181
elif glue_spec.shrink_order == glue_order: # shrinking
21812182
cur_glue += glue_spec.shrink
2182-
cur_g = round(clamp(float(box.glue_set) * cur_glue))
2183+
cur_g = np.round(clamp(float(box.glue_set) * cur_glue))
21832184
rule_height += cur_g
21842185
self.cur_v += rule_height
21852186
elif isinstance(p, Char):

lib/matplotlib/mlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@ def frange(xini, xfin=None, delta=None, **kw):
22152215
npts = kw['npts']
22162216
delta = (xfin-xini)/float(npts-endpoint)
22172217
except KeyError:
2218-
npts = int(round((xfin-xini)/delta)) + endpoint
2218+
npts = int(np.round((xfin-xini)/delta)) + endpoint
22192219
# round finds the nearest, so the endpoint can be up to
22202220
# delta/2 larger than xfin.
22212221

lib/matplotlib/patches.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,9 +2291,9 @@ def _get_sawtooth_vertices(self, x0, y0, width, height, mutation_size):
22912291

22922292
# the sizes of the vertical and horizontal sawtooth are
22932293
# separately adjusted to fit the given box size.
2294-
dsx_n = int(round((width - tooth_size) / (tooth_size * 2))) * 2
2294+
dsx_n = int(np.round((width - tooth_size) / (tooth_size * 2))) * 2
22952295
dsx = (width - tooth_size) / dsx_n
2296-
dsy_n = int(round((height - tooth_size) / (tooth_size * 2))) * 2
2296+
dsy_n = int(np.round((height - tooth_size) / (tooth_size * 2))) * 2
22972297
dsy = (height - tooth_size) / dsy_n
22982298

22992299
x0, y0 = x0 - pad + tooth_size2, y0 - pad + tooth_size2

lib/matplotlib/projections/geo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, round_to=1.0):
3434

3535
def __call__(self, x, pos=None):
3636
degrees = (x / np.pi) * 180.0
37-
degrees = round(degrees / self._round_to) * self._round_to
37+
degrees = np.round(degrees / self._round_to) * self._round_to
3838
if rcParams['text.usetex'] and not rcParams['text.latex.unicode']:
3939
return r"$%0.0f^\circ$" % degrees
4040
else:

lib/matplotlib/ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ def __call__(self):
19371937
# TODO: Need a better way to figure out ndivs
19381938
ndivs = 1
19391939
else:
1940-
x = int(round(10 ** (np.log10(majorstep) % 1)))
1940+
x = int(np.round(10 ** (np.log10(majorstep) % 1)))
19411941
if x in [1, 5, 10]:
19421942
ndivs = 5
19431943
else:

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,17 +1949,17 @@ def _3d_extend_contour(self, cset, stride=5):
19491949

19501950
polyverts = []
19511951
normals = []
1952-
nsteps = round(len(topverts[0]) / stride)
1952+
nsteps = np.round(len(topverts[0]) / stride)
19531953
if nsteps <= 1:
19541954
if len(topverts[0]) > 1:
19551955
nsteps = 2
19561956
else:
19571957
continue
19581958

19591959
stepsize = (len(topverts[0]) - 1) / (nsteps - 1)
1960-
for i in range(int(round(nsteps)) - 1):
1961-
i1 = int(round(i * stepsize))
1962-
i2 = int(round((i + 1) * stepsize))
1960+
for i in range(int(np.round(nsteps)) - 1):
1961+
i1 = int(np.round(i * stepsize))
1962+
i2 = int(np.round((i + 1) * stepsize))
19631963
polyverts.append([topverts[0][i1],
19641964
topverts[0][i2],
19651965
botverts[0][i2],
@@ -2499,13 +2499,13 @@ def quiver(self, *args, **kwargs):
24992499
25002500
*pivot*: [ 'tail' | 'middle' | 'tip' ]
25012501
The part of the arrow that is at the grid point; the arrow
2502-
rotates about this point, hence the name *pivot*.
2502+
rotates about this point, hence the name *pivot*.
25032503
Default is 'tail'
2504-
2504+
25052505
*normalize*: [False | True]
2506-
When True, all of the arrows will be the same length. This
2506+
When True, all of the arrows will be the same length. This
25072507
defaults to False, where the arrows will be different lengths
2508-
depending on the values of u,v,w.
2508+
depending on the values of u,v,w.
25092509
25102510
Any additional keyword arguments are delegated to
25112511
:class:`~matplotlib.collections.LineCollection`

0 commit comments

Comments
 (0)