《Python编程,从入门到实践》第16章
import requests
import pygal
import math
# 执行API调用并存储相应
url = 'https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
r = requests.get(url) # 将响应对象存储到变量r中
with open('btc_close_2017.json','w') as f:
f.write(r.text)
file_requests = r.json() # 将响应对象中的信息转化为python标准数据类型,这里是list
dates = []
months = []
weeks = []
weekdays = []
close = []
# 每一天的信息
for dic in file_requests:
dates.append(dic['date'])
months.append(int(dic['month']))
weeks.append(int(dic['week']))
weekdays.append(dic['weekday'])
close.append(int(float(dic['close'])))
'''
line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
line_chart.title = "收盘价 ($)"
line_chart.x_labels = dates
N = 20 # x轴每隔20天显示一次
line_chart.x_labels_major = dates[::N]
line_chart.add('收盘价',close)
line_chart.render_to_file('收盘价折线图.svg')
'''
line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
line_chart.title = "收盘价对数变换 ($)"
line_chart.x_labels = dates
N = 20 # x轴每隔20天显示一次
line_chart.x_labels_major = dates[::N]
close_log = [math.log10(_) for _ in close]
line_chart.add('收盘价',close_log)
line_chart.render_to_file('收盘价对数变换折线图.svg')
网友评论