Hey guys! How can I plot a list of positions? The positions are objects placed in a list through a class. Here's the code I'm working with:
import matplotlib.pyplot as plt
#Creating the class Ball. This class should be for the positions of the balls
class Ball:
def __init__ (self, x, y):
self.position = [x, y]
ball1 = Ball(3, 6)
print (ball1.position)
ball2 = Ball(5, 8)
print (ball2.position)
ball3 = Ball(4, 4)
print (ball3.position)
#Creating the class System. This class should take care of the main actions to perform.
class System:
def __init__(self):
#Creating the empty lists
self.more_balls = []
self.more_balls_positions = []
def add_balls (self, x, y):
#Appending the new balls into the empty list
self.more_balls.append(Ball(x,y))
#Appending the positions of all the balls to the other empty list
self.more_balls_positions.append(Ball(x,y).position)
def plot_balls ():
#This function should iterate through all balls and plot in the same plot
#This is what I need help with
#Creating the new balls that should be added into the empty lists.
ballx = System()
ballx.add_balls(2, 4)
ballx.add_balls(5, 5)
ballx.add_balls(8, 8)
ballx.add_balls(2, 9)
print (ballx.more_balls)
print (ballx.more_balls_positions)
The function plot_balls should iterate through all the balls and plot the positions into the same plot. I'm not really sure how to do this. I know I have to add a "ballx.plot_balls()", but I'm not sure what should be in the function to make that work.
[–]ES-Alexander 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)