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=star'
r = requests.get(URL)
print("Status code:", r.status_code)
response_dict = r.json()
print(response_dict.keys())
print("total respositories:", response_dict['total_count'])
print("是否成功", response_dict['incomplete_results'])
print('\n')
respo_dict = response_dict['items']
respo_dict_0 = respo_dict[0]
for key in respo_dict_0.keys():
print(key)
names = []
stars = []
for dict in respo_dict:
names.append(dict['name'])
stars.append(dict['stargazers_count'])
"""可视化"""
mt_style = LS('#333366', base_style=LCS)
chart = pygal.Bar(style=mt_style, x_label_rotation=45, show_lengend=False)
chart.title = 'Most-Starred Python Projects on Github'
chart.x_labels = names
chart.add('', stars)
chart.render_to_file('python_repos.svg')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2cbd1e5ae6e049e73ab49f6fe56c7ec7.jpeg)
改进的图表,可自定义图表参数外观
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=star'
r = requests.get(URL)
print("Status code:", r.status_code)
response_dict = r.json()
print(response_dict.keys())
print("total respositories:", response_dict['total_count'])
print("是否成功", response_dict['incomplete_results'])
print('\n')
respo_dict = response_dict['items']
respo_dict_0 = respo_dict[0]
for key in respo_dict_0.keys():
print(key)
names = []
stars = []
for dict in respo_dict:
names.append(dict['name'])
stars.append(dict['stargazers_count'])
"""改进可视化,配置更优质的图表
通过修改my_config,可以定制图表的外观"""
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_front_size = 24
my_config.label_front_size = 14
my_config.major_label_front_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = True
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('', stars)
chart.render_to_file('python_repos_2.svg')