# Subplotting incoherent behaviour

**URL:** <https://discourse.julialang.org/t/subplotting-incoherent-behaviour/117303>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [July 21, 2024, 2:56pm UTC](https://discourse.julialang.org/t/subplotting-incoherent-behaviour/117303 "2024-07-21T14:56:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![traincopter](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@traincopter](https://discourse.julialang.org/u/traincopter)\
**Post date:** [July 21, 2024, 2:56pm UTC](https://discourse.julialang.org/t/subplotting-incoherent-behaviour/117303/1 "2024-07-21T14:56:05Z")

</div>

Hello,  
I’m experiencing the following behavior, which confuses me a lot.

First of all, let’s consider the following code:

```julia
using Plots
x = range(0, 10, length=100)
y1 = @. exp(-0.1x) * cos(4x)
y2 = @. exp(-0.3x) * cos(4x)
y3 = @. exp(-0.1x)
y4 = @. exp(-0.3x)
y = [y1 y2 y3 y4]

p1 = plot(x, y)
p2 = plot(x, y, title="Title 2", lw=3)
p3 = scatter(x, y, ms=2, ma=0.5, xlabel="xlabel 3")
p4 = scatter(x, y, title="Title 4", ms=2, ma=0.2)
pp = [p1,p2,p3,p4]
plot(pp..., layout=(2,2), legend=false)

```

In this case I put all plots in a vector and splat them into plot, it is not very smart but it works.

In the following I do the same except that I append each subplot to `pp` instead of constructing it at the end

```julia
using Plots
x = range(0, 10, length=100)
y1 = @. exp(-0.1x) * cos(4x)
y2 = @. exp(-0.3x) * cos(4x)
y3 = @. exp(-0.1x)
y4 = @. exp(-0.3x)
y = [y1 y2 y3 y4]

pp = []
p1 = plot(x, y)
append!(pp,p1)
p2 = plot(x, y, title="Title 2", lw=3)
append!(pp,p2)
p3 = scatter(x, y, ms=2, ma=0.5, xlabel="xlabel 3")
append!(pp,p3)
p4 = scatter(x, y, title="Title 4", ms=2, ma=0.2)
append!(pp,p4)
plot(pp..., layout=(2,2), legend=false)

```

Here I get the error

```julia
julia> show(err)
1-element ExceptionStack:
LoadError: Couldn't process recipe args: (Plots.PlaceHolder, Plots.PlaceHolder, Plots.Subplot{Plots.GRBackend}, Plots.Subplot{Plots.GRBackend}, Plots.Subplot{Plots.GRBackend})
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] macro expansion
    @ ~/.julia/packages/RecipesPipeline/BGM3l/src/user_recipe.jl:158 [inlined]
  [3] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, v1::Any, v2::Any, v3::Any, v4::Any, vrest::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesBase/BRe07/src/RecipesBase.jl:300
  [4] _process_userrecipes!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/user_recipe.jl:38
  [5] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/RecipesPipeline.jl:72
  [6] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/du2dt/src/plot.jl:223
  [7] plot!(::Plots.Plot, ::Any, ::Vararg{Any}; kw...)
    @ Plots ~/.julia/packages/Plots/du2dt/src/plot.jl:213
  [8] plot(::Plots.Plot{Plots.GRBackend}, ::Plots.PlaceHolder, ::Vararg{Any}; kw::@Kwargs{layout::Tuple{Int64, Int64}, legend::Bool})
    @ Plots ~/.julia/packages/Plots/du2dt/src/plot.jl:206
  [9] plot(::Plots.Subplot, ::Any, ::Vararg{Any}; kw...)
    @ Plots ~/.julia/packages/Plots/du2dt/src/plot.jl:293
 [10] top-level scope
    @ ~/UNI/SLT/mini_project_2020/prova.jl:18
 [11] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [12] top-level scope
    @ REPL[7]:1
in expression starting at /home/ape/UNI/SLT/mini_project_2020/prova.jl:18

```

What’s the problem?

`Julia Version 1.10.4 (2024-06-04)`  
`Plots Version 1.40.5`

---

<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:** [July 21, 2024, 3:51pm UTC](https://discourse.julialang.org/t/subplotting-incoherent-behaviour/117303/2 "2024-07-21T15:51:42Z")

</div>

Hello.  
I think you should use `push!()` instead of `append!()`.

The reason [is this one](https://stackoverflow.com/a/72916898/14071775).

---

<div class="post-metadata">

**Author:** ![traincopter](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@traincopter](https://discourse.julialang.org/u/traincopter)\
**Post date:** [July 21, 2024, 6:48pm UTC](https://discourse.julialang.org/t/subplotting-incoherent-behaviour/117303/3 "2024-07-21T18:48:20Z")

</div>

That worked. It was an easy fix but I wouldn’t have found the solution by myself in a 1000 years. Thanks a lot.

---

<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:** [July 21, 2024, 6:51pm UTC](https://discourse.julialang.org/t/subplotting-incoherent-behaviour/117303/4 "2024-07-21T18:51:35Z")

</div>

You’re welcome.  
The example in the SO post by @fredrikekre linked above, seems clearer than what is provided [in the official docs](https://docs.julialang.org/en/v1/base/collections/#Base.append!).
