Contour Plot
Branin Function Optimization Landscape
Classic Branin optimization test function with three global minima marked, using green to coral colormap on light background.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Branin function (optimization benchmark)
x = np.linspace(-5, 10, 200)
y = np.linspace(0, 15, 200)
X, Y = np.meshgrid(x, y)
a = 1
b = 5.1 / (4 * np.pi**2)
c = 5 / np.pi
r = 6
s = 10
t = 1 / (8 * np.pi)
Z = a * (Y - b * X**2 + c * X - r)**2 + s * (1 - t) * np.cos(X) + s
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
# Custom colormap - green to coral
colors = ['#dcfce7', '#22c55e', '#F5B027', '#F5276C']
cmap = LinearSegmentedColormap.from_list('green_coral', colors, N=256)
cs = ax.contourf(X, Y, Z, levels=35, cmap=cmap)
ax.contour(X, Y, Z, levels=20, colors='#374151', linewidths=0.4, alpha=0.5)
cbar = plt.colorbar(cs, ax=ax, pad=0.02)
cbar.set_label('f(x, y)', color='#374151', fontsize=11)
cbar.ax.tick_params(colors='#6b7280', labelsize=9)
# Mark global minima
minima = [(-np.pi, 12.275), (np.pi, 2.275), (9.42478, 2.475)]
for mx, my in minima:
ax.scatter(mx, my, color='#5314E6', s=80, marker='*', zorder=5, edgecolor='white', linewidth=1)
ax.set_xlabel('x', fontsize=11, color='#374151')
ax.set_ylabel('y', fontsize=11, color='#374151')
ax.set_title('Branin Function Optimization Landscape', 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
☕