Skip to content

Line2d window extents #17199

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions doc/users/next_whats_new/2020-06-17-path-extents.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
New function to get Path's *stroked* Bbox
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A Path is typically drawn by stroking it (with some ``markeredgewidth``), an
operation which changes its bounding box in a nontrivial way, depending on the
Path's joinstyle, capstyle, miterlimit, and shape.

`~.path.Path.get_stroked_extents` was added to allow computation of the final
bounding box in pixel/points coordinates of the line, after it has been drawn
(and accounting for the joinstyle, capstyle, and miterlimit).
14 changes: 14 additions & 0 deletions lib/matplotlib/bezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ def degree(self):
"""Degree of the polynomial. One less the number of control points."""
return self._N - 1

@property
def tan_in(self):
if self._N < 2:
raise ValueError("Need at least two control points to get tangent "
"vector!")
return self.control_points[1] - self.control_points[0]

@property
def tan_out(self):
if self._N < 2:
raise ValueError("Need at least two control points to get tangent "
"vector!")
return self.control_points[-1] - self.control_points[-2]

@property
def polynomial_coefficients(self):
r"""
Expand Down
47 changes: 39 additions & 8 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,15 +605,46 @@ def set_picker(self, p):
self._picker = p

def get_window_extent(self, renderer):
bbox = Bbox([[0, 0], [0, 0]])
trans_data_to_xy = self.get_transform().transform
bbox.update_from_data_xy(trans_data_to_xy(self.get_xydata()),
ignore=True)
# correct for marker size, if any
"""
Get bbox of Line2D in pixel space.

Notes
-----
Both the (stroked) line itself or any markers that have been placed
every ``markevery`` vertices along the line could be responsible for a
`Line2D`'s extents.
"""
# marker contribution
if self._marker:
ms = (self._markersize / 72.0 * self.figure.dpi) * 0.5
bbox = bbox.padded(ms)
return bbox
pts_box = self._marker.get_drawn_bbox(
self._markersize, self._markeredgewidth)
pix_box = pts_box.transformed(
Affine2D().scale(self.figure.dpi / 72.0))
else:
pix_box = Bbox([[0, 0], [0, 0]])
marker_bbox = Bbox.null()
trans_data_to_xy = self.get_transform().transform
xy = trans_data_to_xy(self.get_xydata())
if self._markevery:
xy = xy[::self._markevery]
bottom_left = xy + np.array([pix_box.x0, pix_box.y0])
marker_bbox.update_from_data_xy(bottom_left, ignore=True)
top_right = xy + np.array([pix_box.x1, pix_box.y1])
marker_bbox.update_from_data_xy(top_right, ignore=False)

# line's contribution
if self.is_dashed():
cap = self._dashcapstyle
join = self._dashjoinstyle
else:
cap = self._solidcapstyle
join = self._solidjoinstyle
line_bbox = Bbox.null()
path, affine = (self._get_transformed_path()
.get_transformed_path_and_affine())
lw = self.get_linewidth() / 72.0 * self.figure.dpi
path_bbox = path.get_stroked_extents(lw, affine, join, cap)
return Bbox.union([path_bbox, marker_bbox])

@Artist.axes.setter
def axes(self, ax):
Expand Down
28 changes: 27 additions & 1 deletion lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

from . import cbook, rcParams
from .path import Path
from .transforms import IdentityTransform, Affine2D
from .transforms import IdentityTransform, Affine2D, Bbox

# special-purpose marker identifiers:
(TICKLEFT, TICKRIGHT, TICKUP, TICKDOWN,
Expand Down Expand Up @@ -908,3 +908,29 @@ def _set_x_filled(self):
self._alt_transform = Affine2D().translate(-0.5, -0.5)
self._transform.rotate_deg(rotate)
self._alt_transform.rotate_deg(rotate_alt)

def get_drawn_bbox(self, markersize, markeredgewidth, **kwargs):
"""
Get size of bbox of marker directly from its path.

Parameters
----------
markersize : float
"Size" of the marker, in points.
markeredgewidth : float, optional, default: 0
Width, in points, of the stroke used to create the marker's edge.
**kwargs
Forwarded to `~.path.Path.iter_angles`.

Returns
-------
bbox : matplotlib.transforms.Bbox
The extents of the marker including its edge (in points) if it were
centered at (0,0).
"""
if np.isclose(markersize, 0):
return Bbox([[0, 0], [0, 0]])
scale = Affine2D().scale(markersize)
transform = scale + self._transform
return self._path.get_stroked_extents(markeredgewidth, transform,
self._joinstyle, self._capstyle)
Loading