Home > OS >  How to add parameters to Matplotlib Class Circle?
How to add parameters to Matplotlib Class Circle?

Time:11-08

The question is probably technically confusing, but I'm not sure how to actually ask this.

Here's the code:

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)

I get the following error with file names redacted:

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'?

When I call my class object it asks me about an animation. I'm trying to add a patch, a circle, to an axes and when I call my class I end up with the error above.

CodePudding user response:

Calling the parent class's __init__ method with super seems to solve the problem.

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
  • Related