马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想把一个含有股价信息json文件做成可视化图表,然后在创建图表时报错了。
我的代码:from itertools import groupby
import json
import pygal
import math
#put data into a list
filename = 'btc_close_2017.json'
with open(filename) as f:
btc_data = json.load(f)
#create 5 list to instore date and close price
dates = []
months = []
weeks = []
weekdays = []
close = []
#print everyday's message
for btc_dict in btc_data:
dates.append(btc_dict['date'])
months.append(int(btc_dict['month']))
weeks.append(int(btc_dict['week']))
weekdays.append(btc_dict['weekday'])
close.append(int(float(btc_dict['close'])))
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_label = x_unique
line_chart.add(y_legend,y_mean)
line_chart.render_to_file(title+'.svg')
return line_chart
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,close[1:idx_week],'a','b')
line_chart_weekday.x_labels = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
line_chart_weekday.render_to_file('close price of week average.svg')
发生了语法错误,实在看不出来哪里错了: File "btc_close_price_week_average.py", line 43
line_chart_weekday = draw_line(weekdays_int,close[1:idx_week],'a','b')
^
SyntaxError: invalid syntax
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]
最后面少了个]吧?
就是报错的上面一行
这个库绘图,和matplotlib比,特点在哪?
|