# How to Transform Plots / Shape with Plots.jl

**URL:** <https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 24, 2022, 8:00am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079 "2022-12-24T08:00:13Z")\
**Posts on this page:** 8\
**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:** [December 24, 2022, 8:00am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/1 "2022-12-24T08:00:13Z")

</div>

Hi all,

I want to transform the shape that I plot with this code:

```julia
using Plots, LaTeXStrings, Roots, SymPy
gr()

f(x) = sin(x^2) + cos(2x) - 6
g(x) = sin(x^2) + cos(2x) 

plot(f,2,4, ylims=(-8,3), 
	legend=:topleft, linecolor=:green, label="")
plot!(g,2,4, ylims=(-8,3),
	legend=:topleft, linecolor=:green, label="")

plot!([2,2],[f(2),g(2)], label="", linecolor=:green)
plot!([4,4],[f(4),g(4)], label="", linecolor=:green)

```

so it can be mirrored, the transformation will be translation and mirroring. Any package here that can help me?

This is the my plot:  
 ![Capture d’écran_2022-12-24_14-59-00](https://global.discourse-cdn.com/julialang/original/3X/5/9/599b38f9f20f5e25e81a00526b296a8ef0f1f7a4.png)

This is what I want to achieve, the mirror of the above shape:

![Capture d’écran_2022-12-24_14-59-46](https://global.discourse-cdn.com/julialang/original/3X/2/6/265087d5a562f8a734280febf5bc9ec96b6599c2.png)

Cavalieri’s Principle

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 24, 2022, 9:25am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/2 "2022-12-24T09:25:34Z")

</div>

Have you tried declaring your own types to scale and shift the function?

```julia
julia> struct YShift{F, Y} <: Function f::F; y::Y end

julia> (g::YShift)(x) = g.f(x) + g.y

julia> struct YScale{F, S} <: Function f::F; s::S end

julia> (g::YScale)(x) = g.s * g.f(x)

julia> plot!(YShift(f, -6), 2, 4, linecolor=:green)

julia> plot!(YScale(f, -1), 2, 4, linecolor=:green)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 24, 2022, 11:53am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/3 "2022-12-24T11:53:31Z")

</div>

Defining new types may be a bit of an overkill for this problem if it does not repeat a few times. Here is another function with some ‘functional’ approach:

```julia
function make_fg_plot()
    f(x) = sin(x^2) + cos(2x) - 6
    g(x) = sin(x^2) + cos(2x)

    yshift = y -> y + 5
    yreflection = y -> -y
    ytransform = yshift ∘ yreflection

    plt = plot(; ylims = (-8, 15), legend = :topleft)
    for (T, attrs) in [(identity, (;label = "", linecolor = :green)), 
                       (ytransform, (;label = "", linecolor = :red))]
        plot!(plt, T ∘ f, 2, 4; attrs...)
        plot!(plt, T ∘ g, 2, 4; attrs...)

        plot!(plt, [2, 2], T.([f(2), g(2)]); attrs...)
        plot!(plt, [4, 4], T.([f(4), g(4)]); attrs...)
    end
    return plt
end

```

This function returns the `plt` object and may be called from REPL or `display`ed otherwise.

---

<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:** [December 25, 2022, 4:45am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/4 "2022-12-25T04:45:19Z")

</div>

It is colorful,

I thought someone here can point me to a package that can transform plot of curves, but we need to create our own function instead. Thanks!

![Capture d’écran_2022-12-25_11-43-44](https://global.discourse-cdn.com/julialang/original/3X/9/e/9e7c18793b399974838444149d1d90c64c49ae1c.png)

---

<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:** [December 25, 2022, 4:52am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/5 "2022-12-25T04:52:56Z")

</div>

Where or how did you learn to make the function? I want to learn and understand more.

Why write

```julia
for (T, attrs) 

```

and

```julia
plot!(plt, T ∘ f, 2, 4; attrs...)

```

So `identity` is defined already in Julia?

---

<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:** [December 25, 2022, 5:00am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/6 "2022-12-25T05:00:49Z")

</div>

I add another simple function for anyone who wants to learn:

```julia
using Plots, LaTeXStrings, Roots, SymPy
gr()

function make_fg_plot()
    f(x) = sin(x^2) + cos(2x) - 6
    g(x) = sin(x^2) + cos(2x)

    yshift = y -> y + 5
    yreflection = y -> -y
    ytransform = yshift ∘ yreflection

    plt = plot(; ylims = (-8, 15), legend = :topleft)
    for (T, attrs) in [(identity, (;label = "", linecolor = :green)), 
                       (ytransform, (;label = "", linecolor = :red))]
        plot!(plt, T ∘ f, 2, 4; label="")
        plot!(plt, T ∘ g, 2, 4; label="")

	for i=0:0.2:2        
	plot!(plt, [2+i, 2+i], T.([f(2+i), g(2+i)]); label="")
	end
    end
    return plt
end

make_fg_plot()

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 25, 2022, 8:15am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/7 "2022-12-25T08:15:34Z")

</div>

I like this approach. It’s not even really specific to the y-axis, and using built-in partial application types makes it play nice with things like InverseFunctions.jl:

```julia
shift(ν) = Base.Fix1(+, ν)
shift(5) ∘ f # y-up-shift
f ∘ shift(2) # x-left-shift

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 25, 2022, 11:02am UTC](https://discourse.julialang.org/t/how-to-transform-plots-shape-with-plots-jl/92079/8 "2022-12-25T11:02:47Z")

</div>

When I ran the function, it didn’t have all the colors (because of the `attr...` bit). So there is an odd discrepancy. Maybe it’s a version issue. Weird.

 ![transformed](https://global.discourse-cdn.com/julialang/original/3X/d/9/d900abfcbcc7c8bda68321d92654a1def157a2f5.png)
