gpt4 book ai didi

julia - Savefig 关闭 session

转载 作者:行者123 更新时间:2023-12-04 08:52:49 27 4
gpt4 key购买 nike

使用 LsqFit
使用绘图

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 session 并使用修订包含上述内容并调用 run(),我会看到绘图窗口立即打开和关闭。如果我取消注释 savefig() 它保持打开状态。我对这种行为有点困惑。

最佳答案

如果您想保存绘图并使用 GR 后端显示它,只需颠倒函数的顺序,它应该是:

    # Safe plot
savefig("plot.png")

# Display plot
display(p)
似乎保存图形需要渲染它,保存后用户界面会关闭。使用这种相反的顺序,一切都会按预期进行。

关于julia - Savefig 关闭 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64012683/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com