這個問題可能在技術上令人困惑,但我不確定如何實際問這個問題。
這是代碼:
import matplotlib.pyplot as plt
import matplotlib.patches as ptch
import numpy as np
import math
import random
class Particle(ptch.Circle):
def __init__(self, center = np.array([0.5,0.5]), radius = 1.0, color = '#FFFFFF', mass = 1, velocity = np.array([1,1]), acceleration = np.array([0,0])):
self.radius = radius
self.ax = acceleration[0]
self.ay = acceleration[1]
self.vx = velocity[0]
self.vy = velocity[1]
self.rx = center[0]
self.ry = center[1]
self.mass = mass
ptch.Circle.__init__(self, xy = (center[0], center[1]), radius = radius)
我收到以下錯誤,檔案名已編輯:
Traceback (most recent call last):
File "Particles_v1.py", line 73, in <module>
particle_list.append(Particle(center = rx_ry, radius = radius, velocity = velocity))
File "Particles_v1.py", line 9, in __init__
self.radius = radius
File "lib/python3.10/site-packages/matplotlib/patches.py", line 1888, in set_radius
self.width = self.height = 2 * radius
File "lib/python3.10/site-packages/matplotlib/patches.py", line 1614, in set_width
self.stale = True
File "lib/python3.10/site-packages/matplotlib/artist.py", line 296, in stale
if self.get_animated():
File "lib/python3.10/site-packages/matplotlib/artist.py", line 818, in get_animated
return self._animated
AttributeError: 'Particle' object has no attribute '_animated'. Did you mean: 'get_animated'?
當我呼叫我的類物件時,它會詢問我有關影片的資訊。我正在嘗試在軸上添加一個補丁,一個圓圈,當我打電話給我的班級時,我最終遇到了上面的錯誤。
uj5u.com熱心網友回復:
用 super呼叫父類的__init__方法似乎可以解決問題。
class Particle(ptch.Circle):
def __init__(self, center = np.array([0.5,0.5]), radius = 1.0, color = '#FFFFFF', mass = 1, velocity = np.array([1,1]), acceleration = np.array([0,0]), **kwargs):
super().__init__(center, radius, color=color, **kwargs)
self.ax = acceleration[0]
self.ay = acceleration[1]
self.vx = velocity[0]
self.vy = velocity[1]
self.rx = center[0]
self.ry = center[1]
self.mass = mass
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/529733.html
上一篇:我可以在matplotlib中為由多條平行線組成的繪圖制作自定義線型嗎
下一篇:如何移動plt.axhline
