How to plot two Seaborn lmplots side-by-side (Matplotlib)?

To plot two Seaborn lmplots side-by-side using Matplotlib subplots, we need to use regplot() instead of lmplot() since lmplot() creates its own figure. The regplot() function allows us to specify axes for subplot positioning.

Steps to Create Side-by-Side Plots

  • Create subplots using plt.subplots(1, 2) with desired figure size

  • Generate sample data with continuous variables for regression plots

  • Use sns.regplot() to create scatter plots with regression lines on each axis

  • Adjust spacing between subplots using tight_layout()

  • Display the plots using plt.show()

Example

Here's how to create two regression plots side-by-side ?

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

# Set random seed for reproducible results
np.random.seed(42)

# Create figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Generate sample data
data1 = pd.DataFrame({
    'x': np.linspace(1, 10, 20),
    'y': np.linspace(2, 8, 20) + np.random.normal(0, 0.5, 20)
})

data2 = pd.DataFrame({
    'x': np.linspace(1, 10, 20), 
    'y': 10 - np.linspace(1, 8, 20) + np.random.normal(0, 0.8, 20)
})

# Create regression plots on each axis
sns.regplot(data=data1, x='x', y='y', color='blue', ax=axes[0])
axes[0].set_title('Positive Correlation')
axes[0].set_xlabel('X Values')
axes[0].set_ylabel('Y Values')

sns.regplot(data=data2, x='x', y='y', color='red', ax=axes[1])
axes[1].set_title('Negative Correlation') 
axes[1].set_xlabel('X Values')
axes[1].set_ylabel('Y Values')

# Adjust layout and display
plt.tight_layout()
plt.show()

Alternative: Using Single DataFrame with Different Columns

You can also plot different columns from the same DataFrame ?

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

# Create sample dataset
np.random.seed(42)
df = pd.DataFrame({
    'height': np.random.normal(170, 10, 50),
    'weight': np.random.normal(70, 15, 50),
    'age': np.random.randint(20, 60, 50),
    'income': np.random.normal(50000, 15000, 50)
})

# Create correlation between height and weight
df['weight'] = df['height'] * 0.8 + np.random.normal(0, 5, 50)

# Create subplots
fig, axes = plt.subplots(1, 2, figsize=(14, 6))

# Plot height vs weight
sns.regplot(data=df, x='height', y='weight', color='green', ax=axes[0])
axes[0].set_title('Height vs Weight Relationship')

# Plot age vs income
sns.regplot(data=df, x='age', y='income', color='purple', ax=axes[1])
axes[1].set_title('Age vs Income Relationship')

plt.tight_layout()
plt.show()

Key Differences: lmplot() vs regplot()

Function Creates Own Figure Works with Subplots Best For
lmplot() Yes No Single plots with faceting
regplot() No Yes Subplot integration

Conclusion

Use sns.regplot() with plt.subplots() to create side-by-side regression plots. The regplot() function works seamlessly with Matplotlib's subplot system, unlike lmplot() which creates its own figure.

Updated on: 2026-03-25T19:53:21+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements