Python – Seaborn tsplot does not display data

Seaborn tsplot does not display data… here is a solution to the problem.

Seaborn tsplot does not display data

I’m trying to make a simple tsplot using seaborn, but for reasons I’m not clear to, nothing shows up when I run the code. Here’s a minimal example:

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()

enter image description here

Usually tsplot provides multiple data points for each time point, but not if only one is provided?

I

know this can be done very easily using matplotlib, but I wanted to use seaborn to implement some of its other features.

Solution

You are missing individual units. When working with data frames, the idea is that multiple time series of the same unit are recorded, and they can be used as separate identifiers in the data frame. The error is then calculated based on the different units.

So for a series, you can make it work again like this:

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

To understand how errors are calculated, take a look at this example:

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')

Related Problems and Solutions