简体   繁体   English

Seaborn 线图意外行为

[英]Seaborn lineplot unexpected behaviour

I am hoping to understand why the following Seaborn lineplot behaviour occurs.我希望了解为什么会出现以下 Seaborn 线图行为。

Spikes are occurring through the time-series and additional data has been added to the left of the actual data.时间序列中出现峰值,并且附加数据已添加到实际数据的左侧。

How can I prevent this unexpected behaviour in Seaborn?如何防止 Seaborn 中出现这种意外行为?

Regular plot of data:常规 plot 数据:

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

aussie_property[['Sydney(SYDD)']].plot();

在此处输入图像描述

Seaborn plot of data: Seaborn plot 数据:

sns.lineplot(data=aussie_property, x='date', y='Sydney(SYDD)');

在此处输入图像描述

This is not a seaborn problem but a question of ambiguous datetimes.这不是 seaborn 问题,而是日期时间不明确的问题。

Convert date to a datetime object with the following code:使用以下代码将date转换为日期时间 object

aussie_property['date'] = pd.to_datetime(aussie_property['Date'], dayfirst=True)

and you get your expected plot with seaborn你得到你预期的 plot 和 seaborn

在此处输入图像描述

Generally, it is advisable to provide the format during datetime conversions, eg,通常,建议在日期时间转换期间提供格式,例如,

aussie_property['date'] = pd.to_datetime(aussie_property['Date'], format="%d/%m/%Y")

because, as we have seen here, dates like 10/12/2020 are ambiguous.因为,正如我们在这里看到的,像10/12/2020年 10 月 12 日这样的日期是模棱两可的。 Consequently, the parser first thought the data would be month/day/year and later noticed this cannot be the case, so changed to parsing your input as day/month/year, giving rise to these time-travelling spikes in your seaborn graph.因此,解析器首先认为数据是月/日/年,后来发现情况并非如此,因此改为将输入解析为日/月/年,从而在 seaborn 图中产生这些时间旅行峰值。 Why you didn't see them in the pandas plot, you ask?你问为什么你没有在 pandas plot 中看到它们? Well, this is plotted against the index, so you don't notice this conversion problem in the pandas plot.好吧,这是针对索引绘制的,因此您不会注意到 pandas plot 中的转换问题。
More information on the format codes can be found in the Python documentation .有关格式代码的更多信息,请参阅 Python 文档

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

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