Julia: "Plot not defined" when attempting to add slider bars

Viewed 195

I am learning how to create plots with slider bars. Here is my code based off the first example of this tutorial

using Plots
gr()
using GLMakie

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

plotLaneEmden()

When I run this, it gives UndefVarError: plot not defined. What am I missing here?

1 Answers

It looks like you are trying to mix and match Plots.jl and Makie.jl. Specifically, the example from your link is entirely for Makie (specifically, with the GLMakie backend), while the the plot function you are trying to add uses syntax specific to the Plots.jl version of plot (specifically including linecolor and label keyword arguments).

Plots.jl and Makie.jl are two separate and unrelated plotting libraries, so you have to pick one and stick with it. Since both libraries export some of the same function names, using both at once will lead to ambiguity and UndefVarErrors if not disambiguated.

The other potential problem is that it looks like you are trying to make a line plot with only a single x and y value (n and log_delta_xi are both single numbers in your code as written). If that's what you want, you'll need a scatter plot instead of a line plot; and if that's not what you want you'll need to make those variables vectors instead somehow.

Depending on what exactly you want, you might try something more along the lines of (in a new session, using only Makie and not Plots):

using GLMakie

function plotLaneEmden(log_delta_xi=-4, n=3)
  fig = Figure()
  
  ax = Axis(fig[1, 1], xlabel="ξ", ylabel="θ")
  
  sl_x = Slider(fig[2, 1], range = 0:0.01:4.99, startvalue = n)
  sl_y = Slider(fig[1, 2], range = -6:0.01:0.1, horizontal = false, startvalue = log_delta_xi)
  
  point = lift(sl_x.value, sl_y.value) do n, log_delta_xi
      Point2f(n, 1 - log_delta_xi^2/6)
  end
  
  sca = scatter!(point,  color = :green, markersize = 20)
  
  axislegend(ax, [sca], ["n = $n"])  
  fig
end

plotLaneEmden()

Or, below, a simple example for interactively plotting a line rather than a point:

using GLMakie

function quadraticsliders(x=-5:0.01:5)
  fig = Figure()

  ax = Axis(fig[1, 1], xlabel="X", ylabel="Y")

  sl_a = Slider(fig[2, 1], range = -3:0.01:3, startvalue = 0.)
  sl_b = Slider(fig[1, 2], range = -3:0.01:3, horizontal = false, startvalue = 0.)

  points = lift(sl_a.value, sl_b.value) do a, b
      Point2f.(x, a.*x.^2 .+ b.*x)
  end

  l = lines!(points, color = :blue)

  onany((a,b)->axislegend(ax, [l], ["$(a)x² + $(b)x"]), sl_a.value, sl_b.value)

  limits!(ax, minimum(x), maximum(x), -10, 10)

  fig
end

quadraticsliders()

Makie.jl sliders for a quadratic equation

ETA: A couple examples closer to what you might be looking for

Related