简体   繁体   English

如何在 plotly 中获取 plot 时间序列数据?

[英]How to plot time series data in plotly?

When I plot my data with just the index the graph looks fine.当我 plot 我的数据只有索引时,图表看起来很好。 But when I try to plot it with a datetime object in the x axis, the plot gets messed up.但是,当我尝试在 x 轴上使用日期时间 object 对它进行 plot 时,plot 会变得一团糟。 Does anyone know why?有谁知道为什么? I provided the head of my data and also the two plots.我提供了我的数据的头部以及两个图。

在此处输入图像描述

import plotly.express as px
fig = px.line(y=data.iloc[:,3])
fig.show()

在此处输入图像描述

fig = px.line(y=data.iloc[:,3],x=data.iloc[:,0])
fig.show()

在此处输入图像描述

It is probably because of missing dates as you have around 180 data points but your second plot shows data spans from 2014 to 2019 that means it does not have many data points in between that's why your second graph looks like that.这可能是因为缺少日期,因为您有大约 180 个数据点,但您的第二个 plot 显示的数据跨度从 2014 年到 2019 年,这意味着它之间没有很多数据点,这就是为什么您的第二个图表看起来像这样。

Instead of datetime try plotting converting it into string but then it will not be a time series as you will have many missing dates而不是 datetime 尝试将其转换为字符串,但它不会是一个时间序列,因为您将有很多丢失的日期

Here I have two solutions:在这里,我有两个解决方案:

  1. Use reset_index() function to get rid off the missing dates but it just explains the chart but x-axis values don't provide date information anymore.使用 reset_index() function 摆脱丢失的日期,但它只是解释图表,但 x 轴值不再提供日期信息。 Heres an example: This is the data frame and I want to plot the chart between time and closing price这是一个例子:这是数据框,我想要 plot 时间和收盘价之间的图表

在此处输入图像描述

import plotly.graph_objects as go
fig = go.Figure([go.Scatter(x=df.reset_index().index, y=df['close'])])
fig.show()

在此处输入图像描述

1277 is the index value and corresponding value is the closing price. 1277为指数值,对应值为收盘价。 Use.iloc() to find the x-axis value使用.iloc() 查找x轴值在此处输入图像描述

  1. Convert x-axis value to datetime object Follow this link: https://stackoverflow.com/a/51231209/10277042将 x 轴值转换为日期时间 object 遵循此链接: https://stackoverflow.com/a/51231209/10277042

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM