# Plots grid line at every integer or half integer

**URL:** <https://discourse.julialang.org/t/plots-grid-line-at-every-integer-or-half-integer/60059>\
**Category:** Visualization\
**Created:** [April 26, 2021, 8:01pm UTC](https://discourse.julialang.org/t/plots-grid-line-at-every-integer-or-half-integer/60059 "2021-04-26T20:01:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [April 26, 2021, 8:01pm UTC](https://discourse.julialang.org/t/plots-grid-line-at-every-integer-or-half-integer/60059/1 "2021-04-26T20:01:53Z")

</div>

Is there a way to specify `Plots.jl` to have a grid line at every integer without manually listing the integers? I want grid lines based on the plot extent, without calculating the extent beforehand (since `Plots.jl` will do that).

For example, if I want to ensure ticks at all integers for the following plot:

```julia
plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/7/471d479bc47ac71fb0c1b8542dc9cf965556ed61.png)

I can do

```julia
plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0, xticks=-1000:1000)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c38d4de8cd34ade0215792221077831976e94a4.png)  
but the `xticks` argument has to be chosen to be large enough.

From my understanding of the code, it seems like the answer is no:

> <https://github.com/JuliaPlots/Plots.jl/blob/8cf268a3903f847ef3071019b3e77b99240ba9c4/src/axes.jl#L298-L313>

> <https://github.com/JuliaPlots/Plots.jl/blob/8cf268a3903f847ef3071019b3e77b99240ba9c4/src/axes.jl#L171-L192>

---

<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:** [April 26, 2021, 10:43pm UTC](https://discourse.julialang.org/t/plots-grid-line-at-every-integer-or-half-integer/60059/2 "2021-04-26T22:43:17Z")

</div>

Would this be OK:

```julia
using Plots; gr()
p1 = plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0)
plot!(xticks = round(Int,xlims(p1)[1]):round(Int,xlims(p1)[2]))

```

The advantage being that there can be a number of series plotting into p1, and the integer range of `xticks` with step one is requested in the end, based on the actual plot limits.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [April 26, 2021, 10:43pm UTC](https://discourse.julialang.org/t/plots-grid-line-at-every-integer-or-half-integer/60059/3 "2021-04-26T22:43:40Z")

</div>

How about the following?

```nohighlight
plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0, xtick=-10_000:10_1000, ytick=-10_000:10_000)

```

Instead of `10_000` you can use whatever is a sufficiently large integer for you to cover all your cases.
