Python 中的 Seaborn tsplot()

Manav Narula 2024年2月15日
Python 中的 Seaborn tsplot()

在本教程中,我们将学习如何在 Seaborn 中使用 seaborn.tsplot() 函数。

seaborn.tsplot() 是一个非常强大和有用的函数。当我们有可用数据的时间戳时使用它。它用于绘制一个或多个时间序列数据。数据可以是长数据帧的形式,也可以是具有单位和时间维度的 N 维数组。

此功能还可以轻松高效地绘制多个时间序列数据。我们可以使用各种参数(例如 unitcondition 等)自定义最终绘图。unit 参数可以是一个 series 或 DataFrame 列,用于标识采样单元,condition 参数可以标识数据的条件或其类别。

我们可以使用 colorlinewidth 参数更改最后一行,使绘图在视觉上更具吸引力。

在以下代码中,我们将使用此函数绘制图形。

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

df = pd.DataFrame(
    {
        "Date": [
            "01012019",
            "01022019",
            "01032019",
            "01042019",
            "01052019",
            "01062019",
            "01072019",
            "01082019",
        ],
        "Price 1": [77, 76, 68, 70, 78, 79, 74, 75],
    }
)
df["Date"] = pd.to_datetime(df["Date"], format="%d%m%Y")

sns.tsplot(data=df["Price 1"], time=df["Date"], color="blue", linewidth=5)

seaborn tsplot 函数

值得一提的是,虽然该功能对于绘制时间序列数据非常强大,但它在 2020 年 1 月版本的 seaborn 模块中已被弃用。建议使用 seaborn.lineplot() 函数作为替代。

作者: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn