python编程入门到实_《python编程从入门到实践》读书笔记与代码实现(二)——数据可视化(3)...

笔记与代码实现

第十七章

# -*- coding: gbk -*-

#自动从网上采集数据并对其进行可视化

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)

"""

响应对象包含一个名为status_code的属性

它让我们知道请求是否成功了(状态码200表示请求成功)

"""

#将API响应存储在一个变量中

response_dict = r.json()

#处理结果

print(response_dict.keys())

print("Total repositories:", response_dict['total_count'])

# 探索有关仓库的信息

repo_dicts = response_dict['items']

#print("Repositories returned:", len(repo_dicts))

# 研究第一个仓库

#repo_dict = repo_dicts[0]

#print("\nKeys:", len(repo_dict))

#研究都有哪些键

#for key in sorted(repo_dict.keys()):

#print(key)

#研究与键相关联的值

#print("\nSelected information about first repository:")

#print('Name:', repo_dict['name'])

#print('Owner:', repo_dict['owner']['login'])

#print('Stars:', repo_dict['stargazers_count'])

#print('Repository:', repo_dict['html_url'])

#print('Created:', repo_dict['created_at'])

#print('Updated:', repo_dict['updated_at'])

#print('Description:', repo_dict['description'])

#涵盖多个仓库

#print("\nSelected information about each repository:")#遍历repo_dicts中的所有字典

#for repo_dict in repo_dicts:

#print('\nName:', repo_dict['name'])

#print('Owner:', repo_dict['owner']['login'])

#print('Stars:', repo_dict['stargazers_count'])

#print('Repository:', repo_dict['html_url'])

#print('Description:', repo_dict['description'])

names, stars = [], []

for repo_dict in repo_dicts:

names.append(repo_dict['name'])

stars.append(repo_dict['stargazers_count'])

#可视化

#定义了一种样式,并将其基色设置为深蓝色

#传递了实参base_style,以使用LightColorizedStyle类

my_style = LS('#333366', base_style=LCS)

#Bar()创建一个简单的条形图,传入上面定义的样式,将x轴旋转45度,隐藏图例

#chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)

#改进Pygal图表

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#副标签是x轴上的项目名以及y轴上的大部分数字

my_config.major_label_font_size = 18#主标签是y轴上为5000整数倍的刻度,应该更大以与副标签相区分

my_config.truncate_label = 15#将较长的项目名缩短为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('', stars)

chart.render_to_file('python_repos.svg')

import pygal

from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

#添加工具提示(将鼠标指向条形将显示它表示的信息)

my_style = LS('#333366', base_style=LCS)

chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)

chart.title = 'Python Projects'

chart.x_labels = ['httpie', 'django', 'flask']

#Pygal根据与键'value'相关联的数字来确定条形的高度,并使用与'label'相关联的字符串给条形创建工具提示

plot_dicts = [

{'value': 16101, 'label': 'Description of httpie.'},

{'value': 15028, 'label': 'Description of django.'},

{'value': 14798, 'label': 'Description of flask.'},

]

#方法add()接受一个字符串和一个列表

chart.add('', plot_dicts)

chart.render_to_file('bar_descriptions.svg')

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)

#将API响应存储在一个变量中

response_dict = r.json()

#探索有关仓库的信息

repo_dicts = response_dict['items']

print("Number of items:", len(repo_dicts))

names, plot_dicts = [], []

for repo_dict in repo_dicts:

names.append(repo_dict['name'])

plot_dict = {

'value': repo_dict['stargazers_count'],

'label': str(repo_dict['description']),#针对114报错

#在图表中添加可单击的链接

#Pygal根据与键'xlink'相关联的URL将每个条形都转换为活跃的链接

'xlink': repo_dict['html_url'],

}

plot_dicts.append(plot_dict)

#可视化

#定义了一种样式,并将其基色设置为深蓝色

#传递了实参base_style,以使用LightColorizedStyle类

my_style = LS('#333366', base_style=LCS)

#Bar()创建一个简单的条形图,传入上面定义的样式,将x轴旋转45度,隐藏图例

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('', plot_dicts)

chart.render_to_file('python_repos_30.svg')

#113报错AttributeError: 'NoneType' object has no attribute 'decode'

点赞

收藏

分享

文章举报

28fa23ffec13c8ae844f71b48e4db184.gif

de638df49f1d5a3342eecff86bcb011e.png

Seal_Wings

发布了44 篇原创文章 · 获赞 0 · 访问量 1100

私信

关注

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值