Contour Plot
Gaussian Mixture Model
Contour plot of a mixture of three Gaussian distributions.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
x = np.linspace(-5, 5, 200)
y = np.linspace(-5, 5, 200)
X, Y = np.meshgrid(x, y)
# Three Gaussians
def gaussian(X, Y, mx, my, sx, sy):
return np.exp(-((X-mx)**2/(2*sx**2) + (Y-my)**2/(2*sy**2)))
Z = 0.4 * gaussian(X, Y, -2, -1, 0.8, 1.2)
Z += 0.35 * gaussian(X, Y, 2, 1.5, 1, 0.7)
Z += 0.25 * gaussian(X, Y, 0, -2, 1.2, 0.9)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
# Pink gradient
colors = ['#fdf2f8', '#fbcfe8', '#F527B0', '#9d174d']
cmap = LinearSegmentedColormap.from_list('pink', colors, N=256)
cs = ax.contourf(X, Y, Z, levels=25, cmap=cmap)
ax.contour(X, Y, Z, levels=12, colors='#831843', linewidths=0.5, alpha=0.5)
cbar = plt.colorbar(cs, ax=ax, pad=0.02)
cbar.set_label('Density', color='#374151', fontsize=11)
cbar.ax.yaxis.set_tick_params(color='#374151')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#374151')
ax.set_xlabel('X', fontsize=11, color='#374151', fontweight='500')
ax.set_ylabel('Y', fontsize=11, color='#374151', fontweight='500')
ax.set_title('Gaussian Mixture Model', fontsize=14, color='#1f2937', fontweight='bold', pad=15)
ax.tick_params(colors='#6b7280', labelsize=9)
for spine in ax.spines.values():
spine.set_color('#d1d5db')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Contour Plot examples
☕