API项目——数据绘图

# coding: utf-8
"""根据数据自动生成plot_dicts"""
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) # 状态码status_code,看是否请求成功

# -----------------将API响应存储在一个变量中-----------------------------------
response_dict = r.json()
print(response_dict.keys())
print("total respositories:", response_dict['total_count'])
print("是否成功", response_dict['incomplete_results']) # False代表成功
print('\n')

# ----------------看看每个仓库的都有哪些内容-------------------------------------
respo_dict = response_dict['items']
respo_dict_0 = respo_dict[0] # 第一个仓库
for key in respo_dict_0.keys():
    print(key)

# -----------------可视化---------------------------------------------
names = [] # 用来生成横轴
plot_dicts = []

for dict in respo_dict:
    names.append(dict['name'])

    plot_dict = {'value': dict['stargazers_count'],
                 'label': str(dict['description'])}
    plot_dicts.append(plot_dict)


"""改进可视化,配置更优质的图表
通过修改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 # 副标签的字体大小(x轴上的项目名,y轴上的大部分数字)
my_config.major_label_front_size = 18 # 主标签的字体大小(主标签是y轴上为5000整数倍的刻度)
my_config.truncate_label = 15 # 将较长的项目名缩短为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('', plot_dicts)
chart.render_to_file('python_repos_3.svg')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值