如何解决此错误:AttributeError: ‘Polygon’对象没有属性’colour’。

huangapple go评论95阅读模式
英文:

How to remove this error: AttributeError: 'Polygon' object has no property 'colour'

问题

我尝试在Python中模拟一个音乐舞台,并且在尝试向舞台添加灯光时遇到了这个错误。以下是我的代码:

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_light(self, light):
        self.lights.append(light)
        spread = np.array(light.distribution)
        self.ax1.fill(spread[:, 0], spread[:, 1], color=light.colour, alpha=0.5)

    def simulate_lights(self):
        for light 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()

你尝试向舞台添加灯光,希望在matplotlib中看到它,但一直遇到这个错误:

AttributeError: 'Polygon' object has no property 'colour'

这个错误是由于在self.ax1.fill()中使用了colour而不是color引起的。我已经将其更正为color,请尝试重新运行你的代码。

希望这可以帮助你解决问题。如果还有其他问题,请随时提问。

英文:

I'm trying to simulate a music stage in Python, and I keep coming across this error when trying to add lights to the stage. This is my code below.

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 tried to add a light to my stage, and thought I'd be able to see it in matplotlib, but keep getting this error:
I have changed the light class to the correct class, but now I get a big error message regarding polygon colours.

    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'

I am not sure what to do with this error. I am going to be trying to add multiple lights, to match the circles on the 'stage', but I cannot even get one light to work.

I appreciate the help.

I'm just learning Python, so it can be hard to me to see where I went wrong.

Thank you

答案1

得分: 1

I figure it out now, it is "color" not "colour".
Also I change some codes, you can check it like:

spread_angle = np.radians(self.distribution)
...
self.ax1.fill(spread[:,0], spread[:,1], color=light.colour, alpha=0.5)
...
plt.show()
plt.close()
# stage.show()

the whole codes is here:

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 = np.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_light(self, light): 
        self.lights.append(light) 
        spread=np.array(light.distribution) 
        self.ax1.fill(spread[:,0], spread[:,1], color=light.colour, alpha=0.5) 

    def simulate_lights(self): 
        for lights in self.lights: 
            lights.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,390],direction=90,intensity=1,distribution=[[230,390],[270,390], [270,500],[230,500]])]

for light in lights: 
    stage.add_light(light) 
    stage.simulate_lights() 

plt.show()
plt.close()
# stage.show()

this is result:
如何解决此错误:AttributeError: ‘Polygon’对象没有属性’colour’。

英文:

I figure it out now, it is "color" not "colour".<br>
Also I change some codes, you can check it like: <br>

spread_angle = np.radians(self.distribution)
...
self.ax1.fill(spread[:,0], spread[:,1], color=light.colour, alpha=0.5)
...
plt.show()
plt.close()
# stage.show()

the whole codes is here<br>

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 = np.radians(self.distribution) #np.array([math.radians(i) for i in 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={&#39;height_ratios&#39;: [1, 10]}, figsize = (10,10))
self.ax0.set_aspect(&quot;equal&quot;)
self.ax0.fill([0,500,500,0],[0,0,50,50],&#39;k&#39;)
self.ax1.set_aspect(&quot;equal&quot;)
self.ax1.fill([0,500,500,0],[0,0,500,500],&#39;k&#39;)
self.title=(&quot;STAGEVIEW&quot;)
self.lights=[]
self.smoke_machines=[]
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], color=light.colour, alpha=0.5) 
def simulate_lights(self): 
for lights in self.lights: 
lights.calculate_cone() 
def show(self): 
plt.suptitle(self.title, fontsize=&quot;18&quot;) 
stage = Stage() 
stage.add_circle([50,25],20, &#39;steelblue&#39;) 
stage.add_circle([150,25],20, &#39;lightblue&#39;) 
stage.add_circle([250,25],20, &#39;steelblue&#39;)
stage.add_circle([350,25],20, &#39;lightblue&#39;)
stage.add_circle([450,25],20, &#39;steelblue&#39;)
lights = [Light(colour=&quot;red&quot;,position=[250,390],direction=90,intensity=1,distribution=[[230,390],[270,390], [270,500],[230,500]])]
for light in lights: 
stage.add_light(light) 
stage.simulate_lights() 
plt.show()
plt.close()
# stage.show()

this is result:<br>
如何解决此错误:AttributeError: ‘Polygon’对象没有属性’colour’。

答案2

得分: 0

你试图传递Light类的colour属性。

但是,你使用了self.colour,它引用了Stage类,而Stage类没有colour属性,因此引发了这个错误。

将你的add_light方法从这个:

def add_light(self, light):
    self.lights.append(light)
    spread=np.array(light.distribution)
    self.ax1.fill(spread[:,0], spread[:,1], colour=self.colour, alpha=0.5)

更新为这个:

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)

将解决你的问题。

英文:

You are trying to pass the colour attribute of the Light class.

But instead of using light.colour, you're using self.colour, which references the class Stage, which does not have the colour attribute, thus raising this error.

Updating your add_light method from this:

def add_light(self, light):
    self.lights.append(light)
    spread=np.array(light.distribution)
    self.ax1.fill(spread[:,0], spread[:,1], colour=self.colour, alpha=0.5)

to this:

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)

would solve your issue.

huangapple
  • 本文由 发表于 2023年6月25日 18:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定