1 import numpy as np, matplotlib.pyplot as plt
   2 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS
   3 setup_style()
   4 
   5 x = np.linspace(-3, 3, 100); y = np.linspace(-3, 3, 100)
   6 X, Y = np.meshgrid(x, y)
   7 Z = (1-X)**2 + 100*(Y-X**2)**2  # Rosenbrock 函数
   8 Z = np.log10(Z + 1)
   9 
  10 fig, ax = plt.subplots(figsize=(7, 6))
  11 cf = ax.contourf(X, Y, Z, levels=20, cmap='YlOrRd', alpha=0.8)
  12 cs = ax.contour(X, Y, Z, levels=10, colors='white', linewidths=0.5, alpha=0.6)
  13 ax.clabel(cs, inline=True, fontsize=7, fmt='%.1f')
  14 
  15 # 最优点
  16 ax.scatter([1], [1], s=100, color=COLORS['down'], edgecolor='white', linewidth=2, zorder=5, marker='*')
  17 ax.annotate('全局最优\n(1.0, 1.0)', xy=(1, 1), xytext=(1.8, 2.2),
  18             fontsize=9, fontweight='bold', color=COLORS['down'],
  19             arrowprops=dict(arrowstyle='->', color=COLORS['down'], lw=1.2),
  20             bbox=dict(boxstyle='round,pad=0.3', facecolor='white', edgecolor=COLORS['down'], alpha=0.9))
  21 
  22 fig.colorbar(cf, ax=ax, shrink=0.8, label='$\\log_{10}(f+1)$')
  23 ax.set_xlabel('参数 $x_1$', fontsize=11); ax.set_ylabel('参数 $x_2$', fontsize=11)
  24 ax.spines['top'].set_visible(False); ax.spines['right'].set_visible(False)
  25 fig.tight_layout()
  26 save_fig(fig, 'figures/fig_contour.pdf')