matplotlib多边形中的手动旋转 导入matplotlib.pyplot作为plt 将matplotlib导入为mpl 将numpy作为np导入 打印(mpl.get\u backend()) def旋转(xy、deg、rp): “”“xy是路径,deg是旋转度,rp是旋转点”“” #先平移、旋转,然后重新平移 xy=xy rp i=0 而i

matplotlib多边形中的手动旋转 导入matplotlib.pyplot作为plt 将matplotlib导入为mpl 将numpy作为np导入 打印(mpl.get\u backend()) def旋转(xy、deg、rp): “”“xy是路径,deg是旋转度,rp是旋转点”“” #先平移、旋转,然后重新平移 xy=xy rp i=0 而i,matplotlib,Matplotlib,首先,rotate函数中有一个错误,因为y坐标考虑了新计算的x坐标,而不是原始坐标。一个简单的解决办法是 import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np print(mpl.get_backend()) def rotate(xy,deg,rp): """xy is the path, deg is degrees of rotation, rp is the rotation po

首先,
rotate
函数中有一个错误,因为y坐标考虑了新计算的x坐标,而不是原始坐标。一个简单的解决办法是

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
print(mpl.get_backend())

def rotate(xy,deg,rp):
    """xy is the path, deg is degrees of rotation, rp is the rotation point"""
    #translate first, rotate, then retranslate
    xy = xy-rp
    i=0
    while i<len(xy):
        xy[i][0]= xy[i][0]*np.cos(np.deg2rad(deg))-(xy[i][1]*np.sin(np.deg2rad(deg)))
        xy[i][1]= xy[i][0]*np.sin(np.deg2rad(deg))+xy[i][1]*np.cos(np.deg2rad(deg))
        i+=1
    return xy+rp


#fig = plt.figure()
#ax = fig.add_subplot(111)

f,(ax) = plt.subplots(1,1,figsize=(8,8))
f.subplots_adjust(hspace=0,wspace=0)
ax.set_xlim(0,10)
ax.set_ylim(-2,8)
plt.grid(True)
plt.xticks(np.arange(0, 10+1, 0.5))
plt.yticks(np.arange(-2, 8+1, 0.5))


def xy(height=1.3,width=2,center=(0,0)):

    num = []
    ran1 = range(0,361)
    b=height#height
    a=width#width
    i=360
    femur_dict = {}
    xcen=center[0]
    ycen=center[1]
    for counter in range(0,360):
        num.append(np.array([a*np.cos(np.deg2rad(counter))+xcen,b*np.sin(np.deg2rad(ran1[counter]))+ycen]))
        i-=1
    return num

scale = 1
t2 = mpl.patches.Polygon(xy(center=(7,7),height=1*scale,width=0.25*scale),fc="blue")
td2dis = ax.transData
coords = td2dis.transform(t2.get_xy()[270])
    #rotate transform
tr = mpl.transforms.Affine2D().rotate_deg_around(coords[0], coords[1], -90)
t = td2dis + tr
t3=mpl.patches.Polygon(t2.get_xy(),fc='green',alpha=0.5,zorder=10000)
t3.set_transform(t)

t4 = mpl.patches.Polygon(rotate(xy(center=(7,7),height=1*scale,width=0.25*scale),45,t2.get_xy()[270]),fc="red")

ax.add_patch(t4)

ax.add_patch(t2)
ax.add_patch(t3)


plt.show()
现在,当你询问t3的“原始坐标”时,有两种可能性

您是指数据坐标吗?

在这种情况下,它们位于
tr.transform(t3.get_xy())
中,其中
tr
是从上方旋转

您是指显示坐标吗?


这些都在
(tr+td2dis).transform(t3.get_xy())

很好,您修复了该函数。我没有注意到。不幸的是,数据坐标和显示坐标都不是我要找的。我在寻找一个函数来返回坐标(9,6),它是绿色椭圆上最远的点。可能是,因为在你的问题中,你甚至不知道你在寻找什么。当讨论这样的图像时,它必须是图形上最直观的数据点,你沿着x轴上的9和y轴上的6。您可以使用get_xy获得未转换的等效椭圆。但是在变换后的椭圆上,你不能得到那个点,你也不能用数据坐标技术或显示坐标技术得到它。我不知道你的意思
tr.transform(t3.get_xy())
提供转换后的数据。tr.transform(t3.get_xy())[0]=array([-37,1063.41666667])我在数据集中找不到(9,6)
def rotate(xy,deg,rp):
    """xy is the path, deg is degrees of rotation, rp is the rotation point"""
    #translate first, rotate, then retranslate
    xy = xy-rp
    i=0
    while i<len(xy):
        a = xy[i][0]*np.cos(np.deg2rad(deg))-(xy[i][1]*np.sin(np.deg2rad(deg)))
        b = xy[i][0]*np.sin(np.deg2rad(deg))+xy[i][1]*np.cos(np.deg2rad(deg))
        xy[i] = [a,b]
        i+=1
    return xy+rp
t2 = mpl.patches.Polygon(xy(center=(7,7),height=1,width=0.25),fc="blue")
td2dis = ax.transData
coords = t2.get_xy()[270]

tr = mpl.transforms.Affine2D().rotate_deg_around(coords[0], coords[1], -90)

t3=mpl.patches.Polygon(t2.get_xy(),fc='green',alpha=0.5,zorder=10000)
t3.set_transform(tr + td2dis)

ax.add_patch(t2)
ax.add_patch(t3)