How to fix the x axis for a histogram in Julia

I’m trying to make a histogram in Julia using hist command from Makie, as detailed here:

I would like to control the xlimits, i.e. force the x range to go from 0 to 5.

I can’t seem to find anywhere how to do this online, the closest I get is a discussion of xlim! which does not seem to apply to my setting.

Can anyone point me in the right direction?

do you have a MWE?

Howdy again, here’s a couple ways that come to mind based on the docs here:

using CairoMakie

fig = Figure()
limits = (-0.75, 0.75, nothing, nothing)
ax_left = Axis(fig[1, 1]; title="Original data")
ax_middle = Axis(fig[1, 2]; limits, title="Axis keyword")
ax_right = Axis(fig[1, 3]; title="xlims!")

x = randn(1000)

# Regular
hist!(ax_left, x)

# Zoomed with Axis setup above
hist!(ax_middle, x)

# Zoomed with xlims!
hist!(ax_right, x)
xlims!(-0.75, 0.75)

fig

Interestingly, trying to use a special keyword on an already existing axis fails:

hist!(ax_right, x; axis=(; limits))
# Error: setting axis for scene via plot attribute not supported anymore

but creating it all in one shot is still fine:

hist(x; axis=(; limits))

I think this may be related to this issue? Wrong error when trying to set axis elements in `hist!` call · Issue #2005 · JuliaPlots/Makie.jl · GitHub

it sounds like they deprecated this usage

it makes sense that the one-shot version works, because in one-shot, you can’t have conflicting axis setting, where here if you !() incompatible axis setting, what’s the overriding rules? merging? replace entirely with the last called axis?

That’s a good point. I didn’t think about how the statefulness would come into play

Yes that’s exactly the reason, you can only pass attributes when an Axis is created. I don’t know how it works in Plots.jl, do later calls merge with previous attributes and override ones that have been set twice?