# PGFPlotsX.jl - xtick distance/range/count for histograms?

**URL:** https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311
**Category:** Visualization
**Tags:** pgfplotsx
**Created:** [August 8, 2019, 1:31pm UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311 "2019-08-08T13:31:17Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [August 8, 2019, 1:31pm UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/1 "2019-08-08T13:31:17Z")

</div>

This is more a general question: I have to deal with lots of histograms and I have problems setting the xticks when the option `ybar interval` is set, since `xtick distance = {NUMBER}` seems to be ignored by `pgfplots`.

To be more specific:

```julia
using PGFPlotsX
using StatsBase

figure = @pgf Axis(
    {
        "ybar interval",
        xmajorgrids = false,
        xtick_distance="{10}",
    },
    Plot(Table(fit(Histogram, rand(-100:100, 1000), -50:50)))
)

```

produces  
 ![hist1](https://global.discourse-cdn.com/julialang/original/3X/6/9/69a6132e17a7948cf529e23d5eae04c1d3b0104d.png)

without restricting the range, it’s still ignored but at least it’s readable:

```julia
figure = @pgf Axis(
    {
        "ybar interval",
        xmajorgrids = false,
        xtick_distance="{10}",
    },
    Plot(Table(fit(Histogram, rand(-100:100, 1000))))#, -50:50)))
)

```

![hist2](https://global.discourse-cdn.com/julialang/original/3X/a/b/abf5babbdd8f95f07689f28d684c3e88130ac258.png)  
Whereas it works when I don’t use the option `ybar interval` (of course then I get a line plot instead).

```julia
figure = @pgf Axis(
    {
        #"ybar interval",
        xmajorgrids = false,
        xtick_distance="{10}",
    },
    Plot(Table(fit(Histogram, rand(-100:100, 1000))))#, -50:50)))
)

```

![hist3](https://global.discourse-cdn.com/julialang/original/3X/d/9/d917a5d31193011ff7119d278be174d51719a6cc.png)

Does anyone know how to do it without hardcoding the whole range with e.g. `xtick={...}`? I’d like to either set the distance between ticks or the number of ticks

---

<div class="post-metadata">

### Author: ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)
#### Post date: [August 8, 2019, 2:15pm UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/2 "2019-08-08T14:15:50Z")

</div>

Maybe try passing a LinRange / Range object?

---

<div class="post-metadata">

### Author: ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)
#### Post date: [August 8, 2019, 2:42pm UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/3 "2019-08-08T14:42:09Z")

</div>

I don’t know how to do this without hardcoding `xtick`, but I also don’t really see a problem with this approach, e.g.

```julia
figure = @pgf Axis(
           {
               "ybar interval",
               xmajorgrids = false,
               x_tick_label_as_interval=false,
               xtick=-50:25:50
           },
           Plot(Table(fit(Histogram, rand(-100:100, 1000), -50:50)))
       )

```

Is this a no-go for you?

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [August 23, 2019, 5:59am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/4 "2019-08-23T05:59:29Z")

</div>

Sorry @jbrea I lost this thread. Yes it’s an alternative which I can live with…

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [August 23, 2019, 6:25am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/5 "2019-08-23T06:25:05Z")

</div>

Still I am confused why such basic things are not working, so I guess I am not grasping how to do it correctly.

Given a 1D dataset, I’d like to simply plot a histogram and define the xtick distance by a single value, instead I have to do specify a range:

```julia
data = randn(1000) .* 10

@pgf Axis(
    {
        "ybar interval",
        xtick=-50:10:50,
        #xtick_distance="{10}", # this has no effect whatsoever
        xmajorgrids = false,
        x_tick_label_as_interval=false
    },
    Plot(Table(StatsBase.fit(Histogram, data, nbins=100)))
)

```

This means that for every histogram I plot, I need to provide a range which is at least as large as the dataset span and then pass that `range` (`-50:10:50`) to it, while the only relevant number in this case is `10`. I’d rather prefer passing the tick distance (10) somehow 🤔

![59](https://global.discourse-cdn.com/julialang/original/3X/0/4/04fd698616c4d8f649fb488f10f7abc8cf9f78e8.png)

With `Plots.jl` the defaults were already pretty good:

![53](https://global.discourse-cdn.com/julialang/original/3X/2/8/28f693514871726e05c5fa154e02bf4d6edc69cd.png)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 23, 2019, 8:02am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/6 "2019-08-23T08:02:35Z")

</div>

AFAIK it is a feature of `ybar interval` to add bin labels to every bin. You can fiddle around with the styling, but IMO the simplest workaround is the one you found.

We could include a utility like [`pretty`](https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/pretty) in R to make a `-50:10:50` from a bunch of random points. I wonder if some package has this already?

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [August 23, 2019, 8:13am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/7 "2019-08-23T08:13:22Z")

</div>

Ah I understand, it just happened that I have not encountered such a situation with `ybar` yet. Indeed with `ybar` it works fine.

`pretty` sounds useful but not sure where to feed this in. Especially when we overlay multiple histograms for example… The data need to be analysed before creating the `Axis` options, if I understand the mechanics correctly. Or modify them afterwards?

Anyways, I can live with the solution above, so if I am the lonely wolf, we can just forget about it 😉

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 23, 2019, 8:18am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/8 "2019-08-23T08:18:09Z")

</div>

> [@tamasgal](#):
>
> The data need to be analysed before creating the `Axis` options, if I understand the mechanics correctly.

Yes, exactly,

But preprocessing is important with PGFPlotsX anyway, since overplotting is very costly.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 23, 2019, 8:28am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/9 "2019-08-23T08:28:41Z")

</div>

> [@tamasgal](#):
>
> so if I am the lonely wolf, we can just forget about it 😉

Also, with PGFPlotsX you are part of a wolf pack whose members are kind of obsessed about how their plots look to an extent that borders on unhealthy, so suggestions, feature requests and PRs are very welcome.

I often needed an equivalent of `pretty`. We should provide more tools for preprocessing, but I am not yet sure if they should live in PGFPlotsX or not. [PlotUtils.jl](https://github.com/JuliaPlots/PlotUtils.jl/blob/master/src/ticks.jl) has some code for this, but I need to understand it better.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [August 23, 2019, 10:26am UTC](https://discourse.julialang.org/t/pgfplotsx-jl-xtick-distance-range-count-for-histograms/27311/10 "2019-08-23T10:26:36Z")

</div>

PlotUtils is intended as a low-dependency library with useful utilities to share across plotting packages: AFAIU both Makie and Plots use it. I think just:

```julia
using PlotUtils
optimize_ticks(0.3, 12, k_min = 5, k_max = 9)

```

should give a sensible outcome. `k_min` and `k_max` are the minimum and maximum number of ticks allowed. How many ticks the algorithm finds is hard to tell in advance, but you can constrain between these two extrema (I think Plots chooses 4 and 8 for example). These two values need to be sufficiently spread apart so that the algorithm can find “strict ticks”, i.e. good ticks that do not go outside of the data. There is some discussion [here](https://github.com/JuliaPlots/PlotUtils.jl/pull/26).
