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=star'
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']
# print("Rsepositories returned:",len(repo_dicts))
names, stars = [], []
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
stars.append(repo_dict['stargazers_count'])
# 可視化
my_style = LS('333366', 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")
報錯如下,求大神解答:
C:\Users\mi\AppData\Local\Programs\Python\Python38-32\python.exe C:/python_work/data_download/python_repos.py
Status code: 200
Total repositories: 5893154
Traceback (most recent call last):
File "C:/python_work/data_download/python_repos.py", line 31, in <module>
chart.render_to_file("python_repos.svg")
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\graph\base.py", line 218, in setup
self.svg.pre_render()
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\svg.py", line 428, in pre_render
self.add_styles()
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\svg.py", line 92, in add_styles
colors = self.graph.style.get_colors(self.id, self.graph._order)
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\style.py", line 162, in get_colors
value_colors.append('white' if is_foreground_light(
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\colors.py", line 158, in is_foreground_light
return rgb_to_hsl(*parse_color(color)[:3])[2] < 17.9
File "C:\Users\mi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygal\colors.py", line 96, in parse_color
if color.startswith('#'):
AttributeError: 'NoneType' object has no attribute 'startswith'
Process finished with exit code 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/60564.html
