lineplot
📂

lineplot

Codes

sns.lineplot()

Notes

Line plot for visualizing trends over a continuous variable (e.g., time series).

Status
Done

Seaborn sns.lineplot() — Complete Guide

The sns.lineplot() function in Seaborn is a fundamental tool for visualizing relationships and trends between numerical variables, especially over a continuous interval like time. It draws a line that connects data points, helping you spot trends, patterns, and deviations.

Function Signature


sns.lineplot(
    data=None,
    *,
    x=None,
    y=None,
    hue=None,
    style=None,
    size=None,
    palette=None,
    hue_order=None,
    size_order=None,
    sizes=None,
    dashes=True,
    markers=None,
    legend="auto",
    ax=None,
    **kwargs
)

🗂️ Key Parameters

🔑 Primary Parameters

  • data: The dataset (Pandas DataFrame or array-like) to plot.
  • x: Variable plotted on the x-axis (often time or ordered values).
  • y: Variable plotted on the y-axis.

🎨 Aesthetic Mappings

  • hue: Differentiate lines by color.
  • style: Differentiate lines by pattern (solid, dashed) based on a categorical variable.
  • size: Vary line thickness based on a numeric or categorical variable.
  • palette: Specify the color palette for hue.

📏 Order and Sizes

  • hue_order: Order of categories for hue.
  • size_order: Order for size.
  • sizes: Range for line widths (e.g., (1, 5)).

➿ Line Patterns & Markers

  • dashes: Whether to use different dash styles for style. Can be TrueFalse, or a dict.
  • markers: Add point markers to the line for each observation.

🗂️ Legend

  • legend"auto""brief""full" or False to control legend display.

🧩 Axes

  • ax: Matplotlib Axes object to draw the plot on.

🎛️ Additional Styling (*kwargs)

Supports Matplotlib keyword arguments for extra customization: alpha (transparency), linewidthlinestyle, etc.

📌 Dataset Preparation

Let’s use the built-in Flights dataset as an example.

import seaborn as sns
import matplotlib.pyplot as plt

# Load example dataset
flights = sns.load_dataset('flights')
flights.head()

     year month  passengers
0    1949   Jan         112
1    1949   Feb         118
2    1949   Mar         132
3    1949   Apr         129
4    1949   May         121
..    ...   ...         ...
139  1960   Aug         606
140  1960   Sep         508
141  1960   Oct         461
142  1960   Nov         390
143  1960   Dec         432

[144 rows x 3 columns]

🚩 Examples

1️⃣ Basic Line Plot


sns.lineplot(data=flights, x='year', y='passengers')
plt.title("Basic Line Plot")
plt.show()

Use Case: Show trend over time.

image

2️⃣ Line Plot with hue


sns.lineplot(data=flights, x='year', y='passengers', hue='month')
plt.title("Line Plot with Hue")
plt.show()

Key Parameterhue differentiates lines by color.

image

3️⃣ Line Plot with style


sns.lineplot(data=flights, x='year', y='passengers', hue='month', style='month')
plt.title("Line Plot with Hue and Style")
plt.show()

Key Parameterstyle uses different line patterns (dashed, solid).

image

4️⃣ Line Plot with size


sns.lineplot(data=flights, x='year', y='passengers', hue='month', size='month', sizes=(1, 5))
plt.title("Line Plot with Variable Line Widths")
plt.show()

Key Parametersize controls line thickness.

image

5️⃣ Custom palette


sns.lineplot(data=flights, x='year', y='passengers', hue='month', palette='tab10')
plt.title("Line Plot with Custom Palette")
plt.show()
image

6️⃣ Ordered hue_order


months_order = ['January', 'February', 'March', 'April', 'May', 'June',
                'July', 'August', 'September', 'October', 'November', 'December']

sns.lineplot(
    data=flights,
    x='year',
    y='passengers',
    hue='month',
    hue_order=months_order,
    palette='tab10'
)
plt.title("Line Plot with Ordered Hue")
plt.show()

image

7️⃣ Add Markers


sns.lineplot(
    data=flights,
    x='year',
    y='passengers',
    hue='month',
    style='month',
    markers=True
)
plt.title("Line Plot with Markers")
plt.show()

image

8️⃣ Control Dashes


sns.lineplot(
    data=flights,
    x='year',
    y='passengers',
    hue='month',
    style='month',
    dashes=False  # Use solid lines only
)
plt.title("Line Plot with Solid Lines")
plt.show()

image

9️⃣ Combine huestylesize, and Markers


sns.lineplot(
    data=flights,
    x='year',
    y='passengers',
    hue='month',
    style='month',
    size='passengers',
    sizes=(1, 5),
    markers=True,
    dashes=True
)
plt.title("Comprehensive Line Plot")
plt.show()

image

🔟 Transparency


sns.lineplot(data=flights, x='year', y='passengers', hue='month', alpha=0.7)
plt.title("Line Plot with Transparency")
plt.show()

image

🧑‍💻 Use Cases for Data Scientists

  1. Trend Analysis
  2. Understand changes in key metrics over time.

  3. Time Series Analysis
  4. Visualize seasonality, cycles, and long-term patterns.

  5. Model Validation
  6. Plot actual vs. predicted values for regression models.

  7. Group Comparison
  8. Compare trends across multiple categories.

✔️ Practical Notes

  • Dealing with Overplotting:
  • Use alpha for transparency or style for different line patterns.

  • Customize Markers & Dashes:
  • Combine markers and dashes for better readability.

  • Faceting for Large Groups:
  • Use relplot(kind="line") to facet by col or row.

  • Legend Control:
  • Use legend="brief" or legend=False for simpler legends.

  • Custom Sizes:
  • Use sizes=(min, max) for meaningful line width scaling.

📌 References in Your ML Workflow

✅ EDA: Spot trends in time series or continuous data.

✅ Visualization: Present clear trend plots.

✅ Model Evaluation: Compare model output vs. actuals.

🔑 Summary:

sns.lineplot() is your go-to function for creating powerful trend and relationship visualizations — ideal for time series analysis, comparing multiple groups, and validating model performance.