Skip to content

Make rounding behavior consistent #7573

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 3 commits into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
MNT: Replace use of builtin round() with numpy's.
The behavior of the builtin round() function changed between python 2
and 3. Use numpy's, which matches python 3's, so that we have consistent
rounding behavior between python versions. This helps keep image test
results (here or downstream) consistent between python versions.
  • Loading branch information
dopplershift committed Dec 6, 2016
commit 66daba6ea5c75a1672845cc6ac840aa1a7173174
6 changes: 3 additions & 3 deletions examples/pylab_examples/date_index_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from __future__ import print_function
import numpy
import numpy as np
from matplotlib.mlab import csv2rec
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
Expand All @@ -27,7 +27,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(round(x))
ind = int(np.round(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind if I push to your branch to change the import of numpy to follow the standards?

if ind >= len(self.dates) or ind < 0:
return ''

Expand All @@ -37,6 +37,6 @@ def __call__(self, x, pos=0):

fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(formatter)
ax.plot(numpy.arange(len(r)), r.close, 'o-')
ax.plot(np.arange(len(r)), r.close, 'o-')
fig.autofmt_xdate()
plt.show()
6 changes: 3 additions & 3 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):

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

def get_text_width_height_descent(self, s, prop, ismath):
"""
Expand Down Expand Up @@ -257,8 +257,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 = round(x + xd)
y = round(y + yd)
x = np.round(x + xd)
y = np.round(y + yd)

self._renderer.draw_text_image(Z, x, y, angle, gc)

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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, round(l), round(b), data)
renderer.draw_image(gc, np.round(l), np.round(b), data)
gc.restore()
del image_group[:]

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,8 +1760,8 @@ def _raw_ticks(self, vmin, vmax):
step = max(1, step)
best_vmin = (_vmin // step) * step

low = round(Base(step).le(_vmin - best_vmin) / step)
high = round(Base(step).ge(_vmax - best_vmin) / step)
low = np.round(Base(step).le(_vmin - best_vmin) / step)
high = np.round(Base(step).ge(_vmax - best_vmin) / step)
ticks = np.arange(low, high + 1) * step + best_vmin + offset
nticks = ((ticks <= vmax) & (ticks >= vmin)).sum()
if nticks >= self._min_n_ticks:
Expand Down