How to create multiple series scatter plots with connected points using seaborn?

Creating multiple series scatter plots with connected points in seaborn combines scatter plots with line connections to show relationships and trends across different data series. This visualization is useful for displaying how multiple variables change together over time or categories.

Basic Setup

First, let's import the required libraries and set up our data ?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Set figure parameters
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

# Create sample data with multiple series
data = {
    'x': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
    'y': [2, 5, 3, 8, 7, 3, 6, 4, 9, 8, 4, 7, 5, 10, 9],
    'series': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C']
}

df = pd.DataFrame(data)
print(df.head())
   x  y series
0  1  2      A
1  2  5      A
2  3  3      A
3  4  8      A
4  5  7      A

Using FacetGrid for Multiple Series

FacetGrid allows us to create separate plots for each series while maintaining consistent styling ?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Create sample data
data = {
    'x': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
    'y': [2, 5, 3, 8, 7, 3, 6, 4, 9, 8],
    'series': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B']
}
df = pd.DataFrame(data)

# Create FacetGrid with hue for different series
g = sns.FacetGrid(df, hue="series", height=5, aspect=1.2)

# Map scatter plot for points
g.map(plt.scatter, "x", "y", s=50, alpha=0.8)

# Map line plot to connect points
g.map(plt.plot, "x", "y", marker='o', linewidth=2)

# Add legend
g.add_legend()

plt.show()

Using seaborn.scatterplot with lineplot

A more direct approach combines scatterplot and lineplot functions ?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Create sample data
data = {
    'x': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
    'y': [2, 5, 3, 8, 7, 3, 6, 4, 9, 8],
    'series': ['Series A', 'Series A', 'Series A', 'Series A', 'Series A', 
               'Series B', 'Series B', 'Series B', 'Series B', 'Series B']
}
df = pd.DataFrame(data)

# Set up the plot
plt.figure(figsize=(10, 6))

# Create scatter plot with different colors for each series
sns.scatterplot(data=df, x="x", y="y", hue="series", s=100)

# Add connected lines
sns.lineplot(data=df, x="x", y="y", hue="series", marker='o', linewidth=2)

plt.title("Multiple Series Scatter Plot with Connected Points")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.grid(True, alpha=0.3)
plt.show()

Customizing the Plot

You can enhance the visualization with custom styling and multiple series ?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# Create more complex sample data
np.random.seed(42)
x_vals = list(range(1, 6)) * 3
series_vals = ['Product A'] * 5 + ['Product B'] * 5 + ['Product C'] * 5
y_vals = [10, 15, 12, 18, 16] + [8, 12, 14, 16, 19] + [12, 14, 16, 20, 22]

df = pd.DataFrame({
    'Month': x_vals,
    'Sales': y_vals,
    'Product': series_vals
})

# Create the plot with custom styling
plt.figure(figsize=(12, 7))
sns.set_style("whitegrid")

# Plot with custom markers and colors
ax = sns.scatterplot(data=df, x="Month", y="Sales", hue="Product", 
                     s=150, alpha=0.8)

# Add connecting lines
sns.lineplot(data=df, x="Month", y="Sales", hue="Product", 
             marker='o', linewidth=2.5, markersize=8)

plt.title("Sales Performance by Product Over Time", fontsize=16, fontweight='bold')
plt.xlabel("Month", fontsize=12)
plt.ylabel("Sales (in thousands)", fontsize=12)
plt.legend(title="Product", title_fontsize=12, fontsize=10)
plt.xticks([1, 2, 3, 4, 5], ['Jan', 'Feb', 'Mar', 'Apr', 'May'])

plt.tight_layout()
plt.show()

Key Parameters

Parameter Function Description
hue Both Column name for grouping series
s scatterplot Size of scatter points
marker lineplot Style of line markers
linewidth lineplot Thickness of connecting lines

Conclusion

Use FacetGrid with map() for complex multi-series plots, or combine scatterplot() and lineplot() for simpler implementations. Both approaches effectively show relationships between multiple data series with connected scatter points.

Updated on: 2026-03-26T19:05:19+05:30

664 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements