Line Plot

Purpose

Visualize trends over time or continuous sequences. Essential for time series analysis and trend identification.

Analysis Type

Bivariate (typically time vs. value)

What to Look For

2. Seasonality
3. Volatility
4. Outliers/Anomalies
5. Multiple Series
6. Change Points

Code Example

# Example: Line plots using seaborn's flights dataset
import seaborn as sns
import matplotlib.pyplot as plt

# Load sample dataset
flights = sns.load_dataset('flights')

# Basic line plot: Passengers over time (by year)
plt.figure(figsize=(8, 4))
yearly = flights.groupby('year')['passengers'].sum().reset_index()
plt.plot(yearly['year'], yearly['passengers'])
plt.title("Total Passengers per Year")
plt.xlabel("Year")
plt.ylabel("Passengers")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

# Multiple lines: Passengers per month for a few years
sample_years = [1949, 1950, 1951]
months = flights['month'].unique()
plt.figure(figsize=(8, 4))
for year in sample_years:
    data = flights[flights['year'] == year]
    plt.plot(months, data['passengers'], label=str(year))

plt.title("Monthly Passengers for Selected Years")
plt.xlabel("Month")
plt.ylabel("Passengers")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

# Seaborn lineplot with confidence interval (trend by month across years)
plt.figure(figsize=(8, 4))
# Convert month to categorical for correct order
flights['month'] = pd.Categorical(flights['month'], categories=months, ordered=True)
sns.lineplot(x='month', y='passengers', data=flights, estimator='mean', marker='o')
plt.title("Average Passengers per Month (All Years)")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Pro Tip

Add rolling averages to smooth noisy time series and reveal underlying trends: df['rolling_avg'] = df['value'].rolling(window=7).mean(); plt.plot(df['date'], df['value'], alpha=0.3, label='Original'); plt.plot(df['date'], df['rolling_avg'], linewidth=2, label='7-day Average'). Use plt.grid(True, alpha=0.3) to add a subtle grid that helps read values. For multiple series, use different line styles: linestyle='--', '-.', or ':'.

ML_AI/_feature_engineering/images/line-1.png
ML_AI/_feature_engineering/images/line-2.png
ML_AI/_feature_engineering/images/line-3.png

Documentation