Skip to content

Commit b190e55

Browse files
committed
Enhance interactive fitting: update fit method to include FWHM widgets and improve sum line handling
1 parent d5763ce commit b190e55

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

Examples/Interactive/plot_interactive_fitting.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def gaussian(x, amp, mu, sigma):
5555
fig, ax = apl.subplots(1, 1, figsize=(720, 380))
5656
plot = ax.plot(signal, axes=[x], color="#adb5bd", linewidth=1.5,
5757
alpha=0.6, label="data")
58+
#
59+
# Live sum of all components — this IS the fit after pressing 'f'
60+
sum_line = plot.add_line(
61+
sum(gaussian(x, **p) for p in INIT_P), x_axis=x,
62+
color="#e0e0e0", linewidth=1.5, linestyle="dashed", label="sum",
63+
)
5864

5965
comp_lines = [
6066
plot.add_line(gaussian(x, **p), x_axis=x,
@@ -63,11 +69,6 @@ def gaussian(x, amp, mu, sigma):
6369
for i, (p, c) in enumerate(zip(INIT_P, COLORS))
6470
]
6571

66-
# Live sum of all components — this IS the fit after pressing 'f'
67-
sum_line = plot.add_line(
68-
sum(gaussian(x, **p) for p in INIT_P), x_axis=x,
69-
color="#e0e0e0", linewidth=1.5, linestyle="dashed", label="sum",
70-
)
7172

7273
# ── GaussianComponent ──────────────────────────────────────────────────────
7374

@@ -226,11 +227,14 @@ def update(self):
226227
)
227228

228229
def fit(self):
229-
"""Least-squares fit; snaps components to the result.
230+
"""Least-squares fit; snaps components and FWHM widgets to the result.
230231
231232
Builds a generic n-Gaussian model from the component list and uses
232233
their current state as the initial guess. On success every component
233-
snaps to the fitted (amp, μ, σ) and the sum redraws as the best fit.
234+
snaps to the fitted (amp, μ, σ): the component line, the peak handle,
235+
**and** the FWHM range widget are all moved to the optimal values.
236+
If a component's widgets have not been shown yet they are created and
237+
revealed automatically. The sum line redraws as the best fit.
234238
On failure the components are left unchanged.
235239
"""
236240
n = len(self.components)

0 commit comments

Comments
 (0)