我正在尝试在下图的x轴上获取日期时间数据。当前,时间数据来自以下列表:Unix格式:[1586088239188, 1586091716319, 1586095462692, ...]
这是我要输出到Django模板的Django视图中的代码。我想将每个列表日期元素转换为人类可读的日期格式。我能够接近:“ 2020-04-12 03:01:47.708000
”,但是它不能在x轴上显示。
这是Django def视图代码,其图表输出如下:
def chart(request):
chart_endpoint = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=7"
request_chart_data = requests.get(chart_endpoint)
results_chart_data = request_chart_data.json()
chart_data = results_chart_data['prices']
#print(chart_data)
price = [item[1] for item in chart_data]
price_date = [item[0] for item in chart_data]
p = figure(title='Line Plot Price and Date', x_axis_label='x', y_axis_label='y')
p.line(price_date, price)
script, div = components(p)
return render(request, 'crypto/chart.html', {'script': script, 'div': div})
这是我尝试的日期转换以及下面该结果的图表输出。如您所见,所有日期都是1.587e + 12等...
def chart(request):
chart_endpoint = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=7"
request_chart_data = requests.get(chart_endpoint)
results_chart_data = request_chart_data.json()
chart_data = results_chart_data['prices']
#print(chart_data)
price = [item[1] for item in chart_data]
price_date = [item[0] for item in chart_data]
for num in price_date:
price_date_formatted = datetime.datetime.fromtimestamp(num / 1e3)
print(price_date_formatted)
p = figure(title='Line Plot example', x_axis_label='x', y_axis_label='y')
p.line(price_date_formatted, price)
script, div = components(p)
return render(request, 'crypto/chart.html', {'script': script, 'div': div})