python编程从入门到实践第十六章制作交易收盘价走势图

python编程从入门到实践中第十六章中的制作交易收盘价走势图的内容

为什么在下图中没有
wd = [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
时的两个图结果和有这几段代码时的结果差别很大,这段代码的意思是什么?下面的两个数据图分别为响应结果。
十分困惑希望能得到解答,非常感谢

python编程从入门到实践第十六章制作交易收盘价走势图_第1张图片
python编程从入门到实践第十六章制作交易收盘价走势图_第2张图片
python编程从入门到实践第十六章制作交易收盘价走势图_第3张图片
python编程从入门到实践第十六章制作交易收盘价走势图_第4张图片

这里是源代码:

import json
#将数据加载到一个文件中
filename = “btc_close_2017.json”
with open(filename) as f:
btc_data = json.load(f)
dates = []
months = []
weeks = []
weekdays = []
close = []

#打印每一天的信息
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”])))

import pygal
import math

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(“log收盘价”, close_log)
line_chart.render_to_file(“收盘价对数变换折线图 (¥) .svg”)

#收盘价均值
from itertools import groupby

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

new_x_unique=[]
for x in x_unique:
new_x_unique.append(str(x))

line_chart.x_labels = new_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”)
line_chart_month = draw_line(months[:idx_month], close[:idx_month], "收盘价月日均值 (¥) ",
“月日均值”)
line_chart_month

#调用收盘价日期星期
idx_week = dates.index(“2017-12-11”)
line_chart_month = draw_line(weeks[1:idx_week], close[1:idx_week], "收盘价周日均值 (¥) ",
“周日均值”)
line_chart_month

#调用收盘价星期均值
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], "收盘价星期均值 (¥) ",
“星期均值”)
line_chart_weekday.x_labels = [“周一”,“周二”,“周三”,“周四”,“周五”,“周六”,“周日”]
line_chart_weekday.render_to_file(“收盘价星期均值 1(¥) .svg”)

你可能感兴趣的:(python编程从入门到实践第十六章制作交易收盘价走势图)