Search code examples
gnuplot

For an unknown plot size, how can this script be edited to move the key outside the plot without compressing the plot?


The script works for making the plot itself. However, moving the key outside of the plot window causes it to compress. I have no idea what the margin sizes are as this script creates and saves each plot as an SVG. I am not sure what to do about the size and I have tried other "fixes"recommended and none seem to work.

Here is the date:

 "potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1ML OH" "1ML OH Fe" "1ML OH Cu" "3/2ML OH Fe" "3/2ML OH Cu" "2ML OH" "0-2"
0.00           0         0.949         0          0           0          0            2.685         4.636     4.511
1.23           0        -0.281         0          0           0          0           -1.005        -0.454    -0.409


"potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1ML OH" "1ML OH Fe" "1ML OH Cu" "3/2ML OH Fe" "3/2ML OH Cu" "2ML OH" "0-6"
0.00           0         0.267         0          0           0          0            2.632         3.960     4.710
1.23           0        -0.533         0          0           0          0           -1.058         0.370    -0.200


"potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1ML OH" "1ML OH Fe" "1ML OH Cu" "3/2ML OH Fe" "3/2ML OH Cu" "2ML OH" "1-1"
0.00           0        -0.295         0          0           0          0            0.498         0.503     2.403
1.23           0        -1.605         0          0           0          0           -2.892        -2.087    -2.517

Here is the script to generate the plots: Some lines are commented out for me.

#!/usr/local/Cellar/gnuplot/5.4.4/bin/gnuplot
### Batch create SVG files from multiple subblocks

FILE = "total.dat"

     set term svg font "{/:Bold}Sans,25"
     #set term qt font "Sans,21"
     set border 15 front linecolor rgb "black"  linewidth 3.000 dashtype solid
     #set key noautotitle samplen 2
     set format x "%.1f"
     #set nokey
     set key top right
     set key outside
     set key font "{/:Bold,15}"
     #set key at 2.3,2,0
     set xrange [0:1.23]
     set yrange [-2:2]
     set xlabel "U (V vs. RHE)"
     set xlabel  offset character 0,0.6,0 font "{/:Bold},23"
     set ylabel "{/Symbol D}G (eV)"
     set ylabel  offset character 1.0, 0, 0 font "{/:Bold},26"
     set xtics nomirror font "{/:Bold},24"
     set ytics nomirror font "{/:Bold},24"
     set title noenhanced
     myFileOut(s) = sprintf("%s.svg",s)

     do for [i=0:2] {
         stats FILE index i every ::::0 u (s=strcol(11),0) nooutput
         set output myFileOut(s)

         plot FILE index i u 1:2 w l lw 3 title columnheader(2), \
         "" index i u 1:3 w l lw 3 title columnheader(3), \
         "" index i u 1:4 w l lw 3 title columnheader(4), \
         "" index i u 1:5 w l lw 3 title columnheader(5), \
         "" index i u 1:6 w l lw 3 title columnheader(6), \
         "" index i u 1:7 w l lw 3 title columnheader(7), \
         "" index i u 1:8 w l lw 3 title columnheader(8), \
         "" index i u 1:9 w l lw 3 title columnheader(9), \
         "" index i u 1:10 w l lw 3 lc rgb "pink" title columnheader(10)

}
 set output

Solution

  • In gnuplot version 5.4.4 (shown by your script) you should be able to combine set key outside and set rmargin at screen <fraction> to get what you want, albeit with some experimentation to find the appropriate setting of the right margin. Here is an example:

    set key outside box
    set rmargin at screen 0.7
    set logscale y 
    DATA = "some_file.dat"
    plot for [col=2:15] DATA using 1:col with lines
    

    enter image description here

    For completeness I will add that if you have even a slightly newer gnuplot version, 5.4.5 if not the latest-and-greatest version 6, then you can use set key offset <whatever> to reposition the key without disturbing anything else.

    Here is a simple plot with the key inside (default) but an explicit right margin since I know I will want to leave some room.

    set logscale y 
    set rmargin at screen 0.7
    DATA = "some_file.dat"
    set style data lines
    set key inside box
    plot for [col=2:15] DATA using 1:col title sprintf("DATA column %d",col)
    

    enter image description here

    And here is exactly the same plot after adding a key offset to move the key placement by exactly the amount reserved on the right.

    set key offset screen 0.3
    replot
    

    enter image description here

    The thing to note is that set key offset works regardless of where the margins started out. Since you say "without compressing the plot", I take it that you want to move the key to the right even if it exceeds the nominal right boundary of the output size. This would not work at all for a bitmap output like PNG, but should work for a scrollable vector output like SVG. So I think you could get away with no set rmargin command. But why? Can't you just request a 30% wider SVG canvas and tell it the plot margin is at screen 0.7?