MCPcopy
hub / github.com/matplotlib/matplotlib / _prepare_view_from_bbox

Method _prepare_view_from_bbox

lib/matplotlib/axes/_base.py:4373–4451  ·  view source on GitHub ↗

Helper function to prepare the new bounds from a bbox. This helper function returns the new x and y bounds from the zoom bbox. This a convenience method to abstract the bbox logic out of the base setter.

(self, bbox, direction='in',
                                mode=None, twinx=False, twiny=False)

Source from the content-addressed store, hash-verified

4371 self.set(**view)
4372
4373 def _prepare_view_from_bbox(self, bbox, direction='in',
4374 mode=None, twinx=False, twiny=False):
4375 """
4376 Helper function to prepare the new bounds from a bbox.
4377
4378 This helper function returns the new x and y bounds from the zoom
4379 bbox. This a convenience method to abstract the bbox logic
4380 out of the base setter.
4381 """
4382 if len(bbox) == 3:
4383 xp, yp, scl = bbox # Zooming code
4384 if scl == 0: # Should not happen
4385 scl = 1.
4386 if scl > 1:
4387 direction = 'in'
4388 else:
4389 direction = 'out'
4390 scl = 1/scl
4391 # get the limits of the axes
4392 (xmin, ymin), (xmax, ymax) = self.transData.transform(
4393 np.transpose([self.get_xlim(), self.get_ylim()]))
4394 # set the range
4395 xwidth = xmax - xmin
4396 ywidth = ymax - ymin
4397 xcen = (xmax + xmin)*.5
4398 ycen = (ymax + ymin)*.5
4399 xzc = (xp*(scl - 1) + xcen)/scl
4400 yzc = (yp*(scl - 1) + ycen)/scl
4401 bbox = [xzc - xwidth/2./scl, yzc - ywidth/2./scl,
4402 xzc + xwidth/2./scl, yzc + ywidth/2./scl]
4403 elif len(bbox) != 4:
4404 # should be len 3 or 4 but nothing else
4405 _api.warn_external(
4406 "Warning in _set_view_from_bbox: bounding box is not a tuple "
4407 "of length 3 or 4. Ignoring the view change.")
4408 return
4409
4410 # Original limits.
4411 xmin0, xmax0 = self.get_xbound()
4412 ymin0, ymax0 = self.get_ybound()
4413 # The zoom box in screen coords.
4414 startx, starty, stopx, stopy = bbox
4415 # Convert to data coords.
4416 (startx, starty), (stopx, stopy) = self.transData.inverted().transform(
4417 [(startx, starty), (stopx, stopy)])
4418 # Clip to axes limits.
4419 xmin, xmax = np.clip(sorted([startx, stopx]), xmin0, xmax0)
4420 ymin, ymax = np.clip(sorted([starty, stopy]), ymin0, ymax0)
4421 # Don't double-zoom twinned axes or if zooming only the other axis.
4422 if twinx or mode == "y":
4423 xmin, xmax = xmin0, xmax0
4424 if twiny or mode == "x":
4425 ymin, ymax = ymin0, ymax0
4426
4427 if direction == "in":
4428 new_xbound = xmin, xmax
4429 new_ybound = ymin, ymax
4430

Callers 3

_set_view_from_bboxMethod · 0.95
_set_view_from_bboxMethod · 0.80

Calls 8

get_xlimMethod · 0.95
get_ylimMethod · 0.95
get_xboundMethod · 0.95
get_yboundMethod · 0.95
transformMethod · 0.45
invertedMethod · 0.45
clipMethod · 0.45
get_transformMethod · 0.45

Tested by 1