# Makie Observables

**URL:** <https://discourse.julialang.org/t/makie-observables/69796>\
**Category:** New to Julia\
**Tags:** glmakie\
**Created:** [October 15, 2021, 2:22am UTC](https://discourse.julialang.org/t/makie-observables/69796 "2021-10-15T02:22:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)\
**Post date:** [October 15, 2021, 2:22am UTC](https://discourse.julialang.org/t/makie-observables/69796/1 "2021-10-15T02:22:30Z")

</div>

Hi,  
I’m trying to use an interval slider to update xlims on a series plot but the xlims! function is not being triggered when the observables are being changed.

```nohighlight
EndDate = Observable(500.0)
DateRange_is = IntervalSlider(fig[4,1],range = (0:100:600), startvalues=(100,500))
LabelText = lift(DateRange_is.interval) do int
    string(int[1]," , ",int[2])
    println("slider changed to ",int[1]," - ",int[2])
    StartDate[] = int[1]
    EndDate[] = int[2]
end
xlims!(Price_ax,StartDate[],EndDate[])

```

I get no errors but the xlims of the plot dont get updated unless I re-execute  
`xlims!(Price_ax,StartDate[],EndDate[])` I thought this line would automatically update the plot because I used observables as parameters.

The values StartDate and EndDate are getting updated correctly when the slider is moved but concerningly I noticed in the object browser that there are no listeners for the observables and that  
If I change the xlims command to `xlims!(Price_ax,StartDate,EndDate)` I just get a type error…  
ERROR: MethodError: Cannot `convert` an object of type Observable{Float64} to an object of type Float32

Can anyone tell me if my use case is appropriate and perhaps give me pointers to examples that use widgets and observables to trigger commands like xlims.

Thanks  
Steve  
Julia 1.6.2  
Julia ext 1.4.3  
vscode 1.61  
Windows 10

---

<div class="post-metadata">

**Author:** ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)\
**Post date:** [October 15, 2021, 6:41am UTC](https://discourse.julialang.org/t/makie-observables/69796/2 "2021-10-15T06:41:59Z")

</div>

Hi Steve,

To listen to observables use `on` for one observable or `orany` for multiple

```julia
onany(StartDate, EndDate) do start_date, end_date
    xlims!(Price_ax, start_date, end_date)
end

```

This is now listening for when the observable changes, and then passing the values into `xlims` with either of `StartDate` or `EndDate` changes, and you should see there are listeners for both observables.

Also if you use backticks around code in discourse posts its much easier to read! See

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<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:** [October 15, 2021, 6:58am UTC](https://discourse.julialang.org/t/makie-observables/69796/3 "2021-10-15T06:58:56Z")

</div>

You can just call `xlims!` in your existing lift of `DateRange_is.interval`.

What you did in `xlims!(Price_ax,StartDate[], EndDate[])` was extracting the plain `Int` values from your two observables and feed them into the function, that doesn’t do any Observable magic. Functions have to be explicitly written to handle observables, and `xlims!` is not such a function, it doesn’t do any connections on its own. The functions that do that are almost exclusively plotting functions such as `lines`, `scatter`, etc.

---

<div class="post-metadata">

**Author:** ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)\
**Post date:** [October 15, 2021, 8:11am UTC](https://discourse.julialang.org/t/makie-observables/69796/4 "2021-10-15T08:11:30Z")

</div>

Thanks Max, that gives me a much better understanding of how to setup the listeners. Sorry about the format, fixed now.

---

<div class="post-metadata">

**Author:** ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)\
**Post date:** [October 15, 2021, 8:14am UTC](https://discourse.julialang.org/t/makie-observables/69796/5 "2021-10-15T08:14:12Z")

</div>

Thanks Jules, that makes sense. Is there some information on what methods support Observables or do I need to learn how to read the source files?  
Thanks  
Steve

---

<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:** [October 15, 2021, 8:27am UTC](https://discourse.julialang.org/t/makie-observables/69796/6 "2021-10-15T08:27:02Z")

</div>

Not really, but the plotting functions all do, input arguments and attributes. The rest usually doesn’t, unless specified in the docstring. Usually functions that do an action, like changing the x limits, will not take an observable. But you can set up your own observables that call such functions.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [March 6, 2023, 5:19pm UTC](https://discourse.julialang.org/t/makie-observables/69796/7 "2023-03-06T17:19:05Z")

</div>

Here is my proposal to adopt the y-axis according to the maximum value of a bar-plot:

```julia
# ----------------------------------------------------------------------------------------------------------------------------- #
# --- Reset button that resets all sliders inside a SliderGrid: #
# ----------------------------------------------------------------------------------------------------------------------------- #
using GLMakie

fig = Figure()
# --- figure set-up: two panes side by side, right one with two sub-panes: 
# --- left pane:
ax = Axis(fig[1, 1]) 
ylims!(ax, 0, 30)

# --- right pane contains two sub-panes: [1,1]: top, [2,1]: bottom
# --- SliderGrid: right pane, top sub-pane:
sg = SliderGrid( fig[1, 2][1, 1], 
    (; label = "Voltage", range = 0:0.1:10, format = "{:.1f}V", startvalue = 5.3),
    (; label = "Current", range = 0:0.1:20, format = "{:.1f}A", startvalue = 10.2),
    (; label = "Resistance", range = 0:0.1:50, format = "{:.1f}Ω", startvalue = 15.9),
    width = 350,
    tellheight = false,
    )

# --- Button: right pane, bottom sub-pane:
bt = Button(fig[1, 2][2, 1]; label = "reset", tellheight = false, 
        strokecolor = RGBf(0.94, 0.14, 0.24), strokewidth = 4)

# --- access elements inside slidergrid, each element is of type "Observable{Any}(Float64)"
# --- variable sliderobservables is a vector of "Observable{Any}(Float64)"
sliderobservables = [s.value for s in sg.sliders]

# --- build a vector of type "Observable" containing the value of each slider inside slider grid:
bars = lift(sliderobservables...) do slvalues...
    [slvalues...]
end
# --- build a scaler of type "Observable" containing the max y-value inside the bar-plot:
y_range_upper = lift(sliderobservables...) do slvalues...
    maximum([slvalues...])
end

# --- reset all sliders inside SliderGrid "sg"
on(bt.clicks) do n # n = number of clicks
    for i_slider in sg.sliders
        set_close_to!(i_slider, i_slider.startvalue[])
        ylims!(ax, 0, 30)
    end
end

# --- listen to observable "y_range_upper"
on(y_range_upper) do _yaxis_upper
    ylims!(ax, 0, max(30, round(Int, 1.2 * _yaxis_upper)))
    # println("y_range_upper: ", _yaxis_upper)
end
barplot!(ax, bars, color = [:yellow, :orange, :red])

display(fig)

```
