Matplotlib: patch rotation

I wanted to rotate the Rectangle into matplotlib, but when I apply the transform, the rectangle no longer displays:

rect = mpl.patches.Rectangle((0.0120,0),0.1,1000) t = mpl.transforms.Affine2D().rotate_deg(45) rect.set_transform(t) 

Is this a known mistake or am I mistaken?

+14
python matplotlib
Nov 26 '10 at 12:00
source share
3 answers

Transformations on patches appear to be composites of several transformations for working with scaling and bounding box. Adding a transform to an existing plot transform seems to give something more than you expected. Although it looks like there is still an offset for development.

 import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib as mpl fig = plt.figure() ax = fig.add_subplot(111) rect = patches.Rectangle((0.0120,0),0.1,1000) t_start = ax.transData t = mpl.transforms.Affine2D().rotate_deg(-45) t_end = t_start + t rect.set_transform(t_end) print repr(t_start) print repr(t_end) ax.add_patch(rect) plt.show() 
+9
Nov 26 '10 at 16:17
source share

The patch in the provided code does not allow us to understand what is happening, so I made a clear demonstration, which I developed from the matplotlib example:

 import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib as mpl fig = plt.figure() ax = fig.add_subplot(111) r1 = patches.Rectangle((0,0), 20, 40, color="blue", alpha=0.50) r2 = patches.Rectangle((0,0), 20, 40, color="red", alpha=0.50) t2 = mpl.transforms.Affine2D().rotate_deg(-45) + ax.transData r2.set_transform(t2) ax.add_patch(r1) ax.add_patch(r2) plt.xlim(-20, 60) plt.ylim(-20, 60) plt.grid(True) plt.show() 

enter image description here

+17
03 Feb 2018-11-11T00:
source share

I believe you can add

ax.axis ("equal")

0
Jul 01 '19 at 3:14
source share



All Articles