```python import numpy as np import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches fig, ax = plt.subplots(figsize=(10, 10), dpi=300) ax.set_aspect('equal') ax.axis('off') # 绘制花瓣 petal_verts = [ (0, 0), (-0.2, 1.5), (-1.2, 2.5), (-2.5, 2.0), (-3.0, 1.0), (-2.8, 0.2), (-2.0, -0.5), (-1.0, -0.8), (0, -0.5), (1.0, -0.8), (2.0, -0.5), (2.8, 0.2), (3.0, 1.0), (2.5, 2.0), (1.2, 2.5), (0.2, 1.5), (0, 0) ] petal_codes = [ Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CLOSEPO ] petal_path = Path(petal_verts, petal_codes) petal_patch = patches.PathPatch(petal_path, facecolor='crimson', edgecolor='darkred', lw=1.5, alpha=0.9) # 绘制花蕊 stamen_verts = [ (0, 0), (0.1, 3.0), (-0.1, 3.0), (0, 0) ] stamen_codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3, Path.CLOSEPO] stamen_path = Path(stamen_verts, stamen_codes) # 创建多个花瓣和花蕊 num_petals = 6 for i in range(num_petals): angle = i * (2 * np.pi / num_petals) rot_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]) rotated_petal_verts = [np.dot(rot_matrix, np.array(v).T) for v in petal_verts] rotated_petal_path = Path(rotated_petal_verts, petal_codes) rotated_petal_patch = patches.PathPatch(rotated_petal_path, facecolor='crimson', edgecolor='darkred', lw=1.5, alpha=0.9) ax.add_patch(rotated_petal_patch) for j in range(8): stamen_angle = angle + j * (2 * np.pi / 8) * 0.2 stamen_rot_matrix = np.array([[np.cos(stamen_angle), -np.sin(stamen_angle)], [np.sin(stamen_angle), np.cos(stamen_angle)]]) rotated_stamen_verts = [np.dot(stamen_rot_matrix, np.array(v).T) for v in stamen_verts] rotated_stamen_path = Path(rotated_stamen_verts, stamen_codes) stamen_patch = patches.PathPatch(rotated_stamen_path, facecolor='gold', edgecolor='darkgoldenrod', lw=1, alpha=0.8) ax.add_patch(stamen_patch) ax.set_xlim(-4, 4) ax.set_ylim(-4, 4) plt.tight_layout() plt.savefig('red_spider_lily.png', bbox_inches='tight', pad_inches=0, dpi=300) plt.show() ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 路径控制点的对称性影响曲线平滑度 旋转角度计算需要精确的弧度转换 图形叠加时注意图层顺序和透明度设置 [2025-08-28 22:52:52 | AI写代码神器 | 958点数解答]