I am struggling to find documentation regarding PyLab (part of matplotlib), specifically about the .get_patch() attribute.
I'm working on a project, and part of the project is to run an animation for the simulation. They have simply provided the code but it does not seem to be working on my end. Here is the provided code for the run method.
import pylab as pl
def run(self, num_frames, animate=False):
if animate:
f = pl.figure()
ax = pl.axes(xlim=(-10, 10), ylim=(-10, 10))
ax.add_artist(self._container.get_patch())
ax.add_patch(self._ball.get_patch())
for frame in range(num_frames):
self.next_collision()
if animate:
pl.pause(0.001)
if animate:
pl.show()
This method is part of a class, Simulation which I have defined as follows:
class Simulation():
def __init__(self, ball = Ball(), container = Container()):
self._container = container
self._ball = ball
Where Ball() and its daughter class, Container() are defined,
class Ball():
def __repr__(self):
return "%s(Mass = %g, radius = %g)" % ("Ball", self.mass, self.radius)
def __str__(self):
return "%g mass, %radius" % (self.mass, self.radius)
class Container(Ball):
def __repr__(self):
return "%s(radius = %g)" % ("Container", self.radius)
def __str__(self):
return "%sContainer radius =" % (self.radius)
def __init__(self, radius = 10, vel = np.array([0,0]), pos = np.array([0,0])):
self.radius = radius
After creating a Ball() object, a Container() object and then passing this to a Simulation() object called test, running the following in the console test.run(100, animate = True), results in this console response:
AttributeError: 'Container' object has no attribute 'get_patch'
I am new to Python, but from what I gather, this error is referring to the fact that the container (and also the ball) objects do not have an attribute get_patch (rather obviously I guess), but from what I gathered from the prompt that gave me the code, .get_patch is an attribute of matplotlib.
If it helps, I am aiming to get an image/simulation similar to this: