奇怪的时间序列图,在 x 轴添加日期时。

huangapple go评论66阅读模式
英文:

weird time series plots when adding the dates on the x axis

问题

I'm just providing the translation as requested:

我正在尝试使用Plotly在Python上绘制时间序列图表,以下是没有x轴上的日期的结果:

并且当我添加日期时:

这是我的表格视图:

这是绘图的代码:

似乎图表没有考虑到系列的所有观察结果。我的日期特征是以日期时间格式存在的,使用Matplotlib时也存在相同的问题。

您有什么建议吗?

英文:

I'm trying to plot a time series on python using plotly, here is the result without the dates on the x-axis :

奇怪的时间序列图,在 x 轴添加日期时。

And when I add the date :

奇怪的时间序列图,在 x 轴添加日期时。

Here is a view of my table :

{'sasdate': {4: Timestamp('1959-01-09 00:00:00'),
  5: Timestamp('1959-01-12 00:00:00'),
  6: Timestamp('1960-01-03 00:00:00'),
  7: Timestamp('1960-01-06 00:00:00'),
  8: Timestamp('1960-01-09 00:00:00'),
  9: Timestamp('1960-01-12 00:00:00'),
  10: Timestamp('1961-01-03 00:00:00'),
  11: Timestamp('1961-01-06 00:00:00'),
  12: Timestamp('1961-01-09 00:00:00'),
  13: Timestamp('1961-01-12 00:00:00')},
 'CDT': {4: 0.9633000000000003,
  5: 0.35329999999999995,
  6: 0.6134,
  7: 1.2666999999999997,
  8: 1.4733,
  9: 1.5799999999999996,
  10: 1.4367,
  11: 1.4867,
  12: 1.6766999999999999,
  13: 1.5133},
 'crise': {4: 0, 5: 0, 6: 0, 7: 1, 8: 1, 9: 1, 10: 1, 11: 0, 12: 0, 13: 0},
 'prev': {4: 0.187232436694447,
  5: 0.3105689948109355,
  6: 0.2539228791308063,
  7: 0.13916924600664243,
  8: 0.11171277377207611,
  9: 0.09915690331613247,
  10: 0.11627115062260285,
  11: 0.1100762567460945,
  12: 0.08869839928964451,
  13: 0.10687875779138178}}

And here is the code of the plot :

import plotly.express as px

fig = px.line(test,x='sasdate', y="prev", labels = 'ligne')


fig.update_layout(legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.01
))

fig.update_layout(showlegend =True)

fig.show()

It seems the plots doesn't consider all the observations of the series. My 'date' features is on a datetime format, the problem is the same with matplotlib.

Do you have suggestions ?

答案1

得分: 1

Your request has been noted. Here's the translated code portion:

你的日期格式不正确它们需要是 YYYY-MM-DD而不是 YYYY-DD-MM例如'1959-01-09 00:00:00' 转换为 '1959-09-01 00:00:00'以此类推然后图表将会如预期般显示

如果你不想手动进行转换你可以使用 `datetime`并告诉它你所拥有的日期/时间的格式像这样

```python
from datetime import datetime

test["sasdate"] = {
    i: Timestamp(datetime.strptime(str(test["sasdate"][i]), "%Y-%d-%m %H:%M:%S"))
    for i in test["sasdate"]
}

如果 test 已经是一个 DataFrame,那么你可以这样做:

from pandas import Series

test["sasdate"] = Series(
    [
        Timestamp(datetime.strptime(str(date), "%Y-%d-%m %H:%M:%S")) for date in test["sasdate"]
    ],
    index=test["sasdate"].index
)

请注意,已翻译的代码部分已在上面提供。

<details>
<summary>英文:</summary>

You dates are in the wrong format. They need to be YYYY-MM-DD rather than YYYY-DD-MM, e.g., swap &#39;1959-01-09 00:00:00&#39; to be &#39;1959-09-01 00:00:00&#39; as so on, and the plot will look as expected.

If you don&#39;t want to do the conversion manually, you could use the `datetime` package and tell it the format of the date/time that you have, by doing something like:

```python
from datetime import datetime

test[&quot;sasdate&quot;] = {
    i: Timestamp(datetime.strptime(str(test[&quot;sasdate&quot;][i]), &quot;%Y-%d-%m %H:%M:%S&quot;))
    for i in test[&quot;sasdate&quot;]
}

If test is already a DataFrame, then you could do:

from pandas import Series

test[&quot;sasdate&quot;] = Series(
    [
        Timestamp(datetime.strptime(str(date), &quot;%Y-%d-%m %H:%M:%S&quot;)) for date in test[&quot;sasdate&quot;]
    ],
    index=test[&quot;sasdate&quot;].index
)

答案2

得分: 0

正如@MattPitkin所说,这是我的日期时间格式的问题。我用以下转换进行了更改:

df["date"] = pd.to_datetime(df["date"], format="%m/%d/%Y")
df["date"] = df["date"].apply(lambda x: x.strftime("%Y-%m-%d"))

谢谢你的帮助!

英文:

As @MattPitkin said, it was a problem with my date time format. I changed it with these transformations :

df[&quot;date&quot;] = pd.to_datetime(df[&quot;date&quot;], format=&quot;%m/%d/%Y&quot;)
df[&quot;date&quot;]  = df[&quot;date&quot;].apply(lambda x : x.strftime(&quot;%Y-%m-%d&quot;)) 

Thanks for the help !

huangapple
  • 本文由 发表于 2023年5月14日 23:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定