# Plotting with user input

**URL:** https://discourse.julialang.org/t/plotting-with-user-input/112460
**Category:** New to Julia
**Tags:** question, plotting, pluto, gui, glmakie
**Created:** [April 3, 2024, 9:57am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460 "2024-04-03T09:57:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Jesse1](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jesse1](https://discourse.julialang.org/u/Jesse1)
#### Post date: [April 3, 2024, 9:57am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/1 "2024-04-03T09:57:55Z")

</div>

Hi, I am very new to Julia and am looking for advice/information on plotting.  
I am trying to visualise my model fits for multiple instances and would like to visualise the plot and then give user input, perhaps like a 0 or 1 so that after the visualisation I can easily select the fits that I deem acceptable. Is there a standard way to do this or is there perhaps another way this kind of operation is commonly done?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 3, 2024, 9:59am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/2 "2024-04-03T09:59:40Z")

</div>

Hello and welcome to the community!

What does “give some user input” mean?

---

<div class="post-metadata">

### Author: ![Jesse1](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jesse1](https://discourse.julialang.org/u/Jesse1)
#### Post date: [April 3, 2024, 10:03am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/3 "2024-04-03T10:03:55Z")

</div>

Hi thank you for responding!

As with user input I was hoping it was possible to pass a boolean is some kind of way so that I can divide the plots into two groups that I want to keep and one that I do not want

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 3, 2024, 10:11am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/4 "2024-04-03T10:11:18Z")

</div>

Do you have something like this in mind?

```julia
plot(fits)
# manually inspect the result
acceptable_fits = [1, 2, 3, 5, 8]
plot(fits[acceptable_fits])

```

?

---

<div class="post-metadata">

### Author: ![Jesse1](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jesse1](https://discourse.julialang.org/u/Jesse1)
#### Post date: [April 3, 2024, 10:24am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/5 "2024-04-03T10:24:44Z")

</div>

Yes something like that, where if possible I would like to loop through plotting all the fits and give a boolean which is stored and then it presents me with the next plot. so something like

```julia
order = []
for i in 1:number_fits
    model = models[i]
    parameters = parameter_sets[i]

    fit = output(model, parameters)
    plot(fit)

    u = user_input()
    push!(order,u)
end

```

I just have no idea if doing this with a user\_input is at all possible.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 3, 2024, 10:26am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/6 "2024-04-03T10:26:44Z")

</div>

are you looking to accept user input from the REPL in the loop there? You could do this with

```julia
help?> readline
search: readline readlines readlink

  readline(io::IO=stdin; keep::Bool=false)
  readline(filename::AbstractString; keep::Bool=false)

  Read a single line of text from the given I/O stream or file (defaults to stdin). When reading from a file, the text is assumed to be encoded in UTF-8. Lines
  in the input end with '\n' or "\r\n" or the end of an input stream. When keep is false (as it is by default), these trailing newline characters are removed
  from the line before it is returned. When keep is true, they are returned as part of the line.

```

together with `parse(Int, user_input)` or `parse(Bool, user_input)` where `user_input` is the string returned by `readline`

```julia-repl
julia> user_input = readline()
true # I typed this
"true"

julia> parse(Bool, user_input)
true

```

---

<div class="post-metadata">

### Author: ![Guillawme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillawme/32/30025_2.png) [@Guillawme](https://discourse.julialang.org/u/Guillawme)
#### Post date: [April 3, 2024, 10:33am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/7 "2024-04-03T10:33:28Z")

</div>

Hi! This looks like an ideal use case for a [Pluto](https://plutojl.org/) notebook. Here is [a notebook I made](https://guillawme.github.io/julia-curve-fitting/curve-fitting.jl.html) for a similar idea (source in [this repo](https://github.com/Guillawme/julia-curve-fitting)).

Take a look at the [PlutoUI package](https://github.com/JuliaPluto/PlutoUI.jl): it facilitates using UI elements like dropdown menus, checkboxes, radio buttons, text fields, sliders, etc. ([demo here](https://featured.plutojl.org/basic/plutoui.jl)). In your case, you could have a multicheckbox for your fits and collect the boolean values of all checkboxes to select what you want to plot.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 3, 2024, 11:03am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/8 "2024-04-03T11:03:30Z")

</div>

It can also be fun to do something like this with a small GLMakie GUI. Here’s a mockup where the “fit” is just a line from first to last point of random data, but you get the gist. Afterwards you have your acceptance results in the `accepted` `Observable` and can process that further.

```julia
using GLMakie

struct Fit
    y::Vector{Float64}
end

n = 100
fits = [Fit(cumsum(randn(n))) for _ in 1:n]
accepted = Observable(zeros(Bool, length(fits)))

f = Figure()
ax = Axis(f[1, 1])

i = Observable(1)

lines!(ax, @lift(fits[$i].y))

pseudofit_points = lift(i) do i
    f = fits[i]
    len = length(f.y)
    [Point2(1, f.y[1]), Point2(len, f.y[end])]
end

lines!(ax, pseudofit_points, color = @lift($accepted[$i] ? :lightgreen : :red), linestyle = :dash, linewidth = 3)

subgl = GridLayout(f[2, 1], tellwidth = false)

prevbutton = Button(subgl[1, 1], label = "Prev")
on(prevbutton.clicks) do _
    i[] = mod1(i[] - 1, n)
    reset_limits!(ax)
end

Label(subgl[1, 2], @lift("Fit $($i)"))

nextbutton = Button(subgl[1, 3], label = "Next")
on(nextbutton.clicks) do _
    i[] = mod1(i[] + 1, n)
    reset_limits!(ax)
end

togglebutton = Button(subgl[1, 4], label = @lift($accepted[$i] ? "Reject" : "Accept"))
on(togglebutton.clicks) do _
    accepted.val[i[]] =! accepted.val[i[]]
    notify(accepted)
end

f

```

---

<div class="post-metadata">

### Author: ![Jesse1](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jesse1](https://discourse.julialang.org/u/Jesse1)
#### Post date: [April 8, 2024, 10:01am UTC](https://discourse.julialang.org/t/plotting-with-user-input/112460/9 "2024-04-08T10:01:43Z")

</div>

Thank you everyone for responding and giving ideas!

I ended up using the GLMakie method suggested by Jules as I was using Makie for other plotting aswell and it worked very nicely!
