如何修复TypeError:\uu init\uuu()缺少3个必需的位置参数:“xy”、“宽度”和“height”matplotlib.patches埃利斯

2024-06-16 10:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个学校项目,我正在用Python上的Bézier曲线制作一个基本的棒状人物动画。我刚开始编程,需要帮助。在

我已经成功地创建了一个基本的动画,一堆段连接在一起形成了一个棒状图形,我现在正试图把这些棒子替换成椭圆,这些椭圆会随着时间的推移而移动和旋转,我在代码中加入了一组Bézier曲线,就像我以前的线条一样。在

因为我不能发布图像,所以我会说我的棒状身材是以人形行走的方式从左向右移动的。每个身体部位都遵循一条由不同的Bézier曲线跟踪的指定路径。在

与matplotlib.patches补丁程序,我想带多个带参数椭圆(xy,height,width,angle)的椭圆,这样它们就可以替换我的线条了。我用我所有不同的身体部位的点组成纽比阵列(我有5个关键帧,所以每个身体部位有5个点)

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse

#This is to make the background of my plot
fig = plt.figure()
ax = plt.axes(xlim=(-3, 50), ylim=(0, 25))
ax.set_title('Bézier Animation')
ax.set_xlabel('X')
ax.set_ylabel('Y')
cercte = plt.Circle((0.5, 21), 2, color='black', fill=False)




def init():

    cercte.center = (0.5, 21)
    ax.add_patch(cercte)
    return cercte

ellipses = []

links = np.array([[0, 1], [1, 2], [1, 3], [2, 4], [3, 5], [1, 6], [6, 7], 
[6, 8], [7, 9], [8, 10], [9, 11], [10, 12]])

for ii in range(len(links[:, 0])):

    ellipses[ii] = Ellipse()

def distance(x1, y1, x2, y2)

    dx = x2 - x1
    dy = y2 - y1
    dsquared = dx**2 + dy**2
    result = dsquared**0.5
    return result

def ellipse(A, B):

    xy = (A + B) / 2
    width = 1
    height = distance(A[0], A[1], B[0], B[1])
    angle = np.arctan((B[1] - A[1]) / (B[0] - A[0]))

    return xy, width, height, angle

#These are all my arrays containing the coordinates of each dot needed
mte = np.array([[mte1[0], mte2[0], mte3[0], mte4[0], mte5[0]], [mte1[1], 
      mte2[1], mte3[1], mte4[1], mte5[1]]])

c = np.array([[c1[0], c2[0], c3[0], c4[0], c5[0]], [c1[1], c2[1], c3[1], 
    c4[1], c5[1]]])

e = np.array([[e1[0], e2[0], e3[0], e4[0], e5[0]], [e1[1], e2[1],  e3[1], 
    e4[1], e5[1]]])

cg = np.array([[cg1[0], cg2[0], cg3[0], cg4[0], cg5[0]], [cg1[1], cg2[1], 
     cg3[1], cg4[1], cg5[1]]])
cd = np.array([[cd1[0], cd2[0], cd3[0], cd4[0], cd5[0]], [cd1[1], cd2[1], 
     cd3[1], cd4[1], cd5[1]]])

mg = np.array([[mg1[0], mg2[0], mg3[0], mg4[0], mg5[0]], [mg1[1], mg2[1], 
     mg3[1], mg4[1], mg5[1]]])
md = np.array([[md1[0], md2[0], md3[0], md4[0], md5[0]], [md1[1], md2[1], 
     md3[1], md4[1], md5[1]]])

bs = np.array([[bs1[0], bs2[0], bs3[0], bs4[0], bs5[0]], [bs1[1], bs2[1], 
     bs3[1], bs4[1], bs5[1]]])

gg = np.array([[gg1[0], gg2[0], gg3[0], gg4[0], gg5[0]], [gg1[1], gg2[1], 
     gg3[1], gg4[1], gg5[1]]])
gd = np.array([[gd1[0], gd2[0], gd3[0], gd4[0], gd5[0]], [gd1[1], gd2[1], 
     gd3[1], gd4[1], gd5[1]]])

tg = np.array([[tg1[0], tg2[0], tg3[0], tg4[0], tg5[0]], [tg1[1], tg2[1], 
     tg3[1], tg4[1], tg5[1]]])
td = np.array([[td1[0], td2[0], td3[0], td4[0], td5[0]], [td1[1], td2[1], 
     td3[1], td4[1], td5[1]]])

og = np.array([[og1[0], og2[0], og3[0], og4[0], og5[0]], [og1[1], og2[1], 
     og3[1], og4[1], og5[1]]])
od = np.array([[od1[0], od2[0], od3[0], od4[0], od5[0]], [od1[1], od2[1], 
     od3[1], od4[1], od5[1]]])

#My bézier function
def bezier(A,t):
    return A[:, 0]*[(1 - t)**4] + 4*A[:, 1]*[(1 - t)**3]*t + 6*A[:, 2]*   
    [(1 - t)**2]*(t**2) + 4*A[:, 3]*(1 - t)*(t**3) + A[:, 4]*(t**4)

dots = []

dots.append(c)
dots.append(e)
dots.append(cg)
dots.append(cd)
dots.append(mg)
dots.append(md)
dots.append(bs)
dots.append(gg)
dots.append(gd)
dots.append(tg)
dots.append(td)
dots.append(og)
dots.append(od)

def animate(i):
    t = 1 / 200

    mtem = bezier(mte, t)

    bez = []

    for ii in range(len(dots)):

        bez.append(bezier(dots[ii], t))

    for ii in range(len(links[0, :])):

        xy, width, height, angle = ellipse(bez[links[ii][0]], bez[liens[ii][1]])
        #I'm not quite sure for this part right here
        ellipses[ii].xy = xy
        ellipses[ii].width = width     
        ellipses[ii].height = height
        ellipses[ii].angle = angle

    xc, yc = cercte.center
    xc = (mtem[0])
    yc = (mtem[1])
    cercte.center = (xc, yc)

    return cercte, ellipses,

anim = animation.FuncAnimation(Fig, animate, init_func=init, frames=200, interval=5, blit=True)

我的其余代码只是在一个animate函数中,该函数更新每个椭圆和圆中参数的值。我希望看到一个动画我的棍子图形移动,但与椭圆身体部分,而不是棍子。然而,自从我得到那个类型错误后,什么也没有出现。在init函数中,是否需要指定每个椭圆的初始位置和角度?如果是这样的话,那就需要一段时间了。在


Tags: defnpaxwidtharrayii椭圆dots
1条回答
网友
1楼 · 发布于 2024-06-16 10:19:28

您需要调用您的ellipse函数,并将返回值从matplotlib传递给Ellipse()函数

for ii in range(len(links[:, 0])):
    xy, width, height, angle = ellipse(a, b)
    ellipses.append(Ellipse(xy, width, height, angle))

你的代码来自哪里。你需要添加代码。在

相关问题 更多 >