# How to fix the x axis for a histogram in Julia

**URL:** https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453
**Category:** Visualization
**Tags:** plotting, makie
**Created:** [June 8, 2022, 5:55pm UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453 "2022-06-08T17:55:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Xavier\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xavier_gonzalez/32/36033_2.png) [@Xavier\_Gonzalez](https://discourse.julialang.org/u/Xavier_Gonzalez)
#### Post date: [June 8, 2022, 5:55pm UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453/1 "2022-06-08T17:55:02Z")

</div>

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

> [@How to put a single marker on a heatmap](https://discourse.julialang.org/t/how-to-put-a-single-marker-on-a-heatmap/82447):
>
> I’m using CairoMakie to make a heatmap in Julia 1.7.3 I’d like to add a single smiley face (or really any marker) in the very center of the heatmap, at say 50 x 50. This seems like a super simple task, but I’ve been looking online and haven’t found anything. The most basic thing to do would seem to be use scatter, which I can make work if I have multiple points, but not if I only want to put one. Can anyone point me in the right direction?

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?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [June 8, 2022, 10:14pm UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453/2 "2022-06-08T22:14:07Z")

</div>

> [@Xavier\_Gonzalez](#):
>
> I get is a discussion of `xlim!` which does not seem to apply to my setting.

do you have a MWE?

---

<div class="post-metadata">

### Author: ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)
#### Post date: [June 9, 2022, 12:00am UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453/3 "2022-06-09T00:00:00Z")

</div>

Howdy again, here’s a couple ways that come to mind based on the docs [here](https://makie.juliaplots.org/v0.17.3/examples/blocks/axis/index.html#setting_axis_limits_and_reversing_axes):

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2de926d250739bdb8f347c1b64002c15bc6717b.png)

Interestingly, trying to use a [special keyword](https://makie.juliaplots.org/v0.17.3/documentation/plot_method_signatures/index.html#special_keyword_arguments) on an already existing axis fails:

```julia
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:

```julia
hist(x; axis=(; limits))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/2/82e330ca8072acacd5567cbfab36c67cc45ce9d0.png)

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](https://github.com/JuliaPlots/Makie.jl/issues/2005)

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [June 9, 2022, 12:08am UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453/4 "2022-06-09T00:08:37Z")

</div>

> [@icweaver](#):
>
> Interestingly, trying to use a [special keyword](https://makie.juliaplots.org/v0.17.3/documentation/plot_method_signatures/index.html#special_keyword_arguments) on an already existing axis fails:

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`?

---

<div class="post-metadata">

### Author: ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)
#### Post date: [June 9, 2022, 4:31am UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453/5 "2022-06-09T04:31:50Z")

</div>

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

---

<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: [June 9, 2022, 6:13am UTC](https://discourse.julialang.org/t/how-to-fix-the-x-axis-for-a-histogram-in-julia/82453/6 "2022-06-09T06:13:10Z")

</div>

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?
