已安装pygal。但用python生成svg文件时报错,请问如何解决
报错代码如下:Traceback (most recent call last):
File "die_visual.py", line 22, in <module>
hist.render_to_file('die_visual.svg')
File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\base.py", line 217, in setup
self._draw()
File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\graph.py", line 930, in _draw
self._compute_margin()
File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\graph.py", line 706, in _compute_margin
self._x_title = split_title(
File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\util.py", line 329, in split_title
title_lines = title.split("\n")
AttributeError: 'list' object has no attribute 'split' 本帖最后由 hrp 于 2020-10-25 17:11 编辑
AttributeError: 'list' object has no attribute 'split':
从报错猜测 title 变量是一个列表,而列表没有split方法,所以报错了。
建议发代码,方便鱼油帮你解决问题。 代码如下:
这是die.py
from random import randint
class Die():
"""a class of die"""
def __init__(self,num_sides=6):
"""default 6 sides"""
self.num_sides = num_sides
def roll(self):
"""return a random number between 1 and die's sides"""
return randint(1,self.num_sides)
这是die_visual.py
from die import Die
import pygal
#create a D6
die = Die()
#roll die a few times and restore result in a list
results =
#analsis result
frequencies =
#visualize result
hist = pygal.Bar()
hist.title = "Results of rolling one D6 6000 times."
hist.x_labels =
hist.x_title = ["Result"]
hist.y_title = "Frenquency of Result"
hist.add('D6',frequencies)
hist.render_to_file("die_visual.svg") hrp 发表于 2020-10-25 16:34
AttributeError: 'list' object has no attribute 'split':
从报错猜测你的 title 变量是一个列表,你对 ...
完全按照《Python编程》书的来,两个掷骰子的py文件都是最后一行有问题。pygal是可以用的,我在网上抄了简单的代码,可以用pygal生成一些简单的图表 张图南 发表于 2020-10-25 17:03
完全按照《Python编程》书的来,两个掷骰子的py文件都是最后一行有问题。pygal是可以用的,我在网上抄了 ...
die_visual.py 第 18 行,把方括号去掉试试 hrp 发表于 2020-10-25 17:10
die_visual.py 第 18 行,把方括号去掉试试
可以了,感谢大佬。若不嫌弃,可否留下支付宝账号,我请您吃顿早饭。 张图南 发表于 2020-10-25 17:48
可以了,感谢大佬。若不嫌弃,可否留下支付宝账号,我请您吃顿早饭。
回答问题也就动动手而已,早饭就不用了{:5_109:}
页:
[1]