# Order of arguments in plotting

**URL:** <https://discourse.julialang.org/t/order-of-arguments-in-plotting/73596>\
**Category:** New to Julia\
**Tags:** question, plotting\
**Created:** [December 24, 2021, 8:37pm UTC](https://discourse.julialang.org/t/order-of-arguments-in-plotting/73596 "2021-12-24T20:37:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Borel](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Borel](https://discourse.julialang.org/u/Borel)\
**Post date:** [December 24, 2021, 8:37pm UTC](https://discourse.julialang.org/t/order-of-arguments-in-plotting/73596/1 "2021-12-24T20:37:14Z")

</div>

This is an example from **QuanEcon Section 31. Optimal Growth I**.

The following code tries to plot the linear interpolation and the true value of a function f.

```julia
f(x) = 2 .* cos.(6x) .+ sin.(14x) .+ 2.5
c_grid = 0:.2:1
f_grid = range(0, 1, length = 150)

Af = LinearInterpolation(c_grid, f(c_grid))

plt = plot(xlim = (0,1), ylim = (0,6))
plot!(plt, f, f_grid, color = :blue, lw = 2, alpha = 0.8, label = "true function")
plot!(plt, f_grid, Af.(f_grid), color = :green, lw = 2, alpha = 0.8,
      label = "linear approximation")
plot!(plt, f, c_grid, seriestype = :sticks, linestyle = :dash, linewidth = 2, alpha = 0.5,
      label = "")
plot!(plt, legend = :top)

```

And the graph will be as follows

 ![Screen Shot 2021-12-24 at 15.28.07](https://global.discourse-cdn.com/julialang/original/3X/e/8/e8b5cc7de870aeab1be2ee4162577d7db0994563.png)

However, if I change from

```julia
plot!(plt, f_grid, Af.(f_grid), color = :green, lw = 2, alpha = 0.8,
      label = "linear approximation")

```

to

```julia
plot!(plt, Af.(f_grid), f_grid, color = :green, lw = 2, alpha = 0.8,
      label = "linear approximation")

```

It will give me a different graph.

But, I have found that the graph doesn’t change if I change the order of arguments of f and f\_grid. i,e

```julia
plot!(plt, f, f_grid, color = :blue, lw = 2, alpha = 0.8, label = "true function")

```

will give us the same graph as

```julia
plot!(plt, f_grid, f,color = :blue, lw = 2, alpha = 0.8, label = "true function")

```

I would appreciate it if someone can help me with this issue. To be more specific, why change of order in some command will result in a different graph while change the order in some commands will lead to the same graph.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 24, 2021, 9:40pm UTC](https://discourse.julialang.org/t/order-of-arguments-in-plotting/73596/2 "2021-12-24T21:40:37Z")

</div>

(don’t forget to mention any packages you use, I’m assuming you’re plotting with `Plots.jl`)

Normally, plots assumes that arguments are in order `y`, `x, y` or `x, y, z`, but it also uses dispatch. It has, for instance, been decided that if the first argument `isa Function`, the next is rather a _domain_ than a `y`, because it seems unlikely you would want to plot a `y` vector vs an `x` function.

Essentially, the fact that transposing doesn’t matter in one of those cases is the exception.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 25, 2021, 12:19pm UTC](https://discourse.julialang.org/t/order-of-arguments-in-plotting/73596/3 "2021-12-25T12:19:55Z")

</div>

> [@gustaphe](#):
>
> It has, for instance, been decided that if the first argument `isa Function` , the next is rather a _domain_ than a `y` , because it seems unlikely you would want to plot a `y` vector vs an `x` function.

Currently `plot(x, g) == plot(g, x) == plot(x, g.(x))`, but it would make sense to have: `plot(g⁻¹, y)` to mean `plot(g⁻¹.(y), y)`

For example:

```julia
using Plots; gr(legend=false, lw=3)
g(x) = log(x)
g⁻¹(y) = exp(y)
x = 0.1:0.01:2
plot(x, g)
y = g.(x)
# plot!(g⁻¹, y) # this could be interpreted instead as: plot!(g⁻¹.(y),y)
plot!(g⁻¹.(y), y, ls=:dash)

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 25, 2021, 12:54pm UTC](https://discourse.julialang.org/t/order-of-arguments-in-plotting/73596/4 "2021-12-25T12:54:02Z")

</div>

I think that’s rare enough it makes sense to force you to be explicit. But it’s a _somewhat agree_ for me.

It definitely won’t change though.
