How can a simple bivariate distribution be shown using 'imshow' in Matplotlib Python?

Matplotlib is a popular Python package used for data visualization. The imshow function is particularly useful for displaying 2D arrays as images, making it perfect for visualizing bivariate distributions where we want to show the relationship between two variables.

A bivariate distribution represents the probability distribution of two random variables occurring together. Using imshow, we can create heatmap-style visualizations that show how the probability density varies across different combinations of the two variables.

Creating Sample Data

First, let's create a bivariate distribution using two Gaussian functions ?

import numpy as np
import matplotlib.pyplot as plt

# Set random seed for reproducibility
np.random.seed(9654241)

# Create coordinate arrays
delta = 0.025
x = y = np.arange(-4.5, 4.5, delta)
X, Y = np.meshgrid(x, y)

# Create two Gaussian distributions
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)

# Combine them to create an interesting bivariate distribution
Z = (Z1 - Z2) * 2

print("Data shape:", Z.shape)
print("Data range:", Z.min(), "to", Z.max())
Data shape: (360, 360)
Data range: -1.9999999999999998 to 2.0

Visualizing with imshow

Now we'll use imshow to display this bivariate distribution ?

import numpy as np
import matplotlib.pyplot as plt

# Create the same data
np.random.seed(9654241)
delta = 0.025
x = y = np.arange(-4.5, 4.5, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

# Create the plot
fig, ax = plt.subplots(figsize=(8, 6))

# Display the bivariate distribution using imshow
im = ax.imshow(Z, 
               interpolation='bilinear',
               origin='lower',
               extent=[-3, 3, -3, 3],
               vmax=abs(Z).max(), 
               vmin=-abs(Z).max(),
               cmap='RdYlBu')

# Add labels and title
plt.title('Bivariate Distribution using imshow')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Add colorbar to show the scale
plt.colorbar(im, label='Probability Density')
plt.show()

Key Parameters Explained

The imshow function accepts several important parameters for bivariate visualization ?

  • interpolation='bilinear' − Smooths the image for better visual appearance
  • origin='lower' − Places the origin at the bottom-left corner
  • extent − Sets the coordinate range for x and y axes
  • vmax/vmin − Controls the color scale range
  • cmap − Specifies the color map for visualization

Alternative Visualization with Contours

For comparison, here's how the same data looks with contour lines ?

import numpy as np
import matplotlib.pyplot as plt

# Create the same data
np.random.seed(9654241)
delta = 0.025
x = y = np.arange(-4.5, 4.5, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

# Create subplot comparison
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# imshow version
im1 = ax1.imshow(Z, interpolation='bilinear', origin='lower', 
                 extent=[-3, 3, -3, 3], cmap='RdYlBu')
ax1.set_title('Using imshow')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y-axis')

# contour version
im2 = ax2.contourf(X, Y, Z, levels=20, cmap='RdYlBu')
ax2.set_title('Using contourf')
ax2.set_xlabel('X-axis')
ax2.set_ylabel('Y-axis')

plt.tight_layout()
plt.show()

Conclusion

The imshow function provides an excellent way to visualize bivariate distributions as heatmaps. It's particularly useful when you want to show continuous probability density surfaces, with colors representing the magnitude of the distribution at each point in the 2D space.

---
Updated on: 2026-03-25T14:53:55+05:30

524 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements