I’ve only recently started learning Python, and I’m trying to simulate a stage.
Below is my code.
import matplotlib.pyplot as plt
import numpy as np
import math
class Light:
def __init__(self, colour, position, direction, intensity, distribution):
self.colour = colour
self.position = position
self.direction = direction
self.intensity = intensity
self.distribution = distribution
def calculate_cone(self):
spread_angle = math.radians(self.distribution)
half_cone_angle = spread_angle/2
start_angle = self.direction - half_cone_angle
end_angle = self.direction + half_cone_angle
class Stage:
def __init__(self, figsize=(10,10)):
self.fig, (self.ax0, self.ax1)=plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 10]}, figsize = (10,10))
self.ax0.set_aspect("equal")
self.ax0.fill([0,500,500,0],[0,0,50,50],'k')
self.ax1.set_aspect("equal")
self.ax1.fill([0,500,500,0],[0,0,500,500],'k')
self.title=("STAGEVIEW")
self.lights=[]
self.smoke_machines=[]
def add_circle(self, centre, radius, colour):
circle=plt.Circle(centre, radius)
self.ax0.add_patch(circle)
def add_circle(self, centre, radius, colour):
circle=plt.Circle(centre, radius)
self.ax0.add_patch(circle)
def add_light(self, light):
self.lights.append(light)
spread=np.array(light.distribution)
self.ax1.fill(spread[:,0], spread[:,1], colour=light.colour, alpha=0.5)
def simulate_lights(self):
for lights in self.lights:
light.calculate_cone()
def show(self):
plt.suptitle(self.title, fontsize="18")
stage = Stage()
stage.add_circle([50,25],20, 'steelblue')
stage.add_circle([150,25],20, 'lightblue')
stage.add_circle([250,25],20, 'steelblue')
stage.add_circle([350,25],20, 'lightblue')
stage.add_circle([450,25],20, 'steelblue')
lights = [Light(colour="red",position[250,490],direction=90,intensity=1,distribution=[[230,490],[270,490], [270,500],[230,500]])]
for light in lights:
stage.add_light(light)
stage.simulate_lights()
stage.show()
I keep getting the below error message, but can’t find where it’s coming from.
Traceback (most recent call last):
File "samplestage(1).py", line 75, in <module>
stage.add_light(light)
File "samplestage(1).py", line 56, in add_light
self.ax1.fill(spread[:,0], spread[:,1], colour=light.colour, alpha=0.5)
File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_axes.py", line 5037, in fill
patches = [*self._get_patches_for_fill(*args, data=data, **kwargs)]
File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 216, in __call__
yield from self._plot_args(this, kwargs)
File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 364, in _plot_args
for j in range(max(ncx, ncy))]
File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 364, in <listcomp>
for j in range(max(ncx, ncy))]
File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 307, in _makefill
seg.set(**kwargs)
File "/usr/local/lib/python3.6/dist-packages/matplotlib/artist.py", line 1101, in set
return self.update(props)
File "/usr/local/lib/python3.6/dist-packages/matplotlib/artist.py", line 1006, in update
ret = [_update_property(self, k, v) for k, v in props.items()]
File "/usr/local/lib/python3.6/dist-packages/matplotlib/artist.py", line 1006, in <listcomp>
ret = [_update_property(self, k, v) for k, v in props.items()]
File "/usr/local/lib/python3.6/dist-packages/matplotlib/artist.py", line 1002, in _update_property
.format(type(self).__name__, k))
AttributeError: 'Polygon' object has no property 'colour'