Savefig 关闭会话

Savefig closes session

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

如果您想使用 GR 后端既保存绘图又显示它,只需反转函数的顺序即可:

    # Safe plot
    savefig("plot.png")

   # Display plot
   display(p) 

好像保存图形需要渲染它,保存后用户界面关闭。有了这个相反的顺序,一切都会按预期工作。