Search code examples
for-looppnggnuplot

Plotting 1,2 multiplots in for loop with gnuplot


I am having an issue generating multiplot of 1x2 for multiple txt files in a for loop. Through the below script, I was able to generate and output different pictures when loading different txt files. However, when I try to use multiplot inside the loop to combine both plots in one plot for each loaded file, gnuplot prompts with this message you can't change the output in multiplot mode

#=======================================
#      Loading Data
#=======================================
set term pngcairo size 1200 ,800 enhanced font "Times-New-Roman, 12"
directory = "../Data/"
filelist = system("cd ../Data/ ; ls *.txt")
files = words(Exp_filelist)
filename(i) = word(filelist,i)

do for [i=1:files] { 
    set output sprintf("picture(x=-%d).png",i)
    #set multiplot layout 1,2
#=======================================
#      First plot
#=======================================
    plot filename(i) using 4:3 with lines notitle 
#=======================================
#      Second plot
#=======================================
    set output sprintf("Picture(x=%d).png",i)

    plot filename(i) using 6:3 with lines notitle 
}
unset multiplot
reset

I wonder what I was doing wrong. How to make set multiplot generate 1x2 inside for loop for each loaded txt file?


Solution

  • You have to close multiplot session before set the next output. Just move unset multiplot into the loop, before set output. After then, you can 're-open' multiplot session by set multiplot.

    set term png
    do for [i=1:files] { 
    set output sprintf("picture(x=-%d).png",i) 
    set multiplot layout 1,2
    plot filename(i) using 4:3 with lines notitle 
    plot filename(i) using 6:3 with lines notitle 
    unset multiplot
    }