Seaborn 线图意外行为 [英] Seaborn lineplot unexpected behaviour

查看:66
本文介绍了Seaborn 线图意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望了解为什么会出现以下 Seaborn 线图行为.

峰值出现在整个时间序列中,并且附加数据已添加到实际数据的左侧.

如何在 Seaborn 中防止这种意外行为?

常规数据图:

将pandas导入为pd从 matplotlib 导入 pyplot 作为 plt将 seaborn 作为 sns 导入aussie_property[['Sydney(SYDD)']].plot();

Seaborn 数据图:

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

解决方案

这不是一个天生的问题,而是一个日期时间不明确的问题.

date 转换为

通常,建议在日期时间转换期间提供格式,例如

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

因为,正如我们在这里看到的,像 10/12/2020 这样的日期是不明确的.因此,解析器首先认为数据将是月/日/年,后来发现情况并非如此,因此更改为将您的输入解析为日/月/年,从而在您的 seaborn 图中产生这些时间旅行尖峰.你问为什么你没有在熊猫图中看到它们?嗯,这是根据索引绘制的,所以你不会注意到熊猫图中的这个转换问题.
有关格式代码的更多信息可以在 Python 中找到文档.

I am hoping to understand why the following Seaborn lineplot behaviour occurs.

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?

Regular plot of data:

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

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

Seaborn plot of data:

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

解决方案

This is not a seaborn problem but a question of ambiguous datetimes.

Convert date to a datetime object with the following code:

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

and you get your expected plot with seaborn

Generally, it is advisable to provide the format during datetime conversions, e.g.,

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. 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. Why you didn't see them in the pandas plot, you ask? Well, this is plotted against the index, so you don't notice this conversion problem in the pandas plot.
More information on the format codes can be found in the Python documentation.

这篇关于Seaborn 线图意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆