Skip to content

Commit

Permalink
Merge pull request matplotlib#17213 from anntzer/boundless
Browse files Browse the repository at this point in the history
MNT: Replace use of Bbox.bounds by appropriate properties.
  • Loading branch information
tacaswell authored Apr 27, 2020
2 parents b55f8c5 + 83b546b commit 1e94c13
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 105 deletions.
22 changes: 9 additions & 13 deletions examples/event_handling/viewlims.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, h=500, w=500, niter=50, radius=2., power=2):
self.radius = radius
self.power = power

def __call__(self, xstart, xend, ystart, yend):
def compute_image(self, xstart, xend, ystart, yend):
self.x = np.linspace(xstart, xend, self.width)
self.y = np.linspace(ystart, yend, self.height).reshape(-1, 1)
c = self.x + 1.0j * self.y
Expand All @@ -46,25 +46,21 @@ def __call__(self, xstart, xend, ystart, yend):

def ax_update(self, ax):
ax.set_autoscale_on(False) # Otherwise, infinite loop

# Get the number of points from the number of pixels in the window
dims = ax.patch.get_window_extent().bounds
self.width = int(dims[2] + 0.5)
self.height = int(dims[2] + 0.5)

self.width, self.height = \
np.round(ax.patch.get_window_extent().size).astype(int)
# Get the range for the new area
xstart, ystart, xdelta, ydelta = ax.viewLim.bounds
xend = xstart + xdelta
yend = ystart + ydelta

vl = ax.viewLim
extent = vl.x0, vl.x1, vl.y0, vl.y1
# Update the image object with our new data and extent
im = ax.images[-1]
im.set_data(self.__call__(xstart, xend, ystart, yend))
im.set_extent((xstart, xend, ystart, yend))
im.set_data(self.compute_image(*extent))
im.set_extent(extent)
ax.figure.canvas.draw_idle()


md = MandelbrotDisplay()
Z = md(-2., 0.5, -1.25, 1.25)
Z = md.compute_image(-2., 0.5, -1.25, 1.25)

fig1, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(Z, origin='lower',
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
if do_suptitle:
bbox = invTransFig(
suptitle.get_window_extent(renderer=renderer))
height = bbox.y1 - bbox.y0
height = bbox.height
if np.isfinite(height):
# reserve at top of figure include an h_pad above and below
suptitle._layoutbox.edit_height(height + h_pad * 2)
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1642,8 +1642,7 @@ def apply_aspect(self, position=None):
xsize = max(abs(xmax - xmin), 1e-30)
ysize = max(abs(ymax - ymin), 1e-30)

l, b, w, h = position.bounds
box_aspect = fig_aspect * (h / w)
box_aspect = fig_aspect * (position.height / position.width)
data_ratio = box_aspect / aspect

y_expander = data_ratio * xsize / ysize - 1
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ def __init__(self, figure, master=None, resize_callback=None):
super(FigureCanvasTk, self).__init__(figure)
self._idle = True
self._idle_callback = None
t1, t2, w, h = self.figure.bbox.bounds
w, h = int(w), int(h)
w, h = self.figure.bbox.size.astype(int)
self._tkcanvas = tk.Canvas(
master=master, background="white",
width=w, height=h, borderwidth=0, highlightthickness=0)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def draw(self):
super().draw()

def get_renderer(self, cleared=False):
l, b, w, h = self.figure.bbox.bounds
w, h = self.figure.bbox.size
key = w, h, self.figure.dpi
reuse_renderer = (hasattr(self, "renderer")
and getattr(self, "_lastKey", None) == key)
Expand Down
18 changes: 7 additions & 11 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ def draw_image(self, gc, x, y, im, transform=None):

clip = []
if bbox is not None:
clipx, clipy, clipw, cliph = bbox.bounds
clip.append(
'%s clipbox' % _nums_to_str(clipw, cliph, clipx, clipy))
clip.append('%s clipbox' % _nums_to_str(*bbox.size, *bbox.p0))
if clippath is not None:
id = self._get_clip_path(clippath, clippath_trans)
clip.append('%s' % id)
Expand Down Expand Up @@ -686,8 +684,8 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):

cliprect = gc.get_clip_rectangle()
if cliprect:
x, y, w, h = cliprect.bounds
write('%1.4g %1.4g %1.4g %1.4g clipbox\n' % (w, h, x, y))
write('%1.4g %1.4g %1.4g %1.4g clipbox\n'
% (*cliprect.size, *cliprect.p0))
clippath, clippath_trans = gc.get_clip_path()
if clippath:
id = self._get_clip_path(clippath, clippath_trans)
Expand Down Expand Up @@ -842,11 +840,10 @@ def _print_figure(
xo = 72 * 0.5 * (paper_width - width)
yo = 72 * 0.5 * (paper_height - height)

l, b, w, h = self.figure.bbox.bounds
llx = xo
lly = yo
urx = llx + w
ury = lly + h
urx = llx + self.figure.bbox.width
ury = lly + self.figure.bbox.height
rotation = 0
if orientation is _Orientation.landscape:
llx, lly, urx, ury = lly, llx, ury, urx
Expand Down Expand Up @@ -997,11 +994,10 @@ def _print_figure_tex(
xo = 0
yo = 0

l, b, w, h = self.figure.bbox.bounds
llx = xo
lly = yo
urx = llx + w
ury = lly + h
urx = llx + self.figure.bbox.width
ury = lly + self.figure.bbox.height
bbox = (llx, lly, urx, ury)

if dryrun:
Expand Down
17 changes: 5 additions & 12 deletions lib/matplotlib/backends/backend_webagg_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,9 @@ def get_diff_image(self):
return buf.getvalue()

def get_renderer(self, cleared=None):
# Mirrors super.get_renderer, but caches the old one
# so that we can do things such as produce a diff image
# in get_diff_image
_, _, w, h = self.figure.bbox.bounds
w, h = int(w), int(h)
# Mirrors super.get_renderer, but caches the old one so that we can do
# things such as produce a diff image in get_diff_image.
w, h = self.figure.bbox.size.astype(int)
key = w, h, self.figure.dpi
try:
self._lastKey, self._renderer
Expand Down Expand Up @@ -316,13 +314,11 @@ def handle_resize(self, event):
fig = self.figure
# An attempt at approximating the figure size in pixels.
fig.set_size_inches(x / fig.dpi, y / fig.dpi, forward=False)

_, _, w, h = self.figure.bbox.bounds
# Acknowledge the resize, and force the viewer to update the
# canvas size to the figure's new size (which is hopefully
# identical or within a pixel or so).
self._png_is_old = True
self.manager.resize(w, h)
self.manager.resize(*fig.bbox.size)
self.resize_event()

def handle_send_image_mode(self, event):
Expand Down Expand Up @@ -424,11 +420,8 @@ def set_window_title(self, title):
def add_web_socket(self, web_socket):
assert hasattr(web_socket, 'send_binary')
assert hasattr(web_socket, 'send_json')

self.web_sockets.add(web_socket)

_, _, w, h = self.canvas.figure.bbox.bounds
self.resize(w, h)
self.resize(*self.canvas.figure.bbox.size)
self._send_event('refresh')

def remove_web_socket(self, web_socket):
Expand Down
18 changes: 4 additions & 14 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,9 @@ def __init__(self, parent, id, figure):
"""

FigureCanvasBase.__init__(self, figure)
# Set preferred window size hint - helps the sizer (if one is
# connected)
l, b, w, h = figure.bbox.bounds
w = math.ceil(w)
h = math.ceil(h)

w, h = map(math.ceil, figure.bbox.size)
# Set preferred window size hint - helps the sizer, if one is connected
wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))

# Create the drawing bitmap
self.bitmap = wx.Bitmap(w, h)
_log.debug("%s - __init__() - bitmap w:%d h:%d", type(self), w, h)
Expand Down Expand Up @@ -864,16 +859,11 @@ def print_xpm(self, filename, *args, **kwargs):
def _print_image(self, filename, filetype, *args, **kwargs):
origBitmap = self.bitmap

l, b, width, height = self.figure.bbox.bounds
width = math.ceil(width)
height = math.ceil(height)

self.bitmap = wx.Bitmap(width, height)

self.bitmap = wx.Bitmap(math.ceil(self.figure.bbox.width),
math.ceil(self.figure.bbox.height))
renderer = RendererWx(self.bitmap, self.figure.dpi)

gc = renderer.new_gc()

self.figure.draw(renderer)

# image is the object that we call SaveFile on.
Expand Down
22 changes: 7 additions & 15 deletions lib/matplotlib/backends/backend_wxagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,16 @@ def blit(self, bbox=None):
self.gui_repaint()
return

l, b, w, h = bbox.bounds
r = l + w
t = b + h
x = int(l)
y = int(self.bitmap.GetHeight() - t)

srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
srcDC = wx.MemoryDC()
srcDC.SelectObject(srcBmp)

destDC = wx.MemoryDC()
destDC.SelectObject(self.bitmap)

destDC.Blit(x, y, int(w), int(h), srcDC, x, y)
x = int(bbox.x0)
y = int(self.bitmap.GetHeight() - bbox.y1)
destDC.Blit(x, y, int(bbox.width), int(bbox.height), srcDC, x, y)

destDC.SelectObject(wx.NullBitmap)
srcDC.SelectObject(wx.NullBitmap)
Expand All @@ -71,22 +67,18 @@ def _convert_agg_to_wx_bitmap(agg, bbox):
agg.buffer_rgba())
else:
# agg => rgba buffer -> bitmap => clipped bitmap
l, b, width, height = bbox.bounds
r = l + width
t = b + height

srcBmp = wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height),
agg.buffer_rgba())
srcDC = wx.MemoryDC()
srcDC.SelectObject(srcBmp)

destBmp = wx.Bitmap(int(width), int(height))
destBmp = wx.Bitmap(int(bbox.width), int(bbox.height))
destDC = wx.MemoryDC()
destDC.SelectObject(destBmp)

x = int(l)
y = int(int(agg.height) - t)
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
x = int(bbox.x0)
y = int(int(agg.height) - bbox.y1)
destDC.Blit(0, 0, int(bbox.width), int(bbox.height), srcDC, x, y)

srcDC.SelectObject(wx.NullBitmap)
destDC.SelectObject(wx.NullBitmap)
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,15 +1033,15 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
B[:, :, 3] = 255
A = B
self._is_grayscale = False
x0, y0, v_width, v_height = self.axes.viewLim.bounds
vl = self.axes.viewLim
l, b, r, t = self.axes.bbox.extents
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,
int(height), int(width),
(x0, x0+v_width, y0, y0+v_height),
(vl.x0, vl.x1, vl.y0, vl.y1),
_interpd_[self._interpolation])
return im, l, b, IdentityTransform()

Expand Down
27 changes: 6 additions & 21 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,18 +1757,10 @@ def bbox_artist(artist, renderer, props=None, fill=True):
pad = props.pop('pad', 4)
pad = renderer.points_to_pixels(pad)
bbox = artist.get_window_extent(renderer)
l, b, w, h = bbox.bounds
l -= pad / 2.
b -= pad / 2.
w += pad
h += pad
r = Rectangle(xy=(l, b),
width=w,
height=h,
fill=fill,
)
r.set_transform(transforms.IdentityTransform())
r.set_clip_on(False)
r = Rectangle(
xy=(bbox.x0 - pad / 2, bbox.y0 - pad / 2),
width=bbox.width + pad, height=bbox.height + pad,
fill=fill, transform=transforms.IdentityTransform(), clip_on=False)
r.update(props)
r.draw(renderer)

Expand All @@ -1779,17 +1771,10 @@ def draw_bbox(bbox, renderer, color='k', trans=None):
box returned by an artist's `.Artist.get_window_extent`
to test whether the artist is returning the correct bbox.
"""

l, b, w, h = bbox.bounds
r = Rectangle(xy=(l, b),
width=w,
height=h,
edgecolor=color,
fill=False,
)
r = Rectangle(xy=(bbox.x0, bbox.y0), width=bbox.width, height=bbox.height,
edgecolor=color, fill=False, clip_on=False)
if trans is not None:
r.set_transform(trans)
r.set_clip_on(False)
r.draw(renderer)


Expand Down
18 changes: 7 additions & 11 deletions lib/mpl_toolkits/axes_grid1/inset_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ def __init__(self, bbox_to_anchor, x_size, y_size, loc,
self.y_size = Size.from_any(y_size)

def get_extent(self, renderer):
x, y, w, h = self.get_bbox_to_anchor().bounds

bbox = self.get_bbox_to_anchor()
dpi = renderer.points_to_pixels(72.)

r, a = self.x_size.get_size(renderer)
width = w * r + a * dpi

width = bbox.width * r + a * dpi
r, a = self.y_size.get_size(renderer)
height = h * r + a * dpi
height = bbox.height * r + a * dpi

xd, yd = 0, 0

fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
Expand All @@ -120,21 +119,18 @@ def __init__(self, parent_axes, zoom, loc,
bbox_transform=None):
self.parent_axes = parent_axes
self.zoom = zoom

if bbox_to_anchor is None:
bbox_to_anchor = parent_axes.bbox

super().__init__(
bbox_to_anchor, None, loc, borderpad=borderpad,
bbox_transform=bbox_transform)

def get_extent(self, renderer):
bb = TransformedBbox(self.axes.viewLim,
self.parent_axes.transData)
x, y, w, h = bb.bounds
bb = TransformedBbox(self.axes.viewLim, self.parent_axes.transData)
fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
pad = self.pad * fontsize
return (abs(w * self.zoom) + 2 * pad, abs(h * self.zoom) + 2 * pad,
return (abs(bb.width * self.zoom) + 2 * pad,
abs(bb.height * self.zoom) + 2 * pad,
pad, pad)


Expand Down

0 comments on commit 1e94c13

Please sign in to comment.