使用 LsqFit 使用 Plots
function run()
# Allocate data
x_data = [ 15.2, 19.9, 2.2, 11.8, 12.1, 18.1, 11.8, 13.4, 11.5, 0.5, 18.0, 10.2,
10.6, 13.8, 4.6, 3.8, 15.1, 15.1, 11.7, 4.2 ]
y_data = [ 0.73, 0.19, 1.54, 2.08, 0.84, 0.42, 1.77, 0.86, 1.95, 0.27, 0.39,
1.39, 1.25, 0.76, 1.99, 1.53, 0.86, 0.52, 1.54, 1.05 ]
t = LinRange(0, 20, 100)
# Plot data
p = plot(x_data, y_data, seriestype = :scatter)
# Set up model to fit data
@.model(x, p) = p[1] * ( x/p[2] ) * exp( -( x/p[2] )^p[3] )
# Initial guess
p0 = [1.5, 1.5, 1.5]
# Fit curve
fit = curve_fit(model, x_data, y_data, p0)
# Get fitting parameter
beta = coef(fit)
# Define fitted function
f(x) = beta[1] * ( x/beta[2] ) * exp( -( x/beta[2] )^beta[3] )
# Plot fitted function
k = plot!(p, t, f.(t))
# Display plot
display(p)
# Safe plot
savefig("plot.png")
end
如果我打开一个 REPL 会话并使用 Revise 包含上述内容并调用 run(),我会看到绘图窗口立即打开和关闭。如果我取消注释 savefig() 它保持打开状态。我对这种行为有点困惑。