Add angle kwarg to patches.Rectangle #1405

Merged
merged 5 commits into from Nov 13, 2012

Projects

None yet

4 participants

@dmcdougall
Member

Allows rotation of a rectangle upon instantiation.

Addresses #987.
The old attempt was #1156.

Member

Here's an example:

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
r = Rectangle((0.5, 1.0), 2, 1, angle=45.0)
ax.add_patch(r)
ax.set_xlim(0, 3)
ax.set_ylim(0, 3)
ax.set_aspect('equal')
fig.savefig('rect.pdf')
Member
pelson commented Oct 16, 2012

Nice solution to the problem. 👍

When will this be merged? It's really useful to me.

Member

I should add an entry to api_changes or whats_new (one of the other devs should advise me on this). It may be the case we are able to extend this for other artists, too. That should be done in a different pull request, though. This PR was only meant to satisfy the feature request in #987.

Member

On Sunday, November 4, 2012, Damon McDougall wrote:

I should add an entry to api_changes or whats_new (one of the other devs
should advise me on this). It may be the case we are able to extend this
for other artists, too. That should be done in a different pull request,
though. This PR was only meant to satisfy the feature request in #987#987
.

It should go into "what's new" because it doesn't require any changes to
existing code.

Member

Added a note in whats_new and added a test, too. Go me. When my friend Travis goes green, I consider this ready to merge. Others should review the code for any heinous style issues or hidden treasures.

@WeatherGod WeatherGod and 1 other commented on an outdated diff Nov 5, 2012
doc/users/whats_new.rst
@@ -20,6 +20,11 @@ revision, see the :ref:`github-stats`.
new in matplotlib-1.3
=====================
+Initialize a rotated rectangle
+------------------------------
+Damon McDougall extended the `~matplotlib.patches.Rectangle` constructor to
WeatherGod
WeatherGod Nov 5, 2012 Member

should be :class:~matplotlib.patches.Rectangle``

@WeatherGod WeatherGod and 1 other commented on an outdated diff Nov 5, 2012
lib/matplotlib/patches.py
@@ -574,7 +578,10 @@ def _update_patch_transform(self):
width = self.convert_xunits(self._width)
height = self.convert_yunits(self._height)
bbox = transforms.Bbox.from_bounds(x, y, width, height)
+ rot_trans = transforms.Affine2D()
+ rot_trans.translate(-x, -y).rotate_deg(self._angle).translate(x, y)
self._rect_transform = transforms.BboxTransformTo(bbox)
WeatherGod
WeatherGod Nov 5, 2012 Member

Why the translation?

dmcdougall
dmcdougall Nov 5, 2012 Member

Because I didn't know rotate_deg_around existed. I will push an update to use that instead.

@dmcdougall How's your friend Travis going?

Member

@wkerzendorf He gave this PR the all-clear. I'm just waiting on some of the other devs to give feedback and merge it. I am against merging my own pull requests unless they're trivial changes.

@pelson pelson merged commit 17e1cf8 into matplotlib:master Nov 13, 2012

1 check passed

default The Travis build passed
Details
Member
pelson commented Nov 13, 2012

Thanks @dmcdougall (and @wkerzendorf for the nudging 😉 ). Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment