两个问题合二为一:鉴于在 Julia 中绘制的一条线,我如何才能
作为下面代码中的一个具体示例,我如何 1. 删除以前的回归线或 2. 将它们的不透明度更改为 0.1?
using Plots; gr()
f = x->.3x+.2
g = x->f(x)+.2*randn()
x = rand(2)
y = g.(x)
plt = scatter(x,y,c=:orange)
plot!(0:.1:1, f, ylim=(0,1), c=:green, alpha=.3, linewidth=10)
anim = Animation()
for i=1:200
r = rand()
x_new, y_new = r, g(r)
push!(plt, x_new, y_new)
push!(x, x_new)
push!(y, y_new)
A = hcat(fill(1., size(x)), x)
coefs = A\y
plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue) # plot new regression line
# 1. delete previous line
# 2. set alpha of previous line to .1
frame(anim)
end
gif(anim, "regression.gif", fps=5)
我试过删除组合,弹出!并删除但没有成功。
可以在此处找到 Python 中的相关问题:How to remove lines in a Matplotlib plot
最佳答案
这是一个有趣且说明性的示例,说明如何使用 pop!()
使用 Makie 在 Julia 中撤消绘图。请注意,您将看到这以与绘制所有内容相反的顺序返回(想想,就像从堆栈中添加和删除),所以 deleteat!(scene.plots, ind)
仍然需要删除特定索引处的图。
using Makie
x = range(0, stop = 2pi, length = 80)
f1(x) = sin.(x)
f2(x) = exp.(-x) .* cos.(2pi*x)
y1 = f1(x)
y2 = f2(x)
scene = lines(x, y1, color = :blue)
scatter!(scene, x, y1, color = :red, markersize = 0.1)
lines!(scene, x, y2, color = :black)
scatter!(scene, x, y2, color = :green, marker = :utriangle, markersize = 0.1)
display(scene)
sleep(10)
pop!(scene.plots)
display(scene)
sleep(10)
pop!(scene.plots)
display(scene)
您可以看到上面的图片,这些图片显示了如何使用
pop()
逐步撤消情节。 .关于sleep()
的关键思想是如果我们不使用它(并且您可以通过运行删除它的代码自行测试),由于渲染时间,屏幕上显示的第一个也是唯一的图像将是上面的最终图像。您可以查看是否运行此窗口渲染的代码,然后休眠 10 秒(以便给它时间渲染),然后使用
pop!()
退回情节。Docs for sleep()
关于plot - Julia 绘图 : delete and modify existing lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829961/