EDIT: The question was not duplicate, but about a subtlety in the argument, that isn't mentioned in the answers.
Trying to rotate rectangles in Matplotlib, I added transform
argument, but whatever its value is, the rectangle is simply broken.
Here's the code and outputs:
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.set_xlim(0, 20)
ax.set_ylim(0, 20)
plt.axis('off')
ax.add_patch(Rectangle(
(5, 5), 2, 2,
color='#444444', ec='#444444', capstyle='round',
linewidth=20, linestyle='-',
joinstyle='round',
#transform=None
))
Output:
Now I try to rotate it. Here are 3 ways, all produce the same output image.
#1 add trasform=None
to Rectangle parameters.
#2 add transform=matplotlib.transforms.Affine2D().rotate_deg_around(0, 0, 15)
#3 as in another question:
r = Rectangle(
(5, 5), 2, 2,
color='#444444', ec='#444444', capstyle='round',
linewidth=20, linestyle='-',
joinstyle='round',
)
t_start = ax.transData
t = mpl.transforms.Affine2D().rotate_deg(-1)
r.set_transform(t)
ax.add_patch(r)
The output is the same in all cases:
What am I doing wrong?
Matplotlib version is 3.4.2.