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.