Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';圆圈';对象不支持索引_Python_Animation - Fatal编程技术网

Python 类型错误:';圆圈';对象不支持索引

Python 类型错误:';圆圈';对象不支持索引,python,animation,Python,Animation,我正在编写一个python代码来模拟任何给定系统的轨道运动。我使用FuncAnimation来设置一些圆的位置的动画。问题是,我试图使它尽可能的通用,所以我认为一个带有不同补丁的数组,我在每一帧更新它,将是一个好主意。但每次运行它时都会出现这样的错误:TypeError:“Circle”对象不支持索引 以下是动画的部分代码: # This class method is the core of the animation and how the different CelestialBodies

我正在编写一个python代码来模拟任何给定系统的轨道运动。我使用FuncAnimation来设置一些圆的位置的动画。问题是,我试图使它尽可能的通用,所以我认为一个带有不同补丁的数组,我在每一帧更新它,将是一个好主意。但每次运行它时都会出现这样的错误:
TypeError:“Circle”对象不支持索引

以下是动画的部分代码:

# 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 (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
回溯(最近一次呼叫最后一次):
SolarSystem.run()中的文件“SolarSystem4.py”,第145行
文件“SolarSystem4.py”,第132行,运行ax.add\u补丁(self.patches[q])
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/Python/matplotlib/axes.py”,第1562行,在add_补丁中
自我更新补丁限制(p)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/Python/matplotlib/axes.py”,第1580行,在更新补丁中
xys=patch.get_patch_transform().transform(顶点)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/Python/matplotlib/patches.py”,第1256行,在get\u patch\u转换中
self.\u重新计算\u变换()
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/Python/matplotlib/patches.py”,第1240行,在“重新计算”转换中
中心=(self.convert_xunits(self.center[0]),
TypeError:“圆”对象不支持索引

问题似乎出在
运行
函数的for循环内的第一行。这里:

self.patches.append(plt.Circle(plt.Circle((0,0), 695700000, color='black')))
您正在构造一个
对象,然后将该对象作为第一个参数传递给另一个
对象的构造函数。该
圆的第一个参数。\uuuuu init\uuuuu
是圆中心的
xy
坐标。因此,当您创建第二个
时,它需要一个形式为
(x,y)
的元组作为第一个参数,但取而代之的是一个完整的
对象,因此它以如下属性结束:

self.center = Circle(....)
而不是

self.center = (0,0)  # or some other coordinates
这不会导致任何问题,直到调用了
\u recompute\u transform
方法,该方法尝试索引该
self.center
属性。索引元组没有问题,但它无法索引
圆圈,因此会抛出错误。若要修复,请将有问题的行更改为:

self.patches.append(plt.Circle((0,0), 695700000, color='black'))

换句话说,一次只做一个
圆圈
,并给它的
\uuuu init\uuuu
方法提供它期望的参数。

请发布整个回溯。现在你有了它。谢谢你,很抱歉:)