# Check if my Plot is Right for sin(|x|)

**URL:** <https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 13, 2022, 2:03pm UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216 "2022-09-13T14:03:50Z")\
**Posts on this page:** 6\
**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:** [September 13, 2022, 2:03pm UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216/1 "2022-09-13T14:03:50Z")

</div>

Hi all,

I want to know whether my plot for \sin (|x|) is correct or not, this is the code:

```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 = -4π, 4π

f(x) = sin(x)
g(x) = sin(abs(x))

plot(f,a,b, xtick=pitick(a, b, 2; mode=:latex), xlims=(-7,7), ylims=(-3,3), 
    bottom_margin = 10mm, label=L"f(x) = \sin(x)", framestyle = :zerolines, 
    legend=:outerright)
plot!(g,a,b, xtick=pitick(a, b, 2; mode=:latex), xlims=(-7,7), ylims=(-3,3), 
    bottom_margin = 10mm, label=L"g(x) = \sin (|x|)", framestyle = :zerolines, 
    legend=:outerright)

```

is it true to plot with code of `sin(abs(x))` ?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 13, 2022, 4:45pm UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216/2 "2022-09-13T16:45:14Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> is it true to plot with code of `sin(abs(x))` ?

I’m not sure what is the question, does it look like this? (your code does not run)

```julia
julia> using UnicodePlots

julia> x = -2π:1e-3:2π;

julia> lineplot(x, sin.(abs.(x)))
      ┌────────────────────────────────────────┐ 
    1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠏⢳⠀⠀⡇⠀⡞⠹⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠈⡇⠀⡇⢸⠁⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⢳⠀⡇⡞⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢸⡀⣇⡇⠀⠀⠈⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠀⠀⠀⡇⣿⠀⠀⠀⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢧⡿⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠘⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠤⠤⠤⠤⠤⠤⠤⢤⠤⠤⠤⠤⠤⢼⠤⠤⠤⠤⠤⠼⡧⠤⠤⠤⠤⠤⡧⠤⠤⠤⠤⠤⡤⠤⠤⠤⠤⠤⠤⠤│ 
      │⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠸⡄⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠈⡇⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⣸⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⣇⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⢸⠀⠀⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⣇⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠈⡇⠀⣸⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
   -1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣄⡏⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⢹⣠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      └────────────────────────────────────────┘ 
      ⠀-10⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀10⠀ 

```

---

<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:** [September 14, 2022, 8:14am UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216/3 "2022-09-14T08:14:44Z")

</div>

Glad someone is using `UnicodePlots`

I get it that I need to use `sin.(abs.(x))` instead of `sin(abs(x))` but the plot stays the same for me, I am using Jupyter Notebook // IJulia.

The plot:

 ![Capture d’écran_2022-09-14_15-34-26](https://global.discourse-cdn.com/julialang/original/3X/e/6/e603d27b4c461cba5626e5286cddf63173a39a27.png)

My question is, is it the right plot for sin(|x|) ?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 14, 2022, 9:17am UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216/4 "2022-09-14T09:17:01Z")

</div>

No. That is a plot of two functions. Only one of those functions is \sin(|x|). (The red line).

The Julia function `abs(x)` gives the absolute value of x, which is often written as |x|. Was that your question?

---

<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:** [September 14, 2022, 12:09pm UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216/5 "2022-09-14T12:09:48Z")

</div>

yes that is my question, I plot on purpose sin(x) too as comparison. I think my plot is right…

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [September 26, 2022, 2:17am UTC](https://discourse.julialang.org/t/check-if-my-plot-is-right-for-sin-x/87216/6 "2022-09-26T02:17:43Z")

</div>

Yes, your plot is fine.
