因此,我通过pycoingecko库从coingecko的API中提取了公共数据,并能够在excel中绘制特定加密货币的历史价格。但是,当我尝试通过chart.add_series()函数创建折线图时,却不断收到错误消息:
xlsxwriter.exceptions.EmptyChartSeries:Chart1必须包含至少一个数据系列。参见chart.add_series()。
好像无法读取API数据...
有帮助吗?
from pycoingecko import CoinGeckoAPI
import datetime
import xlsxwriter
Workbook1 = xlsxwriter.Workbook('HNS_Price_Chart.xlsx')
Worksheet = Workbook1.add_worksheet()
Chart1 = Workbook1.add_chart({'type':'line'})
Worksheet.write('A1','Date')
Worksheet.write('B1','Price')
cg = CoinGeckoAPI()
results = cg.get_coin_market_chart_range_by_id(id='handshake',vs_currency='usd',from_timestamp=1577836800,to_timestamp=1593820800)
y = 1
for p in results['prices']:
Worksheet.write(y,0,datetime.datetime.fromtimestamp(p[0]/1000).strftime('%Y-%m-%d'))
Worksheet.write(y,1,p[1])
y += 1
Chart1.add_series({
'Categories':'=Sheet1!$A$2:$A$144',
'Values':'=Sheet1!$B$2:$B$144',
'name':"HNS Price"
})
Worksheet.insert_chart('D1',Chart1)
Workbook1.close()
答案 0 :(得分:0)
categories
和values
选项应为小写。更改为以下内容应该可以正常工作:
Chart1.add_series({
'categories': '=Sheet1!$A$2:$A$144',
'values': '=Sheet1!$B$2:$B$144',
'name': "HNS Price"
})