可以幫我看下這個代碼為什么會報錯,錯誤是這一行“line_chart.x_labels=x_unique”,錯誤型別是attributeerror:'str' object has no attribute 'x_labels'。
import json
import math
import pygal
#將資料加載到一個串列中
filename='btc_close_2018.json'
with open(filename) as f:
btc_data=https://bbs.csdn.net/topics/json.load(f)
#創建5個串列,分別存盤日期和收盤價
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'])))
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
line_chart.x_labels=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
uj5u.com熱心網友回復:
在這行代碼中: line_chart.x_labels = x_unique你的 x_unique 是整形元組
把整形元組轉化為String型串列
再賦值給 line_chart.x_labels 就沒問題了~
line_chart.x_labels = str(x_unique)
uj5u.com熱心網友回復:
AttributeError: 'str' object has no attribute 'astype'轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/34972.html
上一篇:柱狀圖的坐標軸不顯示
