Julia Plotting: delete and modify existing lines

前端 未结 3 1961
孤街浪徒
孤街浪徒 2021-01-13 01:31

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

  1. delete it from the plot and legend (without clearing the whole plot)
  2. change its propert
相关标签:
3条回答
  • 2021-01-13 02:20

    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)
    
    

    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.

    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.

    Docs for sleep()

    0 讨论(0)
  • 2021-01-13 02:25

    You cannot do that with the Plots package. Even the "cheating" method in the answer by Pei Huang will end up with the whole frame getting redrawn. You can do this with Makie, though - in fact the ability to interactively change plots was one of the reasons for creating that package (point 1 here http://makie.juliaplots.org/dev/why-makie.html) Not sure about the other popular plotting packages for Julia.

    0 讨论(0)
  • 2021-01-13 02:25

    I have to say that I don't know what the formal way is to accomplish them.

    There is a cheating method.

    plt.series_list stores all the plots (line, scatter...).
    If you have 200 lines in the plot, then length(plt.series_list) will be 200.

    plt.series_list[1].plotattributes returns a dictionary containing attributes for the first line(or scatter plot, depends on the order).

    One of the attributes is :linealpha, and we can use it to modify the transparency of a line or let it disappear.

    # your code ...
    
    plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue)  # plot new regression line
    
    # modify the alpha value of the previous line
    if i > 1
        plt.series_list[end-1][:linealpha] = 0.1
    end
    
    # make the previous line invisible
    if i > 2
        plt.series_list[end-2][:linealpha] = 0.0
    end
    
    frame(anim)
    # your code ...
    
    0 讨论(0)
提交回复
热议问题