# How to reflect plot function toward $y=x$ with Plots?

**URL:** https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647
**Category:** General Usage
**Tags:** plotting
**Created:** [March 7, 2023, 4:56am UTC](https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647 "2023-03-07T04:56:29Z")
**Posts on this page:** 5
**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: [March 7, 2023, 4:56am UTC](https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647/1 "2023-03-07T04:56:29Z")

</div>

Hi all,

I have the code from previous forum I made, that reflect a graph toward y axis. Now I want to reflect it toward y=x , I can’t just do `yreflection = y -> x` it prompts a lot of errors

This code works on y axis reflection.

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures
gr()

function make_fg_plot()
    f(x) = log(6x) 
    g(x) = 2x + log(6x)

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

    plt = plot(; xtick=:false, ylims = (-15, 15), 
	 legend = :topleft, bottom_margin=5mm)
    for (T, attrs) in [(identity, (;label = "", linecolor = :green)), 
                       (ytransform, (;label = "", linecolor = :red))]
        plot!(plt, T ∘ f, 0, 10; label="")
        plot!(plt, T ∘ g, 0, 10; label="")

	for i=0:0.2:2        
	plot!(plt, [2+i, 2+i], T.([f(2+i), g(2+i)]); label="")
	end
	
	annotate!([(3,-16.1, (L"x", 10, :black))])
	annotate!([(3,-15.1, (L"|", 10, :black))])
    end
    return plt
end

make_fg_plot()

```

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [March 7, 2023, 9:07am UTC](https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647/2 "2023-03-07T09:07:34Z")

</div>

Hi!

Thanks for the example code. Could you clarify what it _should_ be doing? Because it doesn’t reflect the functions at the y axis, as far as I can tell. Did you mean to write

```julia
yreflection = y -> -y

```

perhaps?

Regarding the question: Doing `yreflection = y -> x` will not do the right thing, because `x` is not known to this “reflection function”. What we are using here are _anonymous functions_ (you can read more about them here [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions) ) and they are basically normal Julia functions, just without a name. So the function you intuitively wrote down would be something like

```julia
begin <no name>(y)
    return x
end

```

but of course, `x` is not defined anywhere in the code, so it doesn’t know how to use it.

* * *

The transformation you want is only possible if we know the pair of coordinates (x, y) and make a function like `reflect_at_45 = (x, y) -> (y, x)`. But then the syntax for the plot would have to change and we would need to pass the range of `x` values explicitly, for example like so:

```julia
# Define the x-range explicitly
xrange = 0.0:0.1:10.0
# Define the transform (or anything else like `(x,y) -> (x, -y)` etc.
xyidentity = (x,y) -> (x,y)
xytransform = (x,y) -> (y, x)

...
for (T, attrs) in [(xyidentity, (;label = "", linecolor = :green)), 
                       (xytransform, (;label = "", linecolor = :red))]

    # Apply the transformation to all tuples (x, f(x))
    transformedPoints = T.(xrange, f.(xrange))
    # Make vectors out of the resulting tuples for plotting
    newx = first.(transformedPoints)
    newy = last.(transformedPoints)

    plot!(plt, newx, newy; label="")
...
end

```

There is probably a simpler way of writing it than in my example, but it should be flexible enough to to any kind of reflections in 2D.

---

<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: [March 7, 2023, 9:22am UTC](https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647/3 "2023-03-07T09:22:58Z")

</div>

Hi @Sevi ,

the code should reflect the graphs toward y=0 axis. This is the plot:  
 ![Capture d’écran_2023-03-07_16-21-37](https://global.discourse-cdn.com/julialang/original/3X/7/0/7081fd80f395e74e2030c21dbe7eb45a6ed8e5d5.png)

I give the bars to make it looks like Pharaoh

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [March 7, 2023, 9:37am UTC](https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647/4 "2023-03-07T09:37:05Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> ` yreflection = y -> y`

I just meant that the reflection in your example code is the same as `identity`. So when I run it, I just get the upper half of the plot, not the lower one. But it’s just a missing minus sign 🙂

---

<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: [March 7, 2023, 12:21pm UTC](https://discourse.julialang.org/t/how-to-reflect-plot-function-toward-y-x-with-plots/95647/5 "2023-03-07T12:21:02Z")

</div>

I am able to create it like this:

![Capture d’écran_2023-03-07_19-20-31](https://global.discourse-cdn.com/julialang/original/3X/0/9/097fd86852c5ef06750ab3b1ab80084bf06fd3da.png)

with this code:

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures
gr()

# Define the x-range explicitly
xrange = 0.0:0.1:20.0
# Define the transform (or anything else like `(x,y) -> (x, -y)` etc.
xyidentity = (x,y) -> (x,y)
xytransform = (x,y) -> (y, x)

function make_fg_plot()
    f(x) = log(x) 
   
    plt = plot(; xtick=:true, xlims = (-15,15), ylims = (-15, 15), 
	 legend = :topleft, bottom_margin=5mm)
    for (T, attrs) in [(xyidentity, (;label = "", linecolor = :green)), 
                       (xytransform, (;label = "", linecolor = :red))]
    # Apply the transformation to all tuples (x, f(x))
    transformedPoints = T.(xrange, f.(xrange))
    # Make vectors out of the resulting tuples for plotting
    newx = first.(transformedPoints)
    newy = last.(transformedPoints)

    plot!(plt, newx, newy; label="")
    g(x)=x
    plot!(g,linecolor=:green, line=(:dash), label="")
    end
    return plt
end

make_fg_plot()

```

thanks a lot!
