Change Location of Label in Plots + The xtick axis are not showing fully

Hi all,

I am plotting sine waves, but there are problems:

  1. xtick shows half the number of denominator, e.g. 5 \pi / 4 (the 4 is barely seen)
  2. The label placement can it be moved above or at the bottom of the whole graph? so we can see the sine waves clearly.

This is the code:


using Plots, LaTeXStrings
gr(fmt=:svg)

f(x) = sin.(5*x + 30)
g(x) = sin.(5*x + 60)
h(x) = sin.(5*x + 135)
#plot([f g]; labels=["sin" "cos"], color=["red" "black"])

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 t", size=(720, 250))
#plot(cos, a, b; xtick=pitick(a, b, 4), label="y = cos t", size=(720, 250))

plot(f, a, b; xtick=pitick(a, b, 4; mode=:latex), label=L"y = \sin{\omega t + 30}", size=(720, 250), tickfontsize=10)
plot!(g, a, b; xtick=pitick(a, b, 4; mode=:latex), label=L"y = \sin{\omega t + 60}", size=(720, 250), tickfontsize=10)
plot!(h, a, b; xtick=pitick(a, b, 4; mode=:latex), label=L"y = \sin{\omega t + 135}", size=(720, 250), tickfontsize=10)

Thank You

You probably want bottom_margin=10Plots.mm and legend=:outertopright

1 Like