Friday, October 7, 2022

[SOLVED] How can I edit this code to include a for-loop to make 30 output files as well as change the title for each plot heading?

Issue

I have to make 30 plots (30 output SVG files). Each plot requires one input file called 'step-x' where x = 1,2,3, ... 30. I was told to use a for-loop but am not sure how to. The code below is what I have so far.

Additionally I need to be able to change the title of the plot where it says set title "Associative Pathway (0,1)" and where it says title "{/:Bold 0.84 V}" for each individual plot.

Thanks in advance

#!/usr/local/Cellar/gnuplot/5.4.4/bin/gnuplot

set termoption font "Sans,22"
set border 15 front lt black linewidth 3.000 dashtype solid
set xrange [0:5.5]
set yrange[-5:0.1]
set title "Associative Pathway (0,1)"
set title  font "{/:Bold},23"
set xlabel "Reaction Coordinate"
set xlabel  font "{/:Bold,23}"
set ylabel "Free Energy (eV)"
set ylabel  font "{/:Bold,23}"
set xtic scale 0
set ytics out nomirror
set xtics nomirror
set xtics ("O_{2}" 0.3, "O@^*_{2}" 1.3, "OOH^*" 2.3  , "O^*" 3.3, "OH^*" 4.3 ,"H_{2}O" 5.3 ) font "{/:Bold},23"
set ytics font "{/:Bold},23"
## Last datafile plotted: "step-x"
p "step-1" u 7:8 w l dt 4 lc rgb 'blue' notitle, "step-1" u 2:3 w l lw 3 lc rgb 'blue' title "{/:Bold 0 V}"
rep "step-1" u 9:10 w l dt 4 lc rgb 'red' notitle, "step-1" u 4:5 w l lw 3 lc rgb 'red' title "{/:Bold 0.84 V}"
#    EOF

Below is the data file

    #X-title     #X-U(0)      #X-U(1.8) #y-title  #y-U(0)        #y-U(1.8)
#1           #2  #3       #4    #5    #6      #7  #8        #9    #10
O_{2}        0   0        0   -3.00  O_{2}    0.5  0         0.5 -3.00
O@^*_{2}     0.5 0        0.5 -3.00  O_{2}    1   -0.1       1  -3.04

O@^*_{2}     1   -0.1     1   -3.04  OOH^*    1.5 -0.1       1.5  -3.04
OOH^*        1.5 -0.1     1.5 -3.04  OOH^*    2   -1.00      2  -3.12

OOH^*        2   -1.00    2   -3.12  O^*      2.5 -1.0       2.5  -3.12
O^*          2.5 -1.00    2.5 -3.12  O^*      3   -2.00      3    -4.00

O^*          3    -2.00   3    -4.00 OH^*     3.5 -2.00      3.5  -4.00
OH^*         3.5  -2.00   3.5  -4.00 OH^*     4   -4.00      4   -4.50

OH^*         4   -4.00    4   -4.50  H_{2}O   4.5 -4.00      4.5 -4.50
H_{2}O       4.5 -4.00    4.5 -4.50  H_{2}O   5   -4.50      5   -4.50

H_{2}O       5   -4.50    5   -4.50
H_{2}O       5.5 -4.50    5.5 -4.50

Solution

Glad that you added some data. This is essential for making reasonable suggestions. However, I wouldn't call your data above "simplified". You basically ignored the suggestions from the answers to your other question. Your data structure here is horribly complicated and difficult to create and maintain. You can simplify the data to the absolute minimum, but well, then you have to put a little bit more effort into the plotting command.

Yes, you can create your 30 graphs in a loop (check help do), no need for bash.

  • define functions for your input/output file names (check help sprintf).
  • you have to set the output before the actual plotting command.

I hope you can adapt the example below to your needs.

Data: SO73873363_step-1.dat

Isn't this data format much smaller, clearer and easier to understand?

"Reaction Coordinate"  "0 V"   "0.84 V" 
O_{2}                   0      -3.00
O@^*_{2}               -0.1    -3.04
OOH^*                  -1.00   -3.12
O^*                    -2.00   -4.00
OH^*                   -4.00   -4.50
H_{2}O                 -4.50   -4.50

Script:

### create multiple output files from multiple input files
reset session

myFileIn(i)  = sprintf("SO73873363_step-%d.dat",i)
myFileOut(i) = sprintf("SO73873363_step-%d.svg",i)

set term svg font "Sans,20"

set border 15 front lt black linewidth 3.000 dashtype solid
set title "Associative Pathway (0,1)"
set xlabel "Reaction Coordinate"
set xrange [-0.5:5.5]
set xtics nomirror scale 0
set ylabel "Free Energy (eV)"
set yrange[-5:0.1]
set ytics out nomirror
set key noautotitle samplen 2
set errorbars 0
dx = 0.2

do for [i=1:30] {
    set output myFileOut(i)

    plot myFileIn(i) u 0:2:(dx):xtic(1) w xerr lc "blue" lw 3 ps 0 ti columnheader(2), \
                  '' u 0:3:(dx)         w xerr lc "red"  lw 3 ps 0 ti columnheader(3), \
        x1=y1=NaN '' u (x0=x1,x1=$0,x0-1+dx):(y0=y1,y1=$2,y0):(1-2*dx):(y1-y0) w vec dt 4 lc "blue" nohead, \
        x1=y1=NaN '' u (x0=x1,x1=$0,x0-1+dx):(y0=y1,y1=$3,y0):(1-2*dx):(y1-y0) w vec dt 4 lc "red" nohead
}
set output   # close the last file
### end of script

Result:

enter image description here



Answered By - theozh
Answer Checked By - Gilberto Lyons (WPSolving Admin)