TypeError:' Circle' object不支持索引

时间:2017-03-25 10:05:53

标签: python animation

我正在使用python代码来模拟任何给定系统的轨道运动。我使用FuncAnimation来动画一些圆圈的位置。问题是我试图让它尽可能地通用,所以我认为一个包含不同补丁的数组,我在每一帧都会更新,这是一个好主意。但是每次运行它时我都会收到此错误:TypeError: 'Circle' object does not support indexing

以下是动画代码的一部分:

# This class method is the core of the animation and how the different CelestialBodies interact.
def Animation(self,a):
    for u in range(len(self.system)-1):
        self.system[u].Position(self.system)
        self.patches[u].center = (self.system[u].P[-1][0],self.system[u].P[-1][1])
    return self.patches

# This is the main class method, which combines the rest of methods to run the animation.
def run(self):
    # Create the axes for the animation
    fig = plt.figure()
    ax = plt.axes()
    ax.axis('scaled')
    ax.set_xlim(-self.max, self.max)
    ax.set_ylim(-self.max, self.max)

    # Create the patches and add them to the axes
    self.patches = []
    for q in range(len(self.system)-1):
        self.patches.append(plt.Circle(plt.Circle((0,0), 695700000, color='black')))
        ax.add_patch(self.patches[q])
    # Call the animation and display the plot
    anim = FuncAnimation(fig, self.Animation, init_func=self.init, frames=10, repeat=True, interval=1, blit=False)
    plt.title("Solar System - Numerical Intregation Simulation")
    plt.xlabel("Position in x (m)")
    plt.ylabel("Position in y (m)")
    plt.grid()
    plt.show()
# Constructor for the animation
def init(self):
    return self.patches

整个Traceback如下:

Traceback (most recent call last):
  File "SolarSystem4.py", line 145, in <module> SolarSystem.run()
  File "SolarSystem4.py", line 132, in run ax.add_patch(self.patches[q])
  File      "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 1562, in add_patch
self._update_patch_limits(p)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 1580, in _update_patch_limits
xys = patch.get_patch_transform().transform(vertices)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/patches.py", line 1256, in get_patch_transform
self._recompute_transform()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/patches.py", line 1240, in _recompute_transform
center = (self.convert_xunits(self.center[0]),
TypeError: 'Circle' object does not support indexing

1 个答案:

答案 0 :(得分:1)

看起来问题出在then finish, and the program finishes, having output a函数中for循环内的第一行。这里:

run

您正在构造一个self.patches.append(plt.Circle(plt.Circle((0,0), 695700000, color='black'))) 对象,然后将该对象作为第一个参数传递给另一个Circle对象的构造函数。 Circle的第一个参数是圆心的Circle.__init__坐标。因此,当您创建第二个xy时,它期望形式为Circle的元组作为第一个参数,而是获得整个(x,y)对象,因此最终得到一个属性如:

Circle

而不是

self.center = Circle(....)

在调用尝试索引self.center = (0,0) # or some other coordinates 属性的_recompute_transform方法之前,这不会导致任何问题。索引元组是没有问题的,但它不能索引self.center,因此它会抛出错误。要修复,请将有问题的行更改为:

Circle

换句话说,一次只能生成一个self.patches.append(plt.Circle((0,0), 695700000, color='black')) 并为其Circle方法提供它所期望的参数。