如何在不关闭 Julia 环境的情况下清理 Plots (GR)
How to clean Plots (GR) without closing Julia environment
我正在调试脚本(使用 Plots.jl
和 GKS QtTerm
后端)。所以我运行脚本很多次了。当我从像 bash> julia pointPlacement.jl
这样的终端 运行 时,初始化 Julia 和 Plots.jl 需要很长时间(与 python 相比,这是一个很大的不便)。因此,我宁愿让 Julia 保持打开状态,并从内部 运行 脚本,比如 julia> include( "pointPlacement.jl" )
grid = [ [ix*0.01 iy*0.01] for ix=1:100, iy=1:100 ]
grid = vcat(ps...)
centers = hexGrid( 2, 0.2 )
using Plots
display(scatter!( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))
问题是剧情越积越多。这是在 9 运行 秒之后。应该只有 2 个数据集,而不是 18 个:
我想关闭(杀死,摧毁)他们
如果我像这样删除 !
,它会有所帮助
display(scatter( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))
但是,我仍然担心一些垃圾(以前的数字)仍然分配在内存中,并且在我 运行 脚本 100 倍之后 Julia 会崩溃。因此,我想调用一些函数,例如 clear()
、flush()
、closeAll()
... 或其他东西 ... 每次我 运行 脚本
删除 !
会产生您想要的效果 - 如果您再次调用 scatter
,情节就会消失,并且它不会存在于后台的某个地方。
如果需要,您可以将绘图存储在变量中并覆盖它 "to be safe",即
p = scatter(...)
scatter!(p, ...)
,其中 ...
是您的策划论点。这将在每个 include
.
上明确覆盖 p
这是对 crstnbr 出色回答的长评论。如果你想制作一个新的情节,但它与以前的情节有相似之处,你可以在函数中定义一个“canvas”(因为想要一个更好的词),我将其命名为 new_plot()
,并重复使用 canvas。如果你有很长的标签和标题,你不想在绘图之间复制,这将特别有用:
using Plots
function new_plot()
plot(xlabel = "x", ylabel = "f(x)",
xlims = (0,Inf), ylims = (-Inf, 1))
end
p = new_plot()
plot!(p, x -> x^2, 0, 1)
plot!(p, x -> x^3, 0, 1)
p = new_plot()
plot!(p, x -> 2^x, 0, 1)
plot!(p, x -> 3^x, 0, 1)
编辑: 不再与 OP 直接相关,但请注意,正如 Benoît Pasquier 在评论中指出的那样,您可以设置默认选项:
default(xlabel = "x", ylabel = "f(x)",
xlims = (0,:auto), ylims = (:auto, 1))
但是您仍然需要创建一个新图来“覆盖”之前的图,如 crstnbr 所解释的那样。考虑一下:
using Plots
p = plot()
for i in 1:5
plot!(p, x -> x^i, 0, 1)
end
什么都没发生?现在试试这个:
p # after a loop, you must call the plot object to display it
我正在调试脚本(使用 Plots.jl
和 GKS QtTerm
后端)。所以我运行脚本很多次了。当我从像 bash> julia pointPlacement.jl
这样的终端 运行 时,初始化 Julia 和 Plots.jl 需要很长时间(与 python 相比,这是一个很大的不便)。因此,我宁愿让 Julia 保持打开状态,并从内部 运行 脚本,比如 julia> include( "pointPlacement.jl" )
grid = [ [ix*0.01 iy*0.01] for ix=1:100, iy=1:100 ]
grid = vcat(ps...)
centers = hexGrid( 2, 0.2 )
using Plots
display(scatter!( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))
问题是剧情越积越多。这是在 9 运行 秒之后。应该只有 2 个数据集,而不是 18 个:
我想关闭(杀死,摧毁)他们
如果我像这样删除 !
,它会有所帮助
display(scatter( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))
但是,我仍然担心一些垃圾(以前的数字)仍然分配在内存中,并且在我 运行 脚本 100 倍之后 Julia 会崩溃。因此,我想调用一些函数,例如 clear()
、flush()
、closeAll()
... 或其他东西 ... 每次我 运行 脚本
删除 !
会产生您想要的效果 - 如果您再次调用 scatter
,情节就会消失,并且它不会存在于后台的某个地方。
如果需要,您可以将绘图存储在变量中并覆盖它 "to be safe",即
p = scatter(...)
scatter!(p, ...)
,其中 ...
是您的策划论点。这将在每个 include
.
p
这是对 crstnbr 出色回答的长评论。如果你想制作一个新的情节,但它与以前的情节有相似之处,你可以在函数中定义一个“canvas”(因为想要一个更好的词),我将其命名为 new_plot()
,并重复使用 canvas。如果你有很长的标签和标题,你不想在绘图之间复制,这将特别有用:
using Plots
function new_plot()
plot(xlabel = "x", ylabel = "f(x)",
xlims = (0,Inf), ylims = (-Inf, 1))
end
p = new_plot()
plot!(p, x -> x^2, 0, 1)
plot!(p, x -> x^3, 0, 1)
p = new_plot()
plot!(p, x -> 2^x, 0, 1)
plot!(p, x -> 3^x, 0, 1)
编辑: 不再与 OP 直接相关,但请注意,正如 Benoît Pasquier 在评论中指出的那样,您可以设置默认选项:
default(xlabel = "x", ylabel = "f(x)",
xlims = (0,:auto), ylims = (:auto, 1))
但是您仍然需要创建一个新图来“覆盖”之前的图,如 crstnbr 所解释的那样。考虑一下:
using Plots
p = plot()
for i in 1:5
plot!(p, x -> x^i, 0, 1)
end
什么都没发生?现在试试这个:
p # after a loop, you must call the plot object to display it