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

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:

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

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

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:

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)
2 Likes

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

		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

1 Like

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

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])

# 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) 

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. :smirk:

You can concatenate the vectors pltsSim, pltsMeas like this:

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:

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.

1 Like

Thanks Andreas your solution works admirable well :slightly_smiling_face:

1 Like