# Determining xlims

**URL:** <https://discourse.julialang.org/t/determining-xlims/72921>\
**Category:** General Usage\
**Tags:** glmakie\
**Created:** [December 11, 2021, 1:49am UTC](https://discourse.julialang.org/t/determining-xlims/72921 "2021-12-11T01:49:56Z")\
**Posts on this page:** 8\
**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:** [December 11, 2021, 1:49am UTC](https://discourse.julialang.org/t/determining-xlims/72921/1 "2021-12-11T01:49:56Z")

</div>

Hi,

I really like the ability of Makie to set the x and y limits automatically but sometimes I want to use the limits to fine tune my plot. Is there a way of returning the x and y limits for an axis when they have been automatically set?

Thanks  
Steve

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [December 11, 2021, 12:50pm UTC](https://discourse.julialang.org/t/determining-xlims/72921/2 "2021-12-11T12:50:16Z")

</div>

Hey Steve,

i’m not sure if there is a nicer option already but you can get them from the `Axis` internals:

```julia
using GLMakie
fig, ax, p = plot(rand(Point2, 4))

rectangle = ax.finallimits[]
xlimits = (rectangle.origin[1], rectangle.origin[1] + rectangle.widths[1])
ylimits = (rectangle.origin[2], rectangle.origin[2] + rectangle.widths[2])

```

Alternatively define a function to get them as `Observables` for the true interactive Makie experience

```julia
xlims(ax::Axis) = @lift ($(ax.finallimits).origin[1], $(ax.finallimits).origin[1] + $(ax.finallimits).widths[1])
ylims(ax::Axis) = @lift ($(ax.finallimits).origin[2], $(ax.finallimits).origin[2] + $(ax.finallimits).widths[2])

```

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 11, 2021, 9:40pm UTC](https://discourse.julialang.org/t/determining-xlims/72921/3 "2021-12-11T21:40:25Z")

</div>

Shoutout to `Plots.jl` for

```julia
using Plots
plot(sin)
xlims()
# (-5.3, 5.3)
ylims()
# (-1.0596986628031682, 1.059331996217173)
```

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [December 11, 2021, 9:48pm UTC](https://discourse.julialang.org/t/determining-xlims/72921/4 "2021-12-11T21:48:43Z")

</div>

```julia
xlims(ax::Axis) = @lift ($(ax.finallimits).origin[1], $(ax.finallimits).origin[1] + $(ax.finallimits).widths[1])
ylims(ax::Axis) = @lift ($(ax.finallimits).origin[2], $(ax.finallimits).origin[2] + $(ax.finallimits).widths[2])

xlims() = xlims(current_axis())
ylims() = ylims(current_axis())

```

😛 (`plot(sin)` still not possible in Makie though)

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 11, 2021, 9:51pm UTC](https://discourse.julialang.org/t/determining-xlims/72921/5 "2021-12-11T21:51:23Z")

</div>

Is this equivalent:

```julia
xlims(ax::Axis=current_axis()) =
    @lift ($(ax.finallimits).origin[1], $(ax.finallimits).origin[1] + $(ax.finallimits).widths[1])
ylims(ax::Axis=current_axis()) =
    @lift ($(ax.finallimits).origin[2], $(ax.finallimits).origin[2] + $(ax.finallimits).widths[2])

```

---

<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:** [December 13, 2021, 1:37am UTC](https://discourse.julialang.org/t/determining-xlims/72921/6 "2021-12-13T01:37:43Z")

</div>

Thanks hexader, that’s a great help.

Just to make sure I have this correct…

this line `xlims(ax::Axis) = @lift ($(ax.finallimits).origin[1], $(ax.finallimits).origin[1] + $(ax.finallimits).widths[1])`  
creates an observable for the finallimits  
this observable it is initialised using `xlims() = xlims(current_axis())`

so, I should then create another observable that listens for changes to xlims()?

Does this look something like

```julia
makeadjustment = lift(xlims()) do
      my code to alter the plot
end

```

Thanks  
Steve

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [December 13, 2021, 9:04am UTC](https://discourse.julialang.org/t/determining-xlims/72921/7 "2021-12-13T09:04:32Z")

</div>

Nearly. `lift` would create a new Observable `makeadjustment` which follows the changes of `xlims()`. If you want to call a specific function on change use `on`:

```julia
using GLMakie
fig, ax, p = plot(rand(Point2, 4))

xlims(ax::Axis=current_axis()) = @lift ($(ax.finallimits).origin[1], $(ax.finallimits).origin[1] + $(ax.finallimits).widths[1])
ylims(ax::Axis=current_axis()) = @lift ($(ax.finallimits).origin[2], $(ax.finallimits).origin[2] + $(ax.finallimits).widths[2])

on(xlims()) do l
    println("xlmits changed to $l")
end

xlims!(-2, 2) # prints "xlimits changed to (-2, 2)"

```

This function will be only called on change though and not on definition. So maybe you want to call `autolimits!(ax)` again after the `on` block to trigger the reaction.

---

<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:** [December 14, 2021, 2:55am UTC](https://discourse.julialang.org/t/determining-xlims/72921/8 "2021-12-14T02:55:12Z")

</div>

Thanks hexaeder, that’s crystal clear now.
