How to make joint bivariate distributions in Matplotlib?

Joint bivariate distributions visualize the relationship between two continuous variables. In Matplotlib, we can create these distributions using scatter plots with transparency to show density patterns.

Basic Joint Bivariate Distribution

Here's how to create a simple joint bivariate distribution using correlated data ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True

# Generate correlated data
np.random.seed(42)
x = 2 * np.random.randn(5000)
y = x + np.random.randn(5000)

# Create scatter plot
fig, ax = plt.subplots()
scatter = ax.scatter(x, y, alpha=0.08, c=x, cmap="viridis")

# Add labels and title
ax.set_xlabel('X Variable')
ax.set_ylabel('Y Variable')
ax.set_title('Joint Bivariate Distribution')

# Add colorbar
plt.colorbar(scatter)
plt.show()

Enhanced Bivariate Distribution with Marginal Plots

For a more comprehensive view, we can add marginal histograms to show individual distributions ?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

# Generate data
np.random.seed(42)
x = np.random.normal(0, 1, 2000)
y = 0.5 * x + np.random.normal(0, 0.8, 2000)

# Create figure with custom grid
fig = plt.figure(figsize=(10, 8))
gs = GridSpec(3, 3, figure=fig)

# Main scatter plot
ax_main = fig.add_subplot(gs[1:, :-1])
ax_main.scatter(x, y, alpha=0.6, c='blue', s=10)
ax_main.set_xlabel('X Variable')
ax_main.set_ylabel('Y Variable')

# Top histogram (x marginal)
ax_top = fig.add_subplot(gs[0, :-1])
ax_top.hist(x, bins=50, alpha=0.7, color='blue', density=True)
ax_top.set_xlim(ax_main.get_xlim())
ax_top.set_title('Joint Bivariate Distribution with Marginals')

# Right histogram (y marginal)
ax_right = fig.add_subplot(gs[1:, -1])
ax_right.hist(y, bins=50, orientation='horizontal', alpha=0.7, color='blue', density=True)
ax_right.set_ylim(ax_main.get_ylim())

plt.tight_layout()
plt.show()

Multiple Distribution Comparison

Compare different bivariate relationships in a single plot ?

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(42)
n_points = 1000

# Generate different relationships
x1 = np.random.normal(0, 1, n_points)
y1 = x1 + np.random.normal(0, 0.5, n_points)  # Positive correlation

x2 = np.random.normal(2, 1, n_points)
y2 = -x2 + np.random.normal(0, 0.5, n_points)  # Negative correlation

x3 = np.random.normal(-1, 1, n_points)
y3 = np.random.normal(0, 1, n_points)  # No correlation

# Create plot
fig, ax = plt.subplots(figsize=(10, 7))

ax.scatter(x1, y1, alpha=0.5, label='Positive Correlation', s=20)
ax.scatter(x2, y2, alpha=0.5, label='Negative Correlation', s=20)
ax.scatter(x3, y3, alpha=0.5, label='No Correlation', s=20)

ax.set_xlabel('X Variable')
ax.set_ylabel('Y Variable')
ax.set_title('Comparing Different Bivariate Relationships')
ax.legend()
ax.grid(True, alpha=0.3)

plt.show()

Key Parameters

Parameter Purpose Common Values
alpha Transparency for density visualization 0.1 - 0.8
c Color mapping based on values Variable or color name
cmap Colormap for gradient effects 'viridis', 'plasma', 'copper'
s Size of scatter points 10-50 for dense plots

Conclusion

Joint bivariate distributions effectively reveal relationships between two variables using scatter plots with transparency. Adding marginal plots and color mapping enhances the visualization by showing both joint and individual distributions.

Updated on: 2026-03-25T21:58:33+05:30

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements