# Plot with x-axis in radians with ticks at multiples of pi

**URL:** <https://discourse.julialang.org/t/plot-with-x-axis-in-radians-with-ticks-at-multiples-of-pi/65325>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [July 26, 2021, 6:00pm UTC](https://discourse.julialang.org/t/plot-with-x-axis-in-radians-with-ticks-at-multiples-of-pi/65325 "2021-07-26T18:00:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 26, 2021, 6:00pm UTC](https://discourse.julialang.org/t/plot-with-x-axis-in-radians-with-ticks-at-multiples-of-pi/65325/1 "2021-07-26T18:00:19Z")

</div>

I have a plot where the x-axis is an angle in radians. Is there an easy way to make the ticks and the tick labels be multiples of pi?

For example, I would like to obtain a figure like this:

![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae8865bd05536a9a857090ad67a5b45dc11d9ab8.png)

---

<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:** [July 26, 2021, 6:10pm UTC](https://discourse.julialang.org/t/plot-with-x-axis-in-radians-with-ticks-at-multiples-of-pi/65325/2 "2021-07-26T18:10:21Z")

</div>

You may check [this example](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/8) from _il bel paese_.

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [July 26, 2021, 7:37pm UTC](https://discourse.julialang.org/t/plot-with-x-axis-in-radians-with-ticks-at-multiples-of-pi/65325/3 "2021-07-26T19:37:56Z")

</div>

A working example with Plots.jl:

```julia
using Plots, LaTeXStrings
pyplot(fmt=:svg)

function pitick(start, stop, denom; mode=:text)
    a = Int(cld(start, π/denom))
    b = Int(fld(stop, π/denom))
    tick = range(a*π/denom, b*π/denom; step=π/denom)
    ticklabel = piticklabel.((a:b) .// denom, Val(mode))
    tick, ticklabel
end

function piticklabel(x::Rational, ::Val{:text})
    iszero(x) && return "0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return S * N * "π"
    S * N * "π/" * repr(d)
end

function piticklabel(x::Rational, ::Val{:latex})
    iszero(x) && return L"0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return L"%$S%$N\pi"
    L"%$S\frac{%$N\pi}{%$d}"
end

a, b = -2π, 2π
plot(sin, a, b; xtick=pitick(a, b, 4), label="y = sin(x)", size=(720, 250))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/0/e04b4973ffe838d006a9feea987e242bb058a7e4.jpeg)

```julia
plot(sin, a, b; xtick=pitick(a, b, 4; mode=:latex), label="y = sin(x)", size=(720, 250), tickfontsize=10)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/1/61a3005337bacaffd61b396e6a7b695d8a0d0ff4.jpeg)

Edit: LaTeX style tick labels

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 27, 2021, 8:33am UTC](https://discourse.julialang.org/t/plot-with-x-axis-in-radians-with-ticks-at-multiples-of-pi/65325/4 "2021-07-27T08:33:27Z")

</div>

A solution with Makie:

```julia
using CairoMakie

format_π_fracs(xs; denom=round(Int, π/minimum(diff(xs)))) = map(xs) do x
    # This will error if x is not an expected fraction of π
    frac = Int(round(x/(π/denom), digits=1))//denom
    sign = x < 0 ? "-" : ""
    absnum = abs(frac.num) == 1 ? "" : string(abs(frac.num))
    frac.den == 1 && return frac.num == 0 ? L"0" : L"%$sign%$(absnum)\pi"
    return L"%$sign\frac{%$(absnum)\pi}{%$(frac.den)}"
end

lines(-2π..2π, sin, figure=(; resolution=(600, 200)),
      axis=(xticks=-2π:π/2:2π, xtickformat=format_π_fracs))

```

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