Seaborn‘s tsplot() function, despite being deprecated for newer APIs like lineplot(), remains a versatile tool for plotting temporal data when working with older Python codebases. This comprehensive guide aims to provide extensive details, best practices, and examples for harnessing tsplot() effectively.

Overview of tsplot() Capabilities

The tsplot() API enables:

  • Visualizing metrics over time
  • Displaying uncertainty and confidence intervals
  • Plotting multiple time series grouped by color
  • Support for irregular/non-linear time axes
  • High levels of customization – colors, style, annotations

It allowed concise plotting directly from pandas DataFrames:

import seaborn as sns
df = pd.DataFrame({"Date": ..., "Values": ...}) 
sns.tsplot(data=df, time="Date", value="Values")

While superseded by lineplot(), facetgrid() and relplot() – tsplot() remains relevant for those maintaining older code or wanting layered control.

Basic Single Time Series Plot

Consider an e-commerce site tracking daily revenue over 30 days. We generate a simulated dataset:

import pandas as pd
import numpy as np

days = 30
revenue = np.random.randint(5000, 15000, days)  

data = pd.DataFrame({
    "Date": pd.date_range(‘2023-01-01‘, periods=days),  
    "Revenue": revenue
})

We can visualize using tsplot():

import seaborn as sns
sns.tsplot(data=data, time="Date", value="Revenue")

Simulated Revenue Time Series

With a simple API call, we plot the progression of revenue over time. The plot is fully styled automatically. Next we add further enhancements.

Plotting Uncertainty Bands

To communicate variance and uncertainty in the data, we visualize confidence intervals using the ci parameter:

sns.tsplot(data=data, time="Date", value="Revenue", ci=99)

Revenue Plot with 99% Confidence Interval

The 99% confidence band indicates the plausible range of revenues at each point based on the observed variance. Other options like the standard deviation could also be used.

Layering Events of Importance

We can annotate events like marketing campaigns that may correlate to revenue patterns:

data[‘Events‘] = ["Campaign #1", None, None,  
                  "Major Sale!", None, None, None,  
                  "Ad Blitz", None, None]

sns.tsplot(data=data, time="Date", value="Revenue", ci=99) 
sns.tsplot(data=data, time="Date", value="Events", color=‘green‘)

Revenue with Event Overlay

Now we visually link revenue fluctuations to external events!

Visualizing Multiple Time Series

We extend our analysis to compare revenue against order volumes:

data = pd.DataFrame({
    "Date": pd.date_range(‘2023-01-01‘, periods=days),  
    "Revenue": revenue, 
    "Orders": np.random.randint(500, 2000, days)  
})

Passing multiple columns to value plots each metric separately:

sns.tsplot(data=data, time="Date", 
           value=["Revenue", "Orders"])

Multi Time Series Plot

We can now contrast revenue and order trends over time in a single view!

Grouping and Comparing Sub-samples

We could also split time series by groups for more advanced analysis:

data[‘Source‘] = np.random.choice([‘Organic‘, ‘Social‘, ‘Referral‘], days) 

sns.tsplot(data=data, time="Date", value="Revenue",
           hue=‘Source‘)

Grouped Time Series by Color

Here revenue is split out and color-coded by the marketing source, revealing trends.

Specialized Time Series Considerations

For effective usage, tsplot() must be configured appropriately for specialized scenarios:

Irregular Time Intervals

If time points are irregular, tsplot() can adapt automatically or the axis can be transformed:

days = [1, 7, 12, 19, 22, 26, 30]  # Irregular intervals

sns.tsplot(time=days, value=revenue)

# Or with temporal axis normalization  
ax = sns.tsplot(time=days, value=revenue) 
ax.set_xlim(1, 30)  

This handles uneven time deltas smoothly without resampling.

Long Time Series

For lengthy time series like multi-year data, downsampling may be needed for overview visualization:

data = pd.DataFrame({
    "Date": pd.date_range(‘2010-01-01‘, periods=5*365),
    "Values": values 
})

sns.tsplot(data=data, time="Date", value="Values", 
           downsample=30) # Downsample to monthly

This aggregates data points for cleaner plots over longer time frames.

Confidence Intervals

Choosing confidence levels involves tradeoffs. Wider bands visually communicate higher uncertainty but may overlap too much. Domain expertise is essential.

Conclusion

While the usage of tsplot() is fading with mature alternatives available, it remains a versatile option for plotting temporal data – especially for existing infrastructure. With the extensive examples and best practices provided in this guide, both novice users and experts can take full advantage of tsplot‘s capabilities.

Similar Posts