# Plot Cosecant with Custom x axis (in pi)

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

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:

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.

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?

The first code produces this:

This is from the second example:

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

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.