How to create a plot with slider bars

I have a function that creates plots after taking in two arguments

function plotLaneEmden(log_delta_xi=-4, n=3)    
    plot(n,  1 .- log_delta_xi.^2/6,  linecolor = :green, label="n = $n")
    
    xlabel!("ξ")
    ylabel!("θ")

end

I would like to make the plot more interactive by adding sliders to change n and log_delta_xi. In Python, this can be done with ipywidgets:

from ipywidgets import interact, interactive, fixed, interact_manual, FloatSlider
import ipywidgets as widgets

interact(plotLaneEmden, 
         n=FloatSlider(min=0, max=4.99, step=0.01, value=3, continuous_update=False),
         log_delta_xi=FloatSlider(min=-6, max=0.1, step=0.01, value=-2, continuous_update=False));

Is there an analogous way to do this in julia?

with Makie.
This case is just the first example here:
https://makie.juliaplots.org/stable/examples/layoutables/slider/index.html#slider

1 Like

@lazarusA Going off the first example, when I run the following code:

function plotLaneEmden(log_delta_xi=-4, n=3)    

    fig = Figure()

    ax = Axis(fig[1, 1])
 
    sl_x = Slider(fig[2, 1], range = 0:0.01:4.99, startvalue = 3)
    sl_y = Slider(fig[1, 2], range = -6:0.01:0.1, horizontal = false, startvalue = -2)

    point = lift(sl_x.value, sl_y.value) do n, log_delta_xi
        Point2f(n, log_delta_xi)
    end

    plot(n,  1 .- log_delta_xi.^2/6,  linecolor = :green, label="n = $n")
    
    xlabel!("ξ")
    ylabel!("θ")

end

The plot does not have sliders. Instead, I have the error, UndefVarError: plot not defined. What am I missing here?

If you don’t mind your plot being displayed inside a web page, you can use Pluto with PlutoUI. It’s quite simple, and works with any plotting package. I have a live demonstration of an interactive plot made with Pluto at ⚡ Pluto.jl ⚡