# Plotting variable number of subplots in a plot

**URL:** <https://discourse.julialang.org/t/plotting-variable-number-of-subplots-in-a-plot/52632>\
**Category:** New to Julia\
**Created:** [December 30, 2020, 7:46pm UTC](https://discourse.julialang.org/t/plotting-variable-number-of-subplots-in-a-plot/52632 "2020-12-30T19:46:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [December 30, 2020, 7:46pm UTC](https://discourse.julialang.org/t/plotting-variable-number-of-subplots-in-a-plot/52632/1 "2020-12-30T19:46:37Z")

</div>

Ho do I plot a variable number of subplots in a single common plot. For example, the following simple code

```julia
using Plots;
plotlyjs();

N = 3;
Q = Array{Plots.Plot, 1}(undef, N);
for n in 1:N
    Q[n] = plot(rand(4));
end
P = plot(Q, layout = (N,1));

```

leads to the error

```julia
ERROR: LoadError: MethodError: no method matching Plots.Plot{Plots.PlotlyJSBackend}(::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char)
Closest candidates are:
  Plots.Plot{Plots.PlotlyJSBackend}(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any) where T<:AbstractBackend at C:\Users\iamsu\.julia\packages\Plots\NVH6y\src\types.jl:68
Stacktrace:
 [1] apply_recipe(::Dict{Symbol,Any}, ::Type{Plots.Plot{Plots.PlotlyJSBackend}}, ::Plots.Plot{Plots.PlotlyJSBackend}) at C:\Users\iamsu\.julia\packages\Plots\NVH6y\src\recipes.jl:50
 [2] _apply_type_recipe(::Dict{Symbol,Any}, ::Array{Plots.Plot,1}, ::Symbol) at C:\Users\iamsu\.julia\packages\RecipesPipeline\NRqoc\src\type_recipe.jl:36
 [3] macro expansion at C:\Users\iamsu\.julia\packages\RecipesPipeline\NRqoc\src\user_recipe.jl:144 [inlined]
 [4] apply_recipe(::Dict{Symbol,Any}, ::Array{Plots.Plot,1}) at C:\Users\iamsu\.julia\packages\RecipesBase\FlaiX\src\RecipesBase.jl:281
 [5] _process_userrecipes!(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Plots.Plot,1}}) at C:\Users\iamsu\.julia\packages\RecipesPipeline\NRqoc\src\user_recipe.jl:35
 [6] recipe_pipeline!(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Plots.Plot,1}}) at C:\Users\iamsu\.julia\packages\RecipesPipeline\NRqoc\src\RecipesPipeline.jl:67
 [7] _plot!(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Plots.Plot,1}}) at C:\Users\iamsu\.julia\packages\Plots\NVH6y\src\plot.jl:167
 [8] plot(::Array{Plots.Plot,1}; kw::Base.Iterators.Pairs{Symbol,Tuple{Int64,Int64},Tuple{Symbol},NamedTuple{(:layout,),Tuple{Tuple{Int64,Int64}}}}) at C:\Users\iamsu\.julia\packages\Plots\NVH6y\src\plot.jl:57
 [9] top-level scope at C:\ShobKichhu\Julia\t.jl:9
 [10] include(::String) at .\client.jl:457
 [11] top-level scope at REPL[1]:1
in expression starting at C:\ShobKichhu\Julia\t.jl:9

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 30, 2020, 8:37pm UTC](https://discourse.julialang.org/t/plotting-variable-number-of-subplots-in-a-plot/52632/2 "2020-12-30T20:37:34Z")

</div>

Splatting the vector of Plots works:

```julia
P = plot(Q..., layout = (N,1));

```

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [December 30, 2020, 8:45pm UTC](https://discourse.julialang.org/t/plotting-variable-number-of-subplots-in-a-plot/52632/3 "2020-12-30T20:45:20Z")

</div>

It worked! Thanks!
