Julia Plotting:删除和修改现有行 [英] Julia Plotting: delete and modify existing lines

查看:139
本文介绍了Julia Plotting:删除和修改现有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题合二为一:给定朱莉娅(Julia)画一条线,我该怎么办

Two questions in one: Given a line plotted in Julia, how can I

  1. 从情节和图例中删除它(无需清除整个情节)
  2. 更改其属性(例如颜色,厚度,不透明度)

作为下面代码中的一个具体示例,我如何1.删除以前的回归线或2.将其不透明度更改为0.1?

As a concrete example in the code below, how can I 1. delete previous regression lines OR 2. change their opacity to 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中可以找到一个相关的问题:如何删除Matplotlib图

I tried combinations of delete, pop! and remove but without success. A related question in Python can be found here: How to remove lines in a Matplotlib plot

推荐答案

以下是一个有趣且说明性的示例,说明了如何使用pop!()使用Makie撤销Julia中的绘图.请注意,您会看到这与绘制所有内容的顺序相反(例如,像从堆栈中添加和删除一样),因此,仍然需要deleteat!(scene.plots, ind)来删除特定索引处的绘图.

Here is a fun and illustrative example of how you can use pop!() to undo plotting in Julia using Makie. Note that you will see this goes back in the reverse order that everything was plotted (think, like adding and removing from a stack), so deleteat!(scene.plots, ind) will still be necessary to remove a plot at a specific index.


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()的关键思想是,如果我们不使用它(并且您可以通过删除该代码运行代码来自己进行测试),则屏幕上仅显示的拳头和拳头图像将是上方的最终图像.由于渲染时间.

You can see the images above that show how the plot progressively gets undone using pop(). The key idea with respect to sleep() is that if we were not using it (and you can test this on your own by running the code with it removed), the fist and only image shown on the screen will be the final image above because of the render time.

您可以查看是否运行了窗口渲染的代码,然后休眠10秒钟(以便给它渲染时间),然后使用pop!()后退一步.

You can see if you run this code that the window renders and then sleeps for 10 seconds (in order to give it time to render) and then uses pop!() to step back through the plot.

sleep()的文档

这篇关于Julia Plotting:删除和修改现有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆