【问题标题】:rotating and translating polygon in matplotlib在 matplotlib 中旋转和平移多边形
【发布时间】:2017-05-12 20:55:59
【问题描述】:

我正在尝试旋转一个形状,然后使用以下代码在同一个函数中进行翻译,但是,只有翻译有效。有谁知道如何做到这一点,所以这两种转变都会发生?谢谢!

这是我的代码:

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()

【问题讨论】:

    标签: python matplotlib rotation geometry


    【解决方案1】:

    如果您设置a = 3 和更高版本的a = 4,最后a 将是4 而不是7。

    通过.set_transform 设置转换,您将覆盖先前设置的转换,最后出现的就是使用的转换。

    因此,您需要使用组合转换对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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多