Skip to content

Correct theta values when drawing a non-circular arc #8047

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 2 commits into from
Mar 10, 2017
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
8 changes: 8 additions & 0 deletions doc/api/api_changes/2017-02-10-DS_elliptical_arc_angle.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Elliptical arcs now drawn between correct angles
````````````````````````````````````````````````

The `matplotlib.patches.Arc` patch is now correctly drawn between the given
angles.

Previously a circular arc was drawn and then stretched into an ellipse,
so the resulting arc did not lie between *theta1* and *theta2*.
20 changes: 13 additions & 7 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,8 +1554,6 @@ def __init__(self, xy, width, height, angle=0.0,
self.theta1 = theta1
self.theta2 = theta2

self._path = Path.arc(self.theta1, self.theta2)

@allow_rasterization
def draw(self, renderer):
"""
Expand Down Expand Up @@ -1607,15 +1605,25 @@ def draw(self, renderer):

self._recompute_transform()

# Get the width and height in pixels
width = self.convert_xunits(self.width)
height = self.convert_yunits(self.height)

# If the width and height of ellipse are not equal, take into account
# stretching when calculating angles to draw between
def theta_stretch(theta, scale):
theta = np.deg2rad(theta)
x = np.cos(theta)
y = np.sin(theta)
return np.rad2deg(np.arctan2(scale * y, x))
theta1 = theta_stretch(self.theta1, width / height)
theta2 = theta_stretch(self.theta2, width / height)

# Get width and height in pixels
width, height = self.get_transform().transform_point(
(width, height))
inv_error = (1.0 / 1.89818e-6) * 0.5

if width < inv_error and height < inv_error:
# self._path = Path.arc(self.theta1, self.theta2)
self._path = Path.arc(theta1, theta2)
return Patch.draw(self, renderer)

def iter_circle_intersect_on_line(x0, y0, x1, y1):
Expand Down Expand Up @@ -1670,8 +1678,6 @@ def iter_circle_intersect_on_line_seg(x0, y0, x1, y1):
self.get_transform().inverted()
box_path = box_path.transformed(box_path_transform)

theta1 = self.theta1
theta2 = self.theta2
thetas = set()
# For each of the point pairs, there is a line segment
for p0, p1 in zip(box_path.vertices[:-1], box_path.vertices[1:]):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 34 additions & 8 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,32 @@ def test_canonical():
ax.plot([1, 2, 3])


@image_comparison(baseline_images=['arc_angles'], remove_text=True,
style='default', extensions=['png'])
def test_arc_angles():
from matplotlib import patches
# Ellipse parameters
w = 2
h = 1
centre = (0.2, 0.5)

fig, axs = plt.subplots(3, 3)
for i, ax in enumerate(axs.flat):
theta2 = i * 360 / 9
theta1 = theta2 - 45
ax.add_patch(patches.Ellipse(centre, w, h, alpha=0.3))
ax.add_patch(patches.Arc(centre, w, h, theta1=theta1, theta2=theta2))
# Straight lines intersecting start and end of arc
ax.plot([2 * np.cos(np.deg2rad(theta1)) + centre[0],
centre[0],
2 * np.cos(np.deg2rad(theta2)) + centre[0]],
[2 * np.sin(np.deg2rad(theta1)) + centre[1],
centre[1],
2 * np.sin(np.deg2rad(theta2)) + centre[1]])
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)


@image_comparison(baseline_images=['arc_ellipse'],
remove_text=True)
def test_arc_ellipse():
Expand All @@ -990,23 +1016,23 @@ def test_arc_ellipse():
width, height = 1e-1, 3e-1
angle = -30

theta = np.arange(0.0, 360.0, 1.0)*np.pi/180.0
x = width/2. * np.cos(theta)
y = height/2. * np.sin(theta)
theta = np.arange(0.0, 360.0, 1.0) * np.pi / 180.0
x = width / 2. * np.cos(theta)
y = height / 2. * np.sin(theta)

rtheta = angle*np.pi/180.
rtheta = angle * np.pi / 180.
R = np.array([
[np.cos(rtheta), -np.sin(rtheta)],
[np.sin(rtheta), np.cos(rtheta)],
])
[np.cos(rtheta), -np.sin(rtheta)],
[np.sin(rtheta), np.cos(rtheta)]])

x, y = np.dot(R, np.array([x, y]))
x += xcenter
y += ycenter

fig = plt.figure()
ax = fig.add_subplot(211, aspect='auto')
ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1)
ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow',
linewidth=1, zorder=1)

e1 = patches.Arc((xcenter, ycenter), width, height,
angle=angle, linewidth=2, fill=False, zorder=2)
Expand Down