Skip to content

Use Path.arc() to interpolate polar arcs. #14852

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 1 commit into from
Mar 13, 2020
Merged
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
58 changes: 52 additions & 6 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import matplotlib.axis as maxis
import matplotlib.markers as mmarkers
import matplotlib.patches as mpatches
import matplotlib.path as mpath
from matplotlib.path import Path
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
import matplotlib.spines as mspines
Expand Down Expand Up @@ -50,11 +50,57 @@ def transform_non_affine(self, tr):

def transform_path_non_affine(self, path):
# docstring inherited
vertices = path.vertices
if len(vertices) == 2 and vertices[0, 0] == vertices[1, 0]:
return mpath.Path(self.transform(vertices), path.codes)
ipath = path.interpolated(path._interpolation_steps)
return mpath.Path(self.transform(ipath.vertices), ipath.codes)
if not len(path) or path._interpolation_steps == 1:
return Path(self.transform_non_affine(path.vertices), path.codes)
xys = []
codes = []
last_t = last_r = None
for trs, c in path.iter_segments():
trs = trs.reshape((-1, 2))
if c == Path.LINETO:
(t, r), = trs
if t == last_t: # Same angle: draw a straight line.
xys.extend(self.transform_non_affine(trs))
codes.append(Path.LINETO)
elif r == last_r: # Same radius: draw an arc.
# The following is complicated by Path.arc() being
# "helpful" and unwrapping the angles, but we don't want
# that behavior here.
last_td, td = np.rad2deg([last_t, t])
if self._use_rmin and self._axis is not None:
r = ((r - self._axis.get_rorigin())
* self._axis.get_rsign())
if last_td <= td:
while td - last_td > 360:
arc = Path.arc(last_td, last_td + 360)
Copy link
Member

Choose a reason for hiding this comment

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

This block seems untested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test added, although it's not particularly interesting (I can't actually think of a very interesting test for that...)

xys.extend(arc.vertices[1:] * r)
codes.extend(arc.codes[1:])
last_td += 360
arc = Path.arc(last_td, td)
xys.extend(arc.vertices[1:] * r)
codes.extend(arc.codes[1:])
else:
# The reverse version also relies on the fact that all
# codes but the first one are the same.
while last_td - td > 360:
arc = Path.arc(last_td - 360, last_td)
Copy link
Member

Choose a reason for hiding this comment

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

As does this block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same as above

xys.extend(arc.vertices[::-1][1:] * r)
codes.extend(arc.codes[1:])
last_td -= 360
arc = Path.arc(td, last_td)
xys.extend(arc.vertices[::-1][1:] * r)
codes.extend(arc.codes[1:])
else: # Interpolate.
trs = cbook.simple_linear_interpolation(
Copy link
Member

Choose a reason for hiding this comment

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

And this block too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test added

np.row_stack([(last_t, last_r), trs]),
path._interpolation_steps)[1:]
xys.extend(self.transform_non_affine(trs))
codes.extend([Path.LINETO] * len(trs))
else: # Not a straight line.
xys.extend(self.transform_non_affine(trs))
codes.extend([c] * len(trs))
last_t, last_r = trs[-1]
return Path(xys, codes)

def inverted(self):
# docstring inherited
Expand Down
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/markevery_polar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,903 changes: 704 additions & 1,199 deletions lib/matplotlib/tests/baseline_images/test_axes/markevery_polar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/polar_axes.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/polar_axes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading