|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
烦请大神解答一下,到底是哪里出了问题!谢谢!谢谢!
|
|