如何解决AttributeError:'Polygon'对象无'colour'属性错误?
修复Matplotlib中
'Polygon' object has no property 'colour'错误 问题描述
我正在使用Python模拟音乐舞台,尝试为舞台添加灯光时持续出现错误:
AttributeError: 'Polygon' object has no property 'colour'。我已调整Light类但仍报错,以下是我的代码及完整报错回溯信息:
原代码
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()
报错回溯信息
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'
错误分析与修复方案
核心错误原因
Matplotlib的fill()方法只支持美式拼写的color参数,不识别英式拼写的colour,这是直接触发报错的原因。
额外语法/逻辑问题修正
- 类方法缩进错误:
Light类的calculate_cone方法未缩进,不属于类成员;Stage类的add_circle、add_light等方法缩进错误,未成为类的一部分。 - 重复方法定义:
Stage类中重复定义了add_circle方法,且未给圆形设置颜色。 - 参数语法错误:创建
Light实例时,position参数缺少等号,应为position=[250,490]。 - 循环变量错误:
simulate_lights方法中循环变量名与迭代对象冲突,for lights in self.lights应改为for light in self.lights。
完整修正代码
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.direction) half_cone_angle = spread_angle / 2 start_angle = self.direction - half_cone_angle end_angle = self.direction + half_cone_angle return start_angle, end_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 = figsize) 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, color=colour) self.ax0.add_patch(circle) def add_light(self, light): self.lights.append(light) spread = np.array(light.distribution) # 将colour改为color 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") plt.show() 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') # 修正position参数的语法错误 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()
内容的提问来源于stack exchange,提问作者Sinibini
相关产品推荐
相关产品推荐

