【问题标题】:Seaborn tsplot not showing dataSeaborn tsplot 不显示数据
【发布时间】:2017-08-14 18:28:31
【问题描述】:

我正在尝试使用 seaborn 制作一个简单的 tsplot,但由于我不清楚的原因,当我运行代码时没有显示任何内容。这是一个最小的例子:

import numpy as np
import seaborn as sns
import pandas as pd

df = pd.DataFrame({'value': np.random.rand(31), 'time': range(31)})

ax = sns.tsplot(data=df, value='value', time='time')
sns.plt.show()

通常 tsplot 你会为每个时间点提供多个数据点,但如果你只提供一个就行不通了吗?

我知道 matplotlib 可以很容易地做到这一点,但我想将 seaborn 用于它的一些其他功能。

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    您缺少个人units。使用数据框时的想法是记录同一unit 的多个时间序列,这可以是数据框中的单独标识符。然后根据不同的units计算误差。

    所以只有一个系列,你可以像这样让它再次工作:

    df = pd.DataFrame({'value': np.random.rand(31), 'time': range(31)})
    df['subject'] = 0
    sns.tsplot(data=df, value='value', time='time', unit='subject')
    

    只是想看看误差是如何计算的,看看这个例子:

    dfs = []
    for i in range(10):
        df = pd.DataFrame({'value': np.random.rand(31), 'time': range(31)})
        df['subject'] = i
        dfs.append(df)
    all_dfs = pd.concat(dfs)
    sns.tsplot(data=all_dfs, value='value', time='time', unit='subject')
    

    【讨论】:

      【解决方案2】:

      您可以将set_index 用于index 从列time 中,然后绘制Series

      df = pd.DataFrame({'value': np.random.rand(31), 'time': range(31)})
      df = df.set_index('time')['value']
      ax = sns.tsplot(data=df)
      sns.plt.show()
      

      【讨论】:

        猜你喜欢
        • 2015-05-23
        • 2015-09-24
        • 2017-05-11
        • 2016-07-22
        • 2016-05-27
        • 2016-03-14
        • 1970-01-01
        • 2015-02-10
        相关资源
        最近更新 更多