Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 83b546b

Browse files
committed
Replace use of Bbox.bounds by appropriate properties.
It's much easier to just use `bbox.y1` instead of `x0, y0, w, h = bbox.bounds; y0 + h`, for example.
1 parent a6c3c22 commit 83b546b

12 files changed

Lines changed: 51 additions & 105 deletions

File tree

examples/event_handling/viewlims.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, h=500, w=500, niter=50, radius=2., power=2):
3131
self.radius = radius
3232
self.power = power
3333

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

4747
def ax_update(self, ax):
4848
ax.set_autoscale_on(False) # Otherwise, infinite loop
49-
5049
# Get the number of points from the number of pixels in the window
51-
dims = ax.patch.get_window_extent().bounds
52-
self.width = int(dims[2] + 0.5)
53-
self.height = int(dims[2] + 0.5)
54-
50+
self.width, self.height = \
51+
np.round(ax.patch.get_window_extent().size).astype(int)
5552
# Get the range for the new area
56-
xstart, ystart, xdelta, ydelta = ax.viewLim.bounds
57-
xend = xstart + xdelta
58-
yend = ystart + ydelta
59-
53+
vl = ax.viewLim
54+
extent = vl.x0, vl.x1, vl.y0, vl.y1
6055
# Update the image object with our new data and extent
6156
im = ax.images[-1]
62-
im.set_data(self.__call__(xstart, xend, ystart, yend))
63-
im.set_extent((xstart, xend, ystart, yend))
57+
im.set_data(self.compute_image(*extent))
58+
im.set_extent(extent)
6459
ax.figure.canvas.draw_idle()
6560

61+
6662
md = MandelbrotDisplay()
67-
Z = md(-2., 0.5, -1.25, 1.25)
63+
Z = md.compute_image(-2., 0.5, -1.25, 1.25)
6864

6965
fig1, (ax1, ax2) = plt.subplots(1, 2)
7066
ax1.imshow(Z, origin='lower',

lib/matplotlib/_constrained_layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
167167
if do_suptitle:
168168
bbox = invTransFig(
169169
suptitle.get_window_extent(renderer=renderer))
170-
height = bbox.y1 - bbox.y0
170+
height = bbox.height
171171
if np.isfinite(height):
172172
# reserve at top of figure include an h_pad above and below
173173
suptitle._layoutbox.edit_height(height + h_pad * 2)

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,8 +1642,7 @@ def apply_aspect(self, position=None):
16421642
xsize = max(abs(xmax - xmin), 1e-30)
16431643
ysize = max(abs(ymax - ymin), 1e-30)
16441644

1645-
l, b, w, h = position.bounds
1646-
box_aspect = fig_aspect * (h / w)
1645+
box_aspect = fig_aspect * (position.height / position.width)
16471646
data_ratio = box_aspect / aspect
16481647

16491648
y_expander = data_ratio * xsize / ysize - 1

lib/matplotlib/backends/_backend_tk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ def __init__(self, figure, master=None, resize_callback=None):
178178
super(FigureCanvasTk, self).__init__(figure)
179179
self._idle = True
180180
self._idle_callback = None
181-
t1, t2, w, h = self.figure.bbox.bounds
182-
w, h = int(w), int(h)
181+
w, h = self.figure.bbox.size.astype(int)
183182
self._tkcanvas = tk.Canvas(
184183
master=master, background="white",
185184
width=w, height=h, borderwidth=0, highlightthickness=0)

lib/matplotlib/backends/backend_agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def draw(self):
385385
super().draw()
386386

387387
def get_renderer(self, cleared=False):
388-
l, b, w, h = self.figure.bbox.bounds
388+
w, h = self.figure.bbox.size
389389
key = w, h, self.figure.dpi
390390
reuse_renderer = (hasattr(self, "renderer")
391391
and getattr(self, "_lastKey", None) == key)

lib/matplotlib/backends/backend_ps.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,7 @@ def draw_image(self, gc, x, y, im, transform=None):
312312

313313
clip = []
314314
if bbox is not None:
315-
clipx, clipy, clipw, cliph = bbox.bounds
316-
clip.append(
317-
'%s clipbox' % _nums_to_str(clipw, cliph, clipx, clipy))
315+
clip.append('%s clipbox' % _nums_to_str(*bbox.size, *bbox.p0))
318316
if clippath is not None:
319317
id = self._get_clip_path(clippath, clippath_trans)
320318
clip.append('%s' % id)
@@ -690,8 +688,8 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
690688

691689
cliprect = gc.get_clip_rectangle()
692690
if cliprect:
693-
x, y, w, h = cliprect.bounds
694-
write('%1.4g %1.4g %1.4g %1.4g clipbox\n' % (w, h, x, y))
691+
write('%1.4g %1.4g %1.4g %1.4g clipbox\n'
692+
% (*cliprect.size, *cliprect.p0))
695693
clippath, clippath_trans = gc.get_clip_path()
696694
if clippath:
697695
id = self._get_clip_path(clippath, clippath_trans)
@@ -846,11 +844,10 @@ def _print_figure(
846844
xo = 72 * 0.5 * (paper_width - width)
847845
yo = 72 * 0.5 * (paper_height - height)
848846

849-
l, b, w, h = self.figure.bbox.bounds
850847
llx = xo
851848
lly = yo
852-
urx = llx + w
853-
ury = lly + h
849+
urx = llx + self.figure.bbox.width
850+
ury = lly + self.figure.bbox.height
854851
rotation = 0
855852
if orientation is _Orientation.landscape:
856853
llx, lly, urx, ury = lly, llx, ury, urx
@@ -1004,11 +1001,10 @@ def _print_figure_tex(
10041001
xo = 0
10051002
yo = 0
10061003

1007-
l, b, w, h = self.figure.bbox.bounds
10081004
llx = xo
10091005
lly = yo
1010-
urx = llx + w
1011-
ury = lly + h
1006+
urx = llx + self.figure.bbox.width
1007+
ury = lly + self.figure.bbox.height
10121008
bbox = (llx, lly, urx, ury)
10131009

10141010
if dryrun:

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,9 @@ def get_diff_image(self):
206206
return buf.getvalue()
207207

208208
def get_renderer(self, cleared=None):
209-
# Mirrors super.get_renderer, but caches the old one
210-
# so that we can do things such as produce a diff image
211-
# in get_diff_image
212-
_, _, w, h = self.figure.bbox.bounds
213-
w, h = int(w), int(h)
209+
# Mirrors super.get_renderer, but caches the old one so that we can do
210+
# things such as produce a diff image in get_diff_image.
211+
w, h = self.figure.bbox.size.astype(int)
214212
key = w, h, self.figure.dpi
215213
try:
216214
self._lastKey, self._renderer
@@ -316,13 +314,11 @@ def handle_resize(self, event):
316314
fig = self.figure
317315
# An attempt at approximating the figure size in pixels.
318316
fig.set_size_inches(x / fig.dpi, y / fig.dpi, forward=False)
319-
320-
_, _, w, h = self.figure.bbox.bounds
321317
# Acknowledge the resize, and force the viewer to update the
322318
# canvas size to the figure's new size (which is hopefully
323319
# identical or within a pixel or so).
324320
self._png_is_old = True
325-
self.manager.resize(w, h)
321+
self.manager.resize(*fig.bbox.size)
326322
self.resize_event()
327323

328324
def handle_send_image_mode(self, event):
@@ -424,11 +420,8 @@ def set_window_title(self, title):
424420
def add_web_socket(self, web_socket):
425421
assert hasattr(web_socket, 'send_binary')
426422
assert hasattr(web_socket, 'send_json')
427-
428423
self.web_sockets.add(web_socket)
429-
430-
_, _, w, h = self.canvas.figure.bbox.bounds
431-
self.resize(w, h)
424+
self.resize(*self.canvas.figure.bbox.size)
432425
self._send_event('refresh')
433426

434427
def remove_web_socket(self, web_socket):

lib/matplotlib/backends/backend_wx.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,9 @@ def __init__(self, parent, id, figure):
519519
"""
520520

521521
FigureCanvasBase.__init__(self, figure)
522-
# Set preferred window size hint - helps the sizer (if one is
523-
# connected)
524-
l, b, w, h = figure.bbox.bounds
525-
w = math.ceil(w)
526-
h = math.ceil(h)
527-
522+
w, h = map(math.ceil, figure.bbox.size)
523+
# Set preferred window size hint - helps the sizer, if one is connected
528524
wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
529-
530525
# Create the drawing bitmap
531526
self.bitmap = wx.Bitmap(w, h)
532527
_log.debug("%s - __init__() - bitmap w:%d h:%d", type(self), w, h)
@@ -864,16 +859,11 @@ def print_xpm(self, filename, *args, **kwargs):
864859
def _print_image(self, filename, filetype, *args, **kwargs):
865860
origBitmap = self.bitmap
866861

867-
l, b, width, height = self.figure.bbox.bounds
868-
width = math.ceil(width)
869-
height = math.ceil(height)
870-
871-
self.bitmap = wx.Bitmap(width, height)
872-
862+
self.bitmap = wx.Bitmap(math.ceil(self.figure.bbox.width),
863+
math.ceil(self.figure.bbox.height))
873864
renderer = RendererWx(self.bitmap, self.figure.dpi)
874865

875866
gc = renderer.new_gc()
876-
877867
self.figure.draw(renderer)
878868

879869
# image is the object that we call SaveFile on.

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,16 @@ def blit(self, bbox=None):
3939
self.gui_repaint()
4040
return
4141

42-
l, b, w, h = bbox.bounds
43-
r = l + w
44-
t = b + h
45-
x = int(l)
46-
y = int(self.bitmap.GetHeight() - t)
47-
4842
srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
4943
srcDC = wx.MemoryDC()
5044
srcDC.SelectObject(srcBmp)
5145

5246
destDC = wx.MemoryDC()
5347
destDC.SelectObject(self.bitmap)
5448

55-
destDC.Blit(x, y, int(w), int(h), srcDC, x, y)
49+
x = int(bbox.x0)
50+
y = int(self.bitmap.GetHeight() - bbox.y1)
51+
destDC.Blit(x, y, int(bbox.width), int(bbox.height), srcDC, x, y)
5652

5753
destDC.SelectObject(wx.NullBitmap)
5854
srcDC.SelectObject(wx.NullBitmap)
@@ -71,22 +67,18 @@ def _convert_agg_to_wx_bitmap(agg, bbox):
7167
agg.buffer_rgba())
7268
else:
7369
# agg => rgba buffer -> bitmap => clipped bitmap
74-
l, b, width, height = bbox.bounds
75-
r = l + width
76-
t = b + height
77-
7870
srcBmp = wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height),
7971
agg.buffer_rgba())
8072
srcDC = wx.MemoryDC()
8173
srcDC.SelectObject(srcBmp)
8274

83-
destBmp = wx.Bitmap(int(width), int(height))
75+
destBmp = wx.Bitmap(int(bbox.width), int(bbox.height))
8476
destDC = wx.MemoryDC()
8577
destDC.SelectObject(destBmp)
8678

87-
x = int(l)
88-
y = int(int(agg.height) - t)
89-
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
79+
x = int(bbox.x0)
80+
y = int(int(agg.height) - bbox.y1)
81+
destDC.Blit(0, 0, int(bbox.width), int(bbox.height), srcDC, x, y)
9082

9183
srcDC.SelectObject(wx.NullBitmap)
9284
destDC.SelectObject(wx.NullBitmap)

lib/matplotlib/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,15 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
10331033
B[:, :, 3] = 255
10341034
A = B
10351035
self._is_grayscale = False
1036-
x0, y0, v_width, v_height = self.axes.viewLim.bounds
1036+
vl = self.axes.viewLim
10371037
l, b, r, t = self.axes.bbox.extents
10381038
width = (round(r) + 0.5) - (round(l) - 0.5)
10391039
height = (round(t) + 0.5) - (round(b) - 0.5)
10401040
width *= magnification
10411041
height *= magnification
10421042
im = _image.pcolor(self._Ax, self._Ay, A,
10431043
int(height), int(width),
1044-
(x0, x0+v_width, y0, y0+v_height),
1044+
(vl.x0, vl.x1, vl.y0, vl.y1),
10451045
_interpd_[self._interpolation])
10461046
return im, l, b, IdentityTransform()
10471047

0 commit comments

Comments
 (0)