鱼C论坛

 找回密码
 立即注册
查看: 1870|回复: 1

生成svg文件时报错,希望有人知道错误原因

[复制链接]
发表于 2020-10-25 08:40:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写了一个掷骰子的代码,希望生成柱状图,但最后生成的svg文件是0kb。代码如下:
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)
…………………………………………………………
from die import Die
import pygal

#create a D6
die = Die()

#roll die a few times and restore result in a list
results = []
for roll_num in range(6000):
    result = die.roll()
    results.append(result)

#analsis result
frequencies = []
for value in range(1,die.num_sides+1):
    frequency = results.count(value)
    frequencies.append(frequency)


#visualize result
hist = pygal.Bar()

hist.title = "Results of rolling one D6 6000 times."
hist.x_labels = ['1','2','3','4','5','6']
hist.x_title = ["Result"]
hist.y_title = "Frenquency of Result"

hist.add('D6',frequencies)
hist.render_to_file('die_visual.svg')
发生了报错,报错代码如下:
Traceback (most recent call last):
  File "die_visual.py", line 29, in <module>
    hist.render_to_file('die_visual.svg')
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\ghost.py", line 165, in render_to_file
    f.write(self.render(is_unicode=True, **kwargs))
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\ghost.py", line 121, in render
    return (self
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\ghost.py", line 114, in make_instance
    self._last__inst = self.cls(
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\bar.py", line 37, in __init__
    super(Bar, self).__init__(*args, **kwargs)
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\base.py", line 74, in __init__
    self._draw()
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\base.py", line 291, in _draw
    self._compute_margin()
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\graph\base.py", line 159, in _compute_margin
    self.x_title = split_title(
  File "C:\Users\asus\AppData\Roaming\Python\Python38\site-packages\pygal\util.py", line 435, in split_title
    title_lines = title.split("\n")
AttributeError: 'list' object has no attribute 'split'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-22 19:05:43 | 显示全部楼层
没用过这个库,不过这个似乎应该去掉[]:
hist.x_title = "Result"
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-17 14:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表