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:html
鄭重提醒:代碼千萬別拼寫錯了,x_label_rotation=20拼寫成x_label_ratation=20都把個人頭搞大了。json
下面作收盤價均值(包括月日均值、週日均值以及星期均值):
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/1190000015098563segmentfault
Figure: