Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to plot a time series graph using Seaborn or Plotly?
Time series graphs help visualize data changes over time. Seaborn and Plotly are powerful Python libraries that make creating time series plots straightforward and visually appealing.
Using Seaborn for Time Series Plot
Seaborn's lineplot() function creates clean time series visualizations with minimal code ?
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True
# Create sample time series data
df = pd.DataFrame({
'time': pd.date_range("2021-01-01 12:00:00", periods=10, freq="30min"),
'speed': np.linspace(1, 10, 10)
})
# Create the time series plot
ax = sns.lineplot(x="time", y="speed", data=df, marker='o')
ax.set_title("Speed Over Time", fontsize=14)
ax.tick_params(axis='x', rotation=45)
plt.tight_layout()
plt.show()
Using Plotly for Interactive Time Series
Plotly creates interactive time series plots with built-in zoom and hover functionality ?
import plotly.express as px
import pandas as pd
import numpy as np
# Create sample data
df = pd.DataFrame({
'time': pd.date_range("2021-01-01", periods=30, freq="D"),
'temperature': np.random.normal(20, 5, 30),
'humidity': np.random.normal(60, 10, 30)
})
# Create interactive time series plot
fig = px.line(df, x='time', y=['temperature', 'humidity'],
title='Temperature and Humidity Over Time',
labels={'value': 'Measurement', 'variable': 'Metric'})
fig.update_layout(xaxis_title="Date", yaxis_title="Value")
fig.show()
Multiple Time Series with Seaborn
Display multiple time series on the same plot using the hue parameter ?
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Create data with multiple series
dates = pd.date_range("2021-01-01", periods=20, freq="D")
data = []
for metric in ['Sales', 'Profit', 'Customers']:
for date in dates:
data.append({
'date': date,
'value': np.random.randint(10, 100),
'metric': metric
})
df = pd.DataFrame(data)
# Plot multiple time series
plt.figure(figsize=(12, 6))
sns.lineplot(data=df, x='date', y='value', hue='metric', marker='o')
plt.title('Business Metrics Over Time')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Comparison
| Library | Interactivity | Customization | Best For |
|---|---|---|---|
| Seaborn | Static | High | Publication-ready plots |
| Plotly | Interactive | Very High | Web dashboards, exploration |
Key Features
- Seaborn: Built on matplotlib, excellent for statistical plots, clean default styling
- Plotly: Interactive plots, hover tooltips, zoom/pan functionality, web-ready output
- Data Format: Both libraries work best with pandas DataFrames in long format
Conclusion
Use Seaborn for clean, publication-ready static time series plots with minimal code. Choose Plotly when you need interactive features like zooming and hover tooltips for data exploration.
Advertisements
