# Error using PGFPlots.jl

**URL:** <https://discourse.julialang.org/t/error-using-pgfplots-jl/19080>\
**Category:** General Usage\
**Tags:** plotting, pgfplot\
**Created:** [December 29, 2018, 1:53am UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080 "2018-12-29T01:53:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)\
**Post date:** [December 29, 2018, 1:53am UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080/1 "2018-12-29T01:53:08Z")

</div>

Hi, please help me with this annoying and incomprehensive behavior. When I run

```julia
using PGFPlots
p = []
fun = [sin, cos]
x = 0:10
for i = 1:2
    push!(p, Plots.Linear(x, fun[i].(x)))
end
Axis(p)

```

I get the following error:

```julia
ERROR: LoadError: MethodError: no method matching Axis(::Array{Any,1})

```

But if I replace `Axis(p)` by `Axis([p[1], p[2]])`, it actually plots the two curves. Moreover, adding `println([p[1], p[2]] == p)` it actually prints `true` as the answer.

So, if `[p[1], p[2]] == p`, why do I get an error with `Axis(p)`? I can’t really understand this behavior.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 29, 2018, 7:44am UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080/2 "2018-12-29T07:44:12Z")

</div>

This isn’t actually a PGFPlots specific issue, but rather to do with the way you construct your arrays:

```julia
julia> p = [] # No type information supplied, hence our array is of type Any
0-element Array{Any,1}

julia> for i ∈ 1:2
       push!(p, 5)
       end

julia> p
2-element Array{Any,1}: # Type stays Array{Any} as this is what was defined above
 5
 5

julia> [p[1], p[2]]
2-element Array{Int64,1}: # Here we are constructing a new Array, the type of which is inferred from the elements and is therefore Array{Int64,1}
 5
 5

julia> [p[1], p[2]] == p # This compares the contents of the Arrays
true

julia> typeof(p) == typeof([p[1], p[2]]) # This compares the types - they differ, as the array comprehension infers the type for us. This causes your issue in dispatch.
false

# You should be doing the equivalent of this:
julia> q = Array{Int64, 1}() # Instantiate the Array with the type you want
0-element Array{Int64,1}

julia> for i ∈ 1:2
       push!(q, 5)
       end

julia> typeof(q) == typeof([p[1], p[2]])
true

```

I don’t have PGFPlots installed on this machine, but you’ll want to check `typeof(p[1])` and then replace `p = []` with `p = Array{ result_of_typeof[p[1]] , 1}()` to ensure `p` has the right type for the call to `Axis` to work.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 29, 2018, 9:27am UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080/3 "2018-12-29T09:27:19Z")

</div>

> [@nilshg](#):
>
> `p = Array{ result_of_typeof[p[1]] , 1}()`

There is a simpler and more convenient way of creating empty typed arrays. Rather than writing `Array{T,1}()` or `Vector{T}()`, you just write `T[]`. E.g.

```julia
julia> Float64[]
0-element Array{Float64,1}

```

Sometimes it might be a bit hard to get `T` right, though, so it is perhaps easier to do something like this:

```julia
p = [plot(x, fun.(x)) for fun in (sin, cos)]

```

(I couldn’t find any function `Plots.Linear` in either `Plots` or `PGFPlots`.)

---

<div class="post-metadata">

**Author:** ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)\
**Post date:** [December 29, 2018, 1:03pm UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080/4 "2018-12-29T13:03:09Z")

</div>

Thank you very much for all your advice. I’ve changed the code as follows and it works fine:

```julia
using PGFPlots
p = Array{PGFPlots.Plots.Linear}(undef, 2)
fun = [sin, cos]
x = 0:10
for i = 1:2
    p[i] = Plots.Linear(x, fun[i].(x))
end
Axis(p)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 29, 2018, 2:32pm UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080/5 "2018-12-29T14:32:42Z")

</div>

It’s up to you, of course, but to me it looks a bit overly complicated and verbose. The comprehension relieves you of having to specify complex types, having to repeat yourself, and potentially getting it subtly wrong.

Whenever I can leave type stuff to the compiler, I tend to find that the resulting code is cleaner and more generic.

---

<div class="post-metadata">

**Author:** ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)\
**Post date:** [December 30, 2018, 7:59am UTC](https://discourse.julialang.org/t/error-using-pgfplots-jl/19080/6 "2018-12-30T07:59:24Z")

</div>

That’s my matlabish background working. My code is slightly more complicated and it was a bit of a challenge to code it as a comprehension. It looks very neat now. Thank you again for your help.
