# Plotting with PGFPlots not able to superimpose graphs in a for loop

**URL:** <https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782>\
**Category:** Visualization\
**Tags:** question, pgfplot\
**Created:** [March 10, 2020, 2:47am UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782 "2020-03-10T02:47:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [March 10, 2020, 2:47am UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/1 "2020-03-10T02:47:19Z")

</div>

I am plotting with PGFPlots JULIA, and I would like to superimpose graphs in the loop such as in _Plots_ by using _Plot!_.

But I was only successful to produce separate graphs:

```julia
X = ∑T[1:θcalibr.Nt]    

Plot_θcalibr = GroupPlot(1, 1, groupStyle = "horizontal sep = 2.5cm, vertical sep = 1.5cm")

 for iDepth = 1:N

      Y = Data[1:N, iDepth] 

      push!(Plot_θcalibr, Axis([PGFPlots.Plots.Linear(X, Y, mark="square", markSize=2, onlyMarks=false, style=Style[iDepth], legendentry=Label),
               ],  
       style="width=8cm, height=8cm", xlabel=L"$\ Time \ [Hour]$", ylabel=L"$\theta \ [cm^3 \ cm^{-3}]$")
            )

         end # loop

PGFPlots.save(path, Plot_θcalibr)

```

This code can plot the graphs in separate graphs

 ![WAITOA_Plot_ThetaCalibr](https://global.discourse-cdn.com/julialang/original/3X/1/8/18a8e88b9e4a7b8acb4cc5028510aa121b20834e.png)

**I am asking how can I superimpose the graphs by producing one graph. I tried to remove _push!_ but it did not work?**

Many thanks for any help you may provide

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [March 10, 2020, 6:10pm UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/2 "2020-03-10T18:10:24Z")

</div>

You call `Axis()` for each plot, which creates a new axis for each plot. You should call Axis once, with a list of all plots:

```julia
using PGFPlots

X = 1:100
Data = rand(100,3)

# List of all Linear plots
plts0 = map(axes(Data,2)) do i
    Y = Data[:, i]
    PGFPlots.Plots.Linear(X, Y, mark="square", markSize=2, onlyMarks=false)
end # loop

Plot_θcalibr = GroupPlot(1, 1, groupStyle = "horizontal sep = 2.5cm, vertical sep = 1.5cm")
push!(Plot_θcalibr, Axis(plts0, style="width=8cm, height=8cm", xlabel=L"$\ Time \ [Hour]$", ylabel=L"$\theta \ [cm^3 \ cm^{-3}]$"))

PGFPlots.save("thePlot.pdf", Plot_θcalibr)

```

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [March 10, 2020, 11:03pm UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/3 "2020-03-10T23:03:00Z")

</div>

Thanks Andreas for providing us a beautiful and clean solution. I am providing to the community the final version of the code:

```julia
		function θ_CALIBRATION(∑T, N_iT, N_iZ, θ, θcalibr, discret)

			# For every depth we have measurements
			X = θcalibr.∑T[1:θcalibr.Nt] / (60.0 * 60.0)
			# X = θcalibr.Date[1:θcalibr.Nt] 
			X2 = ∑T[1:N_iT] / (60.0 * 60.0) 
			
			Style = ["smooth, red, very thick", "smooth, orange, very thick", "smooth, brown, very thick", "smooth, gray, very thick", "smooth, black, very thick"]

			Plot_1 = map(axes(θcalibr.Data,2)) do iDepth
				Label = "Neutron=" * string(discret.Znode[iDepth]) * "mm"
				PGFPlots.Plots.Linear(X, θcalibr.Data[1:θcalibr.Nt, iDepth], legendentry=Label, style=Style[iDepth], mark="none")
		  end # loop

			Plot_θcalibr = GroupPlot(1, 1, groupStyle = "horizontal sep = 2.5cm, vertical sep = 1.5cm")

			push!(Plot_θcalibr, Axis(Plot_1, style="width=20cm, height=10cm", xlabel=L"$\ Time \ [Hour]$", ylabel=L"$\theta \ [cm^3 \ cm^{-3}]$", xmin=0.0, title=path.SiteName_Hypix, legendStyle ="{at={(0.2,-0.3)}, anchor=south west, legend columns=3}"))

			PGFPlots.save(path.Hypix_θcalibr, Plot_θcalibr) 
 
			return
		end # function: θ_CALIBRATION

```

 ![WAITOA_Plot_ThetaCalibr](https://global.discourse-cdn.com/julialang/original/3X/8/4/84461396a48c0d5051650baf56329c253eb111a7.png)

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [March 10, 2020, 11:03pm UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/4 "2020-03-10T23:03:58Z")

</div>

I am wandering if PGFPlots can plot **Dates** on the X-Axis ?

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [March 11, 2020, 2:53am UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/5 "2020-03-11T02:53:42Z")

</div>

**I have an other issue Andrea to add other graphs.**

I plotted the _Observed_ in a loop. Now I need also to plot in the same graph the _simulated data_. I tried **Axis([Plot\_1, Plot\_2])**, which unfortunately does not work. Any help to fix the code would be greatly appreciated?

> The answer is Axis([Plot\_1; Plot\_2])

```julia
# SIMULATIONS
			Plot_1 = map(1:5) do iNeutron		
				PGFPlots.Plots.Linear(X1, Y1)		  
			end # loop

		#OBSERVATIONS
			Plot_2 = map(1:5) do iNeutron
				PGFPlots.Plots.Linear(X2, Y2)
			end # loop

		Plot_θcalibr = GroupPlot(1, 1, groupStyle = "horizontal sep = 2.5cm, vertical sep = 1.5cm")

		push!(Plot_θcalibr, Axis([Plot_1; Plot_2])

		PGFPlots.save(path, Plot_θcalibr) 

```

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [March 11, 2020, 2:57am UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/6 "2020-03-11T02:57:45Z")

</div>

Later on after I have made the plot works, I would need to add an other graph underneath the first one, which I am not sure how to do. 😏

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [March 11, 2020, 11:21am UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/7 "2020-03-11T11:21:25Z")

</div>

You can concatenate the vectors `pltsSim`, `pltsMeas` like this:

```julia
pltsBoth = [pltsSim; pltsMeas]
Plot_θcalibr = GroupPlot(1, 1)
push!(Plot_θcalibr, Axis(pltsBoth, title="Simulation and Measurement"))

```

To make two plots, push two plots into GroupPlot:

```julia
Plot_θcalibr = GroupPlot(1, 2)
push!(Plot_θcalibr, Axis(pltsSim, title="Simulation"))
push!(Plot_θcalibr, Axis(pltsMeas, title="Measurement"))

```

I dont know how to do Dates.

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [March 12, 2020, 7:37am UTC](https://discourse.julialang.org/t/plotting-with-pgfplots-not-able-to-superimpose-graphs-in-a-for-loop/35782/8 "2020-03-12T07:37:01Z")

</div>

Thanks Andreas your solution works admirable well 🙂
