Python matplotlib中多边形的旋转和平移
我正在尝试旋转一个形状,然后使用下面的代码在相同的函数中进行翻译,但是,只有翻译有效。有人知道如何使这两种转换都发生吗?谢谢 这是我的密码:Python matplotlib中多边形的旋转和平移,python,matplotlib,rotation,geometry,Python,Matplotlib,Rotation,Geometry,我正在尝试旋转一个形状,然后使用下面的代码在相同的函数中进行翻译,但是,只有翻译有效。有人知道如何使这两种转换都发生吗?谢谢 这是我的密码: import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib as mpl from math import * #some helper values p=4 theta=pi/6 x1 = p*cos(
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl
from math import *
#some helper values
p=4
theta=pi/6
x1 = p*cos(theta/2)
y1 = p*sin(theta/2)
vertices =[(-x1-p/2,0), (-p/2, y1), (p/2, y1), (x1+p/2, 0), (p/2, -y1), (-p/2, -y1)]
midPoint = [3,4]
#set up the plot
fig = plt.figure()
ax = fig.add_subplot(111)
#function to rotate and translate the standard shape to a new position
def plot_polygon(vertices, midPoint, theta):
polygon = patches.Polygon(vertices, color="red", alpha=0.50)
r = mpl.transforms.Affine2D().rotate(theta) + ax.transData
t = mpl.transforms.Affine2D().translate(midPoint[0],midPoint[1]) + ax.transData
polygon.set_transform(r)
polygon.set_transform(t)
ax.add_patch(polygon)
plot_polygon(vertices, midPoint, theta)
plt.xlim(-30, 30)
plt.ylim(-30, 30)
plt.grid(True)
plt.show()
如果设置a=3,然后设置a=4,那么最后a将是4,而不是7
通过.set_transform设置转换,可以覆盖之前设置的转换,最后出现的就是所使用的转换
因此,您需要一个调用来设置_变换,使用组合变换,例如
r = mpl.transforms.Affine2D().rotate(theta)
t = mpl.transforms.Affine2D().translate(midPoint[0],midPoint[1])
tra = r + t + ax.transData
polygon.set_transform(tra)