鱼C论坛

 找回密码
 立即注册
查看: 2780|回复: 3

[已解决]在做数据可视化的过程中遇到了问题,请大神解答一下!

[复制链接]
发表于 2017-5-25 15:57:17 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 mangozhan 于 2017-5-25 15:58 编辑

代码如下:

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

#执行API调用并储存
url='https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print('Status code:', r.status_code)

#将API响应储存在一个变量中
response_dict = r.json()
print('Total repositories:', response_dict['total_count'])

#探索有关仓库的信息
repo_dicts = response_dict['items']

names, plot_dicts = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
   
    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': repo_dict['description'],
        'xlink': repo_dict['html_url'],
        }
    plot_dicts.append(plot_dict)
   
   
#可视化
my_style = LS('#333366', base_style=LCS)

my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 20
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000

chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most=Starred Python Projects on GitHub'
chart.x_labels = names

chart.add('', plot_dicts)
chart.render_to_file('python_repos_3.svg')

错误信息如下:

Status code: 200
Total repositories: 1709540
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-6c1f6cc72b4a> in <module>()
     45
     46 chart.add('', plot_dicts)
---> 47 chart.render_to_file('python_repos_3.svg')

e:\program files\python35-32\lib\site-packages\pygal\graph\public.py in render_to_file(self, filename, **kwargs)
    112         """Render the graph, and write it to filename"""
    113         with io.open(filename, 'w', encoding='utf-8') as f:
--> 114             f.write(self.render(is_unicode=True, **kwargs))
    115
    116     def render_to_png(self, filename=None, dpi=72, **kwargs):

e:\program files\python35-32\lib\site-packages\pygal\graph\public.py in render(self, is_unicode, **kwargs)
     50     def render(self, is_unicode=False, **kwargs):
     51         """Render the graph, and return the svg string"""
---> 52         self.setup(**kwargs)
     53         svg = self.svg.render(
     54             is_unicode=is_unicode, pretty_print=self.pretty_print)

e:\program files\python35-32\lib\site-packages\pygal\graph\base.py in setup(self, **kwargs)
    215         if self._len < 3:
    216             self.interpolate = None
--> 217         self._draw()
    218         self.svg.pre_render()
    219

e:\program files\python35-32\lib\site-packages\pygal\graph\graph.py in _draw(self)
    931         self._decorate()
    932         if self.series and self._has_data() and self._values:
--> 933             self._plot()
    934         else:
    935             self.svg.draw_no_data()

e:\program files\python35-32\lib\site-packages\pygal\graph\bar.py in _plot(self)
    144         """Draw bars for series and secondary series"""
    145         for serie in self.series:
--> 146             self.bar(serie)
    147         for serie in self.secondary_series:
    148             self.bar(serie, True)

e:\program files\python35-32\lib\site-packages\pygal\graph\bar.py in bar(self, serie, rescale)
    114                 self.svg,
    115                 self.svg.node(bars, class_='bar'),
--> 116                 metadata)
    117
    118             x_, y_, width, height = self._bar(

e:\program files\python35-32\lib\site-packages\pygal\util.py in decorate(svg, node, metadata)
    232     if 'label' in metadata:
    233         svg.node(node, 'desc', class_='label').text = to_unicode(
--> 234             metadata['label'])
    235     return node
    236

e:\program files\python35-32\lib\site-packages\pygal\_compat.py in to_unicode(string)
     60     """Force string to be a string in python 3 or a unicode in python 2"""
     61     if not isinstance(string, coerce):
---> 62         return string.decode('utf-8')
     63     return string
     64

AttributeError: 'NoneType' object has no attribute 'decode'

操作系统是windows

Python版本:3.5 32-bit

烦请大神解答一下,到底是哪里出了问题!谢谢!谢谢!
最佳答案
2017-5-25 16:13:40
怎么自己给自己设置最佳答案呢?
这帖子怎么关闭?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-5-25 16:12:39 | 显示全部楼层
我自己解决了。
原来其中有的描述项目是空值,所以取不到值,报错‘NoneType’
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-25 16:13:40 | 显示全部楼层    本楼为最佳答案   
怎么自己给自己设置最佳答案呢?
这帖子怎么关闭?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-25 18:25:58 | 显示全部楼层
可以给我最佳答案啊~这样就能关闭了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-27 17:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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