想把数据可视化出现报错了,请问有会的吗?
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
url=' https://api.github.com/search/repositories?q=language:python&sort=stars' https://api.github.com/search/repositories?q=language:python&sort=stars%27
r = requests.get(url)
print("Status code:",r.status_code)
response_dict=r.json()
print("Total repositors:",response_dict['total_count'])
repo_dicts=response_dict['items']
names,stars=[],[]
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
stars.append(['stargazers_count'])
#可视化
my_style=LS('#333360',base_style=LCS)
chart=pygal.Bar(style=my_style,x_label_rotation=45,show_legend=False)
chart.title='Most-Starred Python Projects on GitHub'
chart.x_labels=names
chart.add('',stars)
chart.render_to_file('python_repos.svg')
E:\pythonProject\Scripts\python.exe E:/PycharmProjects/pythonProject/web/python_repos.py
Status code: 200
Total repositors: 7710879
Traceback (most recent call last):
File "E:\PycharmProjects\pythonProject\web\python_repos.py", line 26, in
chart.render_to_file('python_repos.svg')
File "E:\pythonProject\lib\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "E:\pythonProject\lib\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "E:\pythonProject\lib\site-packages\pygal\graph\base.py", line 217, in setup
self._draw()
File "E:\pythonProject\lib\site-packages\pygal\graph\graph.py", line 923, in _draw
self._compute()
File "E:\pythonProject\lib\site-packages\pygal\graph\bar.py", line 132, in _compute
self._box.ymin = min(self._min, self.zero)
TypeError: '<' not supported between instances of 'int' and 'list'
进程已结束,退出代码为 1
stars.append(['stargazers_count'])
这一行似乎有点问题?改成这样:
stars.append(repo_dict['stargazers_count'])