标签:oat nbsp utf-8 day ict width encoding inf htm
1 import json 2 import pygal 3 4 #将json数据添加到一个列表中 5 filename = ‘btc_close_2017.json‘ 6 with open(filename) as f: 7 btc_data = json.load(f) 8 9 #创建5个列表,分别存储日期和收盘价 10 dates, months, weeks, weekdays, close = [], [], [], [], [] 11 #将字符串转化为数字值 12 for btc_dict in btc_data: 13 # date = datetime.strptime(btc_dict[‘date‘], ‘%Y-%m-%d‘) 14 # dates.append(date) 15 dates.append(btc_dict[‘date‘]) 16 months.append(int(btc_dict[‘month‘])) 17 weeks.append(int(btc_dict[‘week‘])) 18 weekdays.append(btc_dict[‘weekday‘]) 19 close.append(int(float(btc_dict[‘close‘]))) 20 #创建折线图实例,X轴标签顺时针旋转20度,不用显示所有X轴标签 21 line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False) 22 #标题 23 line_chart.title = ‘收盘价‘ 24 #X轴数据 25 line_chart.x_labels = dates 26 #X轴坐标隔20天显示一次 27 N = 20 28 line_chart.x_labels_major = dates[::N] 29 #添加Y轴信息 30 line_chart.add(‘收盘价‘, close) 31 line_chart.render_to_file(‘aaa收盘价折线图.svg‘)
Figure:
郑重提醒:代码千万别拼写错了,x_label_rotation=20拼写成x_label_ratation=20都把我的头搞大了。
下面做收盘价均值(包括月日均值、周日均值以及星期均值):
1 import json 2 import pygal 3 import math 4 from itertools import groupby 5 6 #将json数据添加到一个列表中 7 filename = ‘btc_close_2017.json‘ 8 with open(filename) as f: 9 btc_data = json.load(f) 10 11 #创建5个列表,分别存储日期和收盘价 12 dates, months, weeks, weekdays, close = [], [], [], [], [] 13 #将字符串转化为数字值 14 for btc_dict in btc_data: 15 dates.append(btc_dict[‘date‘]) 16 months.append(int(btc_dict[‘month‘])) 17 weeks.append(int(btc_dict[‘week‘])) 18 weekdays.append(btc_dict[‘weekday‘]) 19 close.append(int(float(btc_dict[‘close‘]))) 20 #创建折线图实例,X轴标签顺时针旋转20度,不用显示所有X轴标签 21 line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False) 22 line_chart_0 = pygal.Line(x_label_rotation=20, show_minor_x_labels=False) 23 #标题 24 line_chart.title = ‘收盘价‘ 25 line_chart_0.title = ‘收盘价‘ 26 #X轴数据 27 line_chart.x_labels = dates 28 line_chart_0.x_labels = dates 29 #X轴坐标隔20天显示一次 30 N = 20 31 line_chart.x_labels_major = dates[::N] 32 line_chart_0.x_labels_major = dates[::N] 33 #添加Y轴信息, 34 close_log = [math.log10(_) for _ in close] 35 line_chart.add(‘收盘价‘, close_log) 36 line_chart_0.add(‘收盘价‘, close) 37 line_chart.render_to_file(‘收盘价对数变换折线图.svg‘) 38 line_chart_0.render_to_file(‘收盘价折线图.svg‘) 39 40 def draw_line(x_data, y_data, title, y_legend): 41 xy_map = [] 42 43 for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _: _[0]): 44 y_list = [v for _, v in y] 45 xy_map.append([x, sum(y_list) / len(y_list)]) 46 47 x_unique, y_mean = [*zip(*xy_map)] 48 line_chart = pygal.Line() 49 line_chart.title = title 50 line_chart.x_labels = x_unique 51 line_chart.add(y_legend, y_mean) 52 line_chart.render_to_file(title+‘.svg‘) 53 return line_chart 54 55 idx_month = dates.index(‘2017-12-01‘) 56 line_chart_month = draw_line(months[:idx_month], close[:idx_month], ‘收盘价月日均值‘, ‘月日均值‘) 57 line_chart_month 58 59 idx_week = dates.index(‘2017-12-11‘) 60 line_chart_week = draw_line(weeks[1:idx_week], close[1:idx_week], ‘收盘价周日均值‘, ‘周日均值‘) 61 line_chart_week 62 63 idx_week = dates.index(‘2017-12-11‘) 64 wd = [‘Monday‘, ‘Tuesday‘, ‘Wednesday‘, ‘Thursday‘, ‘Friday‘, ‘Saturday‘, ‘Sunday‘] 65 weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]] 66 line_chart_weekday = draw_line(weekdays_int, close[1:idx_week], ‘收盘价星期均值‘, ‘星期均值‘) 67 line_chart_weekday.x_labels = [‘周一‘, ‘周二‘, ‘周三‘, ‘周四‘, ‘周五‘, ‘周六‘, ‘周日‘] 68 line_chart_weekday.render_to_file(‘收盘价星期均值.svg‘) 69 70 # with open(‘收盘价Dashboard.html‘, ‘w‘, encoding=‘utf-8‘) as html_file: 71 # html_file.write(‘<html><head><title>收盘价Dashboard</title><meta‘ + 72 # ‘charset = "utf-8"></head><body>\n‘) 73 # for svg in [ 74 # ‘收盘价折线图.svg‘, ‘收盘价对数变换折线图.svg‘, ‘收盘价月日均值.svg‘, 75 # ‘收盘价周日均值.svg‘, ‘收盘价星期均值.svg‘ 76 # ]: 77 # html_file.write( 78 # ‘ <object type="image/svg+xml" data="{0}" height=500></object>\n‘.format(svg) 79 # ) 80 # html_file.write(‘</body></html>‘)
相关知识点可以参考一篇非常好的博文https://segmentfault.com/a/1190000015098563
Figure:
Python中的json文件数据可视化之制作交易收盘价折线图
标签:oat nbsp utf-8 day ict width encoding inf htm
原文地址:https://www.cnblogs.com/sugar2019/p/10730676.html