8000 Use builtin round instead of np.round for scalars. by anntzer · Pull Request #14616 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Use builtin round instead of np.round for scalars. #14616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/misc/custom_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, round_to=1.0):
self._round_to = round_to

def __call__(self, x, pos=None):
degrees = np.round(np.rad2deg(x) / self._round_to) * self._round_to
degrees = round(np.rad2deg(x) / self._round_to) * self._round_to
if rcParams['text.usetex'] and not rcParams['text.latex.unicode']:
return r"$%0.0f^\circ$" % degrees
else:
Expand Down
30 changes: 15 additions & 15 deletions examples/statistics/barchart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
Score = namedtuple('Score', ['score', 'percentile'])

# GLOBAL CONSTANTS
testNames = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility',
'Push Ups']
testMeta = dict(zip(testNames, ['laps', 'sec', 'min:sec', 'sec', '']))
test_names = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility',
'Push Ups']
test_meta = dict(zip(test_names, ['laps', 'sec', 'min:sec', 'sec', '']))


def attach_ordinal(num):
Expand All @@ -54,7 +54,7 @@ def format_score(scr, test):
info (like for pushups) then don't add the carriage return to
the string
"""
md = testMeta[test]
md = test_meta[test]
if md:
return '{0}\n{1}'.format(scr, md)
else:
Expand All @@ -63,10 +63,10 @@ def format_score(scr, test):

def format_ycursor(y):
y = int(y)
if y < 0 or y >= len(testNames):
if y < 0 or y >= len(test_names):
return ''
else:
return testNames[y]
return test_names[y]


def plot_student_results(student, scores, cohort_size):
Expand All @@ -75,12 +75,12 @@ def plot_student_results(student, scores, cohort_size):
fig.subplots_adjust(left=0.115, right=0.88)
fig.canvas.set_window_title('Eldorado K-8 Fitness Chart')

pos = np.arange(len(testNames))
pos = np.arange(len(test_names))

rects = ax1.barh(pos, [scores[k].percentile for k in testNames],
rects = ax1.barh(pos, [scores[k].percentile for k in test_names],
align='center',
height=0.5,
tick_label=testNames)
tick_label=test_names)

ax1.set_title(student.name)

Expand All @@ -95,7 +95,7 @@ def plot_student_results(student, scores, cohort_size):
# Set the right-hand Y-axis ticks and labels
ax2 = ax1.twinx()

scoreLabels = [format_score(scores[k].score, k) for k in testNames]
scoreLabels = [format_score(scores[k].score, k) for k in test_names]

# set the tick locations
ax2.set_yticks(pos)
Expand Down Expand Up @@ -156,11 +156,11 @@ def plot_student_results(student, scores, cohort_size):


student = Student('Johnny Doe', 2, 'boy')
scores = dict(zip(testNames,
(Score(v, p) for v, p in
zip(['7', '48', '12:52', '17', '14'],
np.round(np.random.uniform(0, 1,
len(testNames)) * 100, 0)))))
scores = dict(zip(
test_names,
(Score(v, p) for v, p in
zip(['7', '48', '12:52', '17', '14'],
np.round(np.random.uniform(0, 100, len(test_names)), 0)))))
cohort_size = 62 # The number of other 2nd grade boys

arts = plot_student_results(student, scores, cohort_size)
Expand Down
2 changes: 1 addition & 1 deletion examples/statistics/boxplot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
# X-axis tick labels with the sample medians to aid in comparison
# (just use two decimal places of precision)
pos = np.arange(num_boxes) + 1
upper_labels = [str(np.round(s, 2)) for s in medians]
upper_labels = [str(round(s, 2)) for s in medians]
weights = ['bold', 'semibold']
for tick, label in zip(range(num_boxes), ax1.get_xticklabels()):
k = tick % 2
Expand Down
2 changes: 1 addition & 1 deletion examples/ticks_and_spines/date_index_formatter2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, dates, fmt='%Y-%m-%d'):

def __call__(self, x, pos=0):
'Return the label for time x at position pos'
ind = int(np.round(x))
ind = int(round(x))
if ind >= len(self.dates) or ind < 0:
return ''
return dates.num2date(self.dates[ind]).strftime(self.fmt)
Expand Down
17 changes: 8 additions & 9 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):

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

def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
Expand All @@ -190,11 +190,11 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
xo, yo = font.get_bitmap_offset()
xo /= 64.0
yo /= 64.0
xd = -d * sin(radians(angle))
xd = d * sin(radians(angle))
yd = d * cos(radians(angle))

self._renderer.draw_text_image(
font, np.round(x - xd + xo), np.round(y + yd + yo) + 1, angle, gc)
x = round(x + xo + xd)
y = round(y + yo + yd)
self._renderer.draw_text_image(font, x, y + 1, angle, gc)

def get_text_width_height_descent(self, s, prop, ismath):
# docstring inherited
Expand Down Expand Up @@ -235,9 +235,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
w, h, d = self.get_text_width_height_descent(s, prop, ismath)
xd = d * sin(radians(angle))
yd = d * cos(radians(angle))
x = np.round(x + xd)
y = np.round(y + yd)

x = round(x + xd)
y = round(y + yd)
self._renderer.draw_text_image(Z, x, y, angle, gc)

def get_canvas_width_height(self):
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,8 @@ def cvt(length, upe=font.units_per_EM, nearest=True):
"Convert font coordinates to PDF glyph coordinates"
value = length / upe * 1000
if nearest:
return np.round(value)
# Perhaps best to round away from zero for bounding
# boxes and the like
return round(value)
# Best(?) to round away from zero for bounding boxes and the like.
if value < 0:
return math.floor(value)
else:
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,16 @@ def to_rgb(c):


def to_hex(c, keep_alpha=False):
"""Convert *c* to a hex color.
"""
Convert *c* to a hex color.

Uses the ``#rrggbb`` format if *keep_alpha* is False (the default),
``#rrggbbaa`` otherwise.
"""
c = to_rgba(c)
if not keep_alpha:
c = c[:3]
return "#" + "".join(format(int(np.round(val * 255)), "02x")
for val in c)
return "#" + "".join(format(int(round(val * 255)), "02x") for val in c)


### Backwards-compatible color-conversion API
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def __init__(self, t, fmt, tz=None):

def __call__(self, x, pos=0):
'Return the label for time *x* at position *pos*'
ind = int(np.round(x))
ind = int(round(x))
if ind >= len(self.t) or ind <= 0:
return ''
return num2date(self.t[ind], self.tz).strftime(self.fmt)
Expand Down
14 changes: 7 additions & 7 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def flush_images():
gc = renderer.new_gc()
gc.set_clip_rectangle(parent.bbox)
gc.set_clip_path(parent.get_clip_path())
renderer.draw_image(gc, np.round(l), np.round(b), data)
renderer.draw_image(gc, round(l), round(b), data)
gc.restore()
del image_group[:]

Expand Down Expand Up @@ -971,8 +971,8 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
self.is_grayscale = False
x0, y0, v_width, v_height = self.axes.viewLim.bounds
l, b, r, t = self.axes.bbox.extents
width = (np.round(r) + 0.5) - (np.round(l) - 0.5)
height = (np.round(t) + 0.5) - (np.round(b) - 0.5)
width = (round(r) + 0.5) - (round(l) - 0.5)
height = (round(t) + 0.5) - (round(b) - 0.5)
width *= magnification
height *= magnification
im = _image.pcolor(self._Ax, self._Ay, A,
Expand Down Expand Up @@ -1086,11 +1086,11 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
bg = mcolors.to_rgba(fc, 0)
bg = (np.array(bg)*255).astype(np.uint8)
l, b, r, t = self.axes.bbox.extents
width = (np.round(r) + 0.5) - (np.round(l) - 0.5)
height = (np.round(t) + 0.5) - (np.round(b) - 0.5)
width = (round(r) + 0.5) - (round(l) - 0.5)
height = (round(t) + 0.5) - (round(b) - 0.5)
# The extra cast-to-int is only needed for python2
width = int(np.round(width * magnification))
height = int(np.round(height * magnification))
width = int(round(width * magnification))
height = int(round(height * magnification))
if self._rgbacache is None:
A = self.to_rgba(self._A, bytes=True)
self._rgbacache = A
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -2241,9 +2241,9 @@ def _get_sawtooth_vertices(self, x0, y0, width, height, mutation_size):

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

x0, y0 = x0 - pad + tooth_size2, y0 - pad + tooth_size2
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/projections/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def __init__(self, round_to=1.0):
self._round_to = round_to

def __call__(self, x, pos=None):
degrees = (x / np.pi) * 180.0
degrees = np.round(degrees / self._round_to) * self._round_to
degrees = round(np.rad2deg(x) / self._round_to) * self._round_to
if rcParams['text.usetex'] and not rcParams['text.latex.unicode']:
return r"$%0.0f^\circ$" % degrees
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def _sub_labels(self, axis, subs=()):
minor_tlocs = axis.get_minorticklocs()
fmt.set_locs(minor_tlocs)
coefs = minor_tlocs / 10**(np.floor(np.log10(minor_tlocs)))
label_expected = [np.round(c) in subs for c in coefs]
label_expected = [round(c) in subs for c in coefs]
label_test = [fmt(x) != '' for x in minor_tlocs]
assert label_test == label_expected

Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,8 @@ def __call__(self, x, pos=None):
# only label the decades
fx = math.log(x) / math.log(b)
is_x_decade = is_close_to_int(fx)
exponent = np.round(fx) if is_x_decade else np.floor(fx)
coeff = np.round(x / b ** exponent)
exponent = round(fx) if is_x_decade else np.floor(fx)
coeff = round(x / b ** exponent)

if self.labelOnlyBase and not is_x_decade:
return ''
Expand Down Expand Up @@ -1116,8 +1116,8 @@ def __call__(self, x, pos=None):
# only label the decades
fx = math.log(x) / math.log(b)
is_x_decade = is_close_to_int(fx)
exponent = np.round(fx) if is_x_decade else np.floor(fx)
coeff = np.round(x / b ** exponent)
exponent = round(fx) if is_x_decade else np.floor(fx)
coeff = round(x / b ** exponent)
if is_x_decade:
fx = round(fx)

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
def _value_in_bounds(self, val):
"""Makes sure *val* is with given bounds."""
if self.valstep:
val = np.round((val - self.valmin)/self.valstep)*self.valstep
val += self.valmin
val = (self.valmin
+ round((val - self.valmin) / self.valstep) * self.valstep)

if val <= self.valmin:
if not self.closedmin:
Expand Down
14 changes: 7 additions & 7 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2042,21 +2042,21 @@ def _3d_extend_contour(self, cset, stride=5):

polyverts = []
normals = []
nsteps = np.round(len(topverts[0]) / stride)
nsteps = round(len(topverts[0]) / stride)
if nsteps <= 1:
if len(topverts[0]) > 1:
nsteps = 2
else:
continue

stepsize = (len(topverts[0]) - 1) / (nsteps - 1)
for i in range(int(np.round(nsteps)) - 1):
i1 = int(np.round(i * stepsize))
i2 = int(np.round((i + 1) * stepsize))
for i in range(int(round(nsteps)) - 1):
i1 = int(round(i * stepsize))
i2 = int(round((i + 1) * stepsize))
polyverts.append([topverts[0][i1],
topverts[0][i2],
botverts[0][i2],
botverts[0][i1]])
topverts[0][i2],
botverts[0][i2],
botverts[0][i1]])

# all polygons have 4 vertices, so vectorize
polyverts = np.array(polyverts)
Expand Down
0