import json
import pygal
import math
from itertools import groupby
# 提取json数据
filename = 'btc_close_2017.json'
with open(filename) as f:
btc_data = json.load(f)
dates = []
months = []
weeks = []
weekdays = []
closes = []
for btc_dict in btc_data:
try:
date = btc_dict['date']
month = int(btc_dict['month'])
week = int(btc_dict['week'])
weekday = btc_dict['weekday']
close = int(float(btc_dict['close'])) # python不能把小数字符串直接转成int
except ValueError as e:
print(e)
else:
dates.append(date)
months.append(month)
weeks.append(week)
weekdays.append(weekday)
closes.append(close)
# print("{} is month {} week {} , {} , the close price is {} RMB".format(date,month,week,weekday,close))
# 使用pygal画折线图
line_chart = pygal.Line(x_label_rotation=20,
show_minor_x_labels=False) # x_label_rotation让X轴上的数据顺时针旋转20°,show_minor_x_labels不用显示所有的X轴标签
line_chart.title = '收盘价'
line_chart.x_labels = dates
N = 20
line_chart.x_labels_major = dates[::N] # X轴坐标每隔20天显示一次
# line_chart.add('收盘价', closes)
# line_chart.render_to_file('收盘价折线图.svg')
# 时间序列特征(使用math函数的对数)
close_log = [math.log10(x) for x in closes]
line_chart.add('log收盘价', close_log)
line_chart.render_to_file('收盘价对数变换折线图(¥).svg')
def draw_line(x_data, y_data, title, y_legend):
xy_map = []
for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _: _[0]):
y_list = [v for _, v in y]
xy_map.append([x, sum(y_list) / len(y_list)])
x_unique, y_mean = [*zip(*xy_map)]
line_chart = pygal.Line()
line_chart.title = title
line_chart.x_labels = x_unique
line_chart.add(y_legend, y_mean)
line_chart.render_to_file(title + '.svg')
return line_chart
idx_month = dates.index('2017-12-01')
draw_line(months[:idx_month], closes[:idx_month], '收盘价月日均值(¥)', '月日均值')
idx_week = dates.index('2017-12-11')
draw_line(weeks[1:idx_week], closes[1:idx_week], '收盘价周日均值(¥)', '周日均值')
idx_week = dates.index('2017-12-11')
wd = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
line_chart_weekday = draw_line(weekdays_int, closes[1:idx_week], '收盘价星期均值(¥)', '星期均值')
line_chart_weekday.x_labels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
line_chart_weekday.render_to_file('收盘价星期均值(¥).svg')
with open('收盘价Dashboard.html', 'w', encoding='utf8') as html_file:
html_file.write('<html><head><title>收盘价</title><meta charset="utf-8"></head><body>\n')
for svg in ['die_visual.svg', 'die_visuals.svg', '收盘价周日均值(¥).svg', '收盘价对数变换折线图(¥).svg', '收盘价折线图.svg',
'收盘价星期均值(¥).svg', '收盘价月日均值(¥).svg']:
html_file.write('<object type="image/svg+xml" data="{0}" height=500></object>\n'.format(svg))
html_file.write('</body></html>')
第六课--json数据的使用和分析
原创
©著作权归作者所有:来自51CTO博客作者壹小小俊的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:ASCII码特例
下一篇:php格式化数字为“0.00万”
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
MCSE第六课-DHCP
MCSE第六课-DHCP
职场 DHCP MCSE 休闲 -
第六课 系统的安装
第六课,系统的安装,下载地址如果不行可以联系我。。http://byjth.blog.51cto.com
安装 系统安装 win2003 虚拟机 操作系统 -
第六课--继承prototype
创建好的对象,我们是不能随便添加方法或者属性的,但是有时候是迫不得已的修改他
继承prototype html -
第六课--python的装饰器
装饰器是对闭包函数的操作。也是python中独有的。创建了一个装饰器之后,只需在
python的装饰器 python -
第六课 抽象工厂模式
第六课 抽象工厂模式 抽象工厂模式其实和工厂方法模式思路一样,可以说是工厂方法模式的一个特例,用于控制一个系列的产品的不同版本。简单举个例子(可能不适用于.net)我现在有几个界面UI控件产品,Button,Label,TextBox。
sqlserver access 产品 textbox windows