Python编程:从入门到实践16.2.6——x轴坐标轴没按数据大小排序(问题解决)

代码如下:

import json
import pygal
import math
# 将数据加载到一个列表
filename = 'btc_close_2017.json'
with open(filename) as f:
	btc_data = json.load(f)
# 创建5哥列表, 分别存储日期和收盘价
dates = []
months = []
weeks = []
weekdays = []
close = []
# 每一天的信息
for btc_dict in btc_data:
	dates.append(btc_dict['date'])
	months.append(btc_dict['month'])
	weeks.append(btc_dict['week'])
	weekdays.append(btc_dict['weekday'])
	close.append(int(float(btc_dict['close'])))
# 绘制图表
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(x) for x in close]
line_chart.add('收盘价', close_log)
line_chart.render_to_file('收盘价对数变换折线图(¥).svg')

from itertoo

你可能感兴趣的:(python,开发语言,后端)