Plot the ctrl applied per control step.
(result: RolloutResult, out_path: Path, title: str | None = None)
| 51 | |
| 52 | |
| 53 | def plot_ctrl(result: RolloutResult, out_path: Path, title: str | None = None) -> None: |
| 54 | """Plot the ctrl applied per control step.""" |
| 55 | out_path = Path(out_path) |
| 56 | out_path.parent.mkdir(parents=True, exist_ok=True) |
| 57 | |
| 58 | if result.ctrl.size == 0: |
| 59 | return |
| 60 | |
| 61 | fig, ax = plt.subplots(figsize=(11, 3.5)) |
| 62 | nu = result.ctrl.shape[1] |
| 63 | for i in range(nu): |
| 64 | name = result.actuator_names[i] if i < len(result.actuator_names) else f"u{i}" |
| 65 | ax.plot(result.ctrl[:, i], lw=1.0, label=name) |
| 66 | ax.set_xlabel("control step") |
| 67 | ax.set_ylabel("ctrl") |
| 68 | if title is not None: |
| 69 | ax.set_title(title) |
| 70 | ax.grid(True, alpha=0.3) |
| 71 | ax.legend(fontsize=8, ncol=min(nu, 6), loc="best") |
| 72 | fig.tight_layout() |
| 73 | fig.savefig(out_path, dpi=110) |
| 74 | plt.close(fig) |