# Plot Cosecant with Custom x axis (in pi)

**URL:** <https://discourse.julialang.org/t/plot-cosecant-with-custom-x-axis-in-pi/90491>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 19, 2022, 11:23am UTC](https://discourse.julialang.org/t/plot-cosecant-with-custom-x-axis-in-pi/90491 "2022-11-19T11:23:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 19, 2022, 11:23am UTC](https://discourse.julialang.org/t/plot-cosecant-with-custom-x-axis-in-pi/90491/1 "2022-11-19T11:23:21Z")

</div>

Hi all I want to plot Cosecant function with the x axis in pi. But the function can’t be plot correctly:

```julia
using Plots, LaTeXStrings
gr()

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π

f(x) = csc(x)

plot(f, a, b; xtick=pitick(a, b, 4; mode=:latex), framestyle=:zerolines,
	legend=:outerright, label=L"f(x) = x + 2 \cos \ x", 
	size=(720, 360), tickfontsize=10)

```

I compare it with my code that plot Cosecant better looking here:

```julia
using Plots, LaTeXStrings, MTH229
gr()

f(x) = csc(x)
               
plot(f, -2π, 2π, ylims=(-3,2),
    label=L"C(\theta) = \csc (\theta)", framestyle=:zerolines,
     legend=:outerright)

scatter!([-1], [f(-1)], color = "red1", label="", markersize = 3)
scatter!([1], [f(1)], color = "red1", label="", markersize = 3)

annotate!([(-1,-0.87, (L"A", 8, :black))])
annotate!([(1,1.2, (L"B", 8, :black))])

```

So why would the first code can’t plot Cosecant like the second code? I just want to make the x-axis in pi terms.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 19, 2022, 11:42am UTC](https://discourse.julialang.org/t/plot-cosecant-with-custom-x-axis-in-pi/90491/2 "2022-11-19T11:42:07Z")

</div>

There’s a lot of noise here. Could you consider removing the parts of your example that don’t enter into it (annotations etc), and describe _what_ is wrong with the first example. Does it error? Or look wrong? Perhaps uploading a couple of pictures could clear it up.

And if your second example works, why not use that one?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 19, 2022, 11:46am UTC](https://discourse.julialang.org/t/plot-cosecant-with-custom-x-axis-in-pi/90491/3 "2022-11-19T11:46:46Z")

</div>

The first code produces this:  
 ![Capture d’écran_2022-11-19_18-46-32](https://global.discourse-cdn.com/julialang/original/3X/f/0/f039d721eb902477500f3aa1a9e6d2a3ea1f799d.png)

This is from the second example:

 ![Capture d’écran_2022-11-19_18-44-45](https://global.discourse-cdn.com/julialang/original/3X/f/4/f46f48d01cf9d557470e9478bf34b08257b977c9.png)

it works but I want to change the x axis into \pi term.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 19, 2022, 2:01pm UTC](https://discourse.julialang.org/t/plot-cosecant-with-custom-x-axis-in-pi/90491/4 "2022-11-19T14:01:30Z")

</div>

In your second example you set the `ylims`. If you did that in the first, it would probably look more like what you want.

This is one of the big advantages of MWE:s. If there’s too many components it can be difficult to tell when something is missing.
