# Argument for number of axis ticks

**URL:** https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817
**Category:** Visualization
**Tags:** plots
**Created:** [February 20, 2022, 7:59pm UTC](https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817 "2022-02-20T19:59:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)
#### Post date: [February 20, 2022, 7:59pm UTC](https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817/1 "2022-02-20T19:59:39Z")

</div>

Is there a keyword argument to tune the number of ticks?  
How could this be done?  
I usually use `Plots` with the `GR` backend.

Often the routine finds a good number for the ticks.  
Sometimes, I’d like to tune it a bit, _e.g._ here:  
 ![Screen Shot 2022-02-20 at 2.48.03 PM](https://global.discourse-cdn.com/julialang/original/3X/c/a/ca12a3570bde0ab324857d9b9eaf63b759ae2594.png)

I am not sure how to touch this.  
It would be great if there would be a way to extend the implementation for `ticks` and not just overwrite everything brute force as with:

```nohighlight
ticks = collect(0:0.2:1)
ticklabels = [@sprintf("%5.1f",x) for x in ticks]
plot(xticks=(ticks,ticklabels))

```

Two things:

1. It would be great if the number of ticks could take the length of the numbers into account. This implies knowledge of the font size and length of the axis as well as the number range to be _ticked_.
2. A quick fix would be a semi manual argument like `plot(..., xticks = :fewer)`

Things that might relate:  
[Number of digits in xticks for Plots.jl](https://discourse.julialang.org/t/number-of-digits-in-xticks-for-plots-jl/14533)  
[time tick issus on x axis](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 20, 2022, 9:59pm UTC](https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817/2 "2022-02-20T21:59:18Z")

</div>

There are a number of knobs to turn in Plots.jl. For instance, you could use the argument `formatter` and an adequate font size:

```julia
using Formatting, Plots; gr()
x = -6:0.01:6
plot(x, sin, formatter = x -> format(x, precision=2), tickfontsize=10)

```

**NB:** there are also individual `xformatter` and `yformatter`

---

<div class="post-metadata">

### Author: ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)
#### Post date: [February 21, 2022, 10:56am UTC](https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817/3 "2022-02-21T10:56:55Z")

</div>

Thanks, `using Formatting` was new to me!

---

<div class="post-metadata">

### Author: ![parb](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@parb](https://discourse.julialang.org/u/parb)
#### Post date: [March 14, 2024, 11:51am UTC](https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817/4 "2024-03-14T11:51:06Z")

</div>

I run in to this a lot: many of my axes have too many y ticks on them (\>6). Would love to have an axis option to set the number of ticks, usually to 4 or 5 instead.

I often end up simply halving the number with a line like below, but this doesn’t always keep the tick for 0.

```julia
p = plot(...)
plot!(p[2], yticks=yticks(p[2])[1][1:2:end])

```

Cheers.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 14, 2024, 4:15pm UTC](https://discourse.julialang.org/t/argument-for-number-of-axis-ticks/76817/5 "2024-03-14T16:15:30Z")

</div>

Note that the `plot()` keyword `xrot` rotates the labels and alleviates this problem.

Regarding your workaround:

> [@parb](#):
>
> but this doesn’t always keep the tick for 0.

You could define a helper function to take care of decimating the x- or y-axis labels.  
For example:

```julia
function decimate_ticks(p, axis)
    zt = axis == :x ? only(xticks(p)) : only(yticks(p))
    n, k = length(zt[1]), findfirst(≈(0), zt[1])
    ix = isnothing(k) ? (1:2:n) : sort([k:-2:1; (k+2):2:n])
    return (zt[1][ix], zt[2][ix])
end

x = -8:9
p = plot(x, x .^4)
plot!(p, xticks=decimate_ticks(p, :x), yticks=decimate_ticks(p,:y))

```
