英文:
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 :
And when I add the date :
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 '1959-01-09 00:00:00' to be '1959-09-01 00:00:00' as so on, and the plot will look as expected.
If you don'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["sasdate"] = {
i: Timestamp(datetime.strptime(str(test["sasdate"][i]), "%Y-%d-%m %H:%M:%S"))
for i in test["sasdate"]
}
If test
is already a DataFrame, then you could do:
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
)
答案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["date"] = pd.to_datetime(df["date"], format="%m/%d/%Y")
df["date"] = df["date"].apply(lambda x : x.strftime("%Y-%m-%d"))
Thanks for the help !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论