Multiplot of variable length data using gnuplot

233 Views Asked by At

Here I have two data files with model parameters and RMSEs computed for them. I would like to get those RMSEs plotted for each model separately with the model also displayed on it.

Please help.

Data file 1:

20         ! # models
1 0.1      ! Theta inc.
3 4 7      ! Approximations
2          ! # layers
GK         ! model name
1.0 1.5    ! X-axis values (length varies for other models)
1.0 1.5    ! Y-axis values (length varies for other models)
2       
GK_MOD1 
1.0 1.5 
1.5 1.0 
4
Song
2.0 3.0 3.5 4.0
2.5 1.0 0.8 1.3
6
Causse
1.5  1.8  1.9  2.0  2.1  2.2
0.25 0.40 0.60 0.20 0.50 0.55
...
...

Data file 2 (reformatted):

#   TKfull   GKfull   Padefull_3   Padefull_7   TK1   Pade1_3   Pade1_7  Pade2_3   Pade2_7   GK1   Pade3_3   Pade3_7   GK2   PadeEnd_3 PadeEnd_7   GKEnd
GK                  0.071     0.028     0.025     0.156     0.023     0.004    0.045     0.012     0.232     0.013     0.090     0.492     0.064   0.064     0.384     0.046
GK_MOD1             0.055     0.030     0.053     0.156     0.012     0.008    0.036     0.050     0.185     0.025     0.169     0.497     0.107   0.125     0.375     0.075
GK_MOD2             0.132     0.042     0.081     0.209     0.035     0.006    0.050     0.051     0.277     0.036     0.267     0.625     0.116   0.192     0.484     0.079
GK_MOD3             0.261     0.067     0.316     0.267     0.066     0.021    0.078     0.138     0.426     0.063     1.137     0.855     0.161   0.810     0.662     0.106
Song                0.082     0.045     0.042     0.163     0.021     0.005    0.027     0.028     0.151     0.050     0.130     0.486     0.156   0.094     0.360     0.106
Song_MOD1           0.087     0.049     0.069     0.171     0.013     0.006    0.025     0.037     0.148     0.051     0.216     0.520     0.175   0.155     0.382     0.120
Song_MOD2           0.109     0.048     0.028     0.179     0.021     0.005    0.030     0.024     0.170     0.048     0.084     0.527     0.161   0.062     0.391     0.110
Song_MOD3           0.073     0.043     0.029     0.145     0.017     0.003    0.024     0.018     0.134     0.049     0.092     0.440     0.156   0.066     0.325     0.105
Cooper_basin        0.031     0.034     0.030     0.105     0.012     0.003    0.016     0.018     0.085     0.055     0.089     0.306   0.139     0.065     0.228     0.093
Cooper_basin_MOD1   0.035     0.017     0.024     0.093     0.016     0.006     0.031     0.034     0.151     0.042     0.083     0.297   0.076     0.063     0.236     0.047
Cooper_basin_MOD2   0.049     0.032     0.032     0.134     0.013     0.005     0.029     0.030     0.150     0.040     0.102     0.412   0.111     0.076     0.312     0.077
Causse2000_1        0.030     0.019     0.020     0.093     0.012     0.004     0.024     0.022     0.121     0.035     0.062     0.286   0.081     0.047     0.219     0.053
Causse2000_2        0.068     0.038     0.014     0.146     0.017     0.001     0.028     0.007     0.151     0.043     0.047     0.444   0.130     0.033     0.332     0.089
Causse2000_3        0.042     0.036     0.023     0.083     0.015     0.005     0.008     0.023     0.156     0.049     0.065     0.195   0.132 0.049     0.177     0.089
Causse2000_4        0.039     0.046     0.046     0.109     0.016     0.003     0.024     0.011     0.049     0.053     0.137     0.318   0.158     0.099     0.230     0.108
Dunkin_1973         0.653     0.641     0.656     0.686     0.703     0.703     0.725     0.239     0.471     0.059     0.434     0.632   0.140     0.353     0.557     0.093
Dunkin_1973_MOD1    0.708     0.624     0.708     0.709     0.694     0.692     0.692     0.760     0.761     0.042     0.785     0.792   0.104     0.773     0.777     0.070
Dunkin_1973_MOD2    0.043     0.029     0.026     0.118     0.050     0.004     0.025     0.023     0.130     0.039     0.082     0.357   0.105     0.061     0.270     0.072
Dunkin_1973_MOD3    0.012     0.014     0.005     0.066     0.011     0.001     0.022     0.002     0.102     0.032     0.017     0.210   0.070     0.012     0.165     0.046
Random              0.033     0.039     0.042     0.114     0.013     0.001     0.017     0.014     0.096     0.047     0.138     0.361   0.140   0.099     0.268     0.097

enter image description here Transposed the Data file 2 using this code then through the following script, plotted the RMSE values as shown in the image.

Gnuplot script

set terminal png size 800,500 enhanced font "Helvetica,20"
set output 'output.png'

red = "#FF0000"; green = "#00FF00"; blue = "#0000FF"; skyblue = 
"#87CEEB";
set yrange [0:2]
set style data histogram
set style histogram cluster gap 1
set style fill solid
set boxwidth 0.9
set xtics format ""
set grid ytics
set xtics rotate by 90 right
set title "A Sample Bar Chart"
plot "RMSE_transpose.dat" using 2:xtic(1) title "GK " linecolor rgb red

The inset figure in the image is hand-drawn, which is the model itself as shown in Data file 1.

Help needed in:

  1. How to get such a plot from both the Data files for all the models, viz., GK, GK_MOD1, GK_MOD2, etc., with individual image having model name as the file name, such as, GK_RMSE.png, GK_MOD1_RMSE.png, etc.

  2. How to display the Y-axis values on top of each bar?

  3. What should I modify in the Gnuplot script to get row-wise and column-wise plots from the Data file 2?

1

There are 1 best solutions below

0
theozh On

Update: Below is now a version which works for gnuplot>=4.6.0 (March 2012). It was a bit a struggle to get it to work for gnuplot 4.6.0, since gnuplot 4.x doesn't know reset session and set datafile separator "\n", but for the rest which didn't work I can't exactly tell what it was. Something with the column header and the comment char, but seems now to be eliminated.

Here is a starting point for further optimization.

As mentioned in the comments, gnuplot doesn't like data in rows. So, plotting file1 in it's original format requires some extra lines.

A few comments:

  • get the column labels and row labels into variables (colLabels and rowLabels)
  • loop these strings/lists (check help word) for creating your graph by either columns or rows
  • for plotting the x,y rows from file1 get the lines into strings Xs and Ys
  • plot Xs versus Ys by using the special-file '+' (check help special-filenames).
  • maybe the plotting style with histeps comes closest to what I understand you want to have from file1

I hope you can figure out the rest yourself by checking help ....

There are a lot of ways to optimize the script, depending on your exact requirements and data which I don't know in detail.

You can answer your question 2 yourself as homework: see How to display actual values on top of each bar in a bar-plot?

Data:

SO77228821_1.dat (no text allowed after x,y values)

20         ! # models
1 0.1      ! Theta inc.
3 4 7      ! Approximations
2          ! # layers
GK         ! model name
1.0 1.5
1.0 1.5
2       
GK_MOD1 
1.0 1.5 
1.5 1.0 
4
Song
2.0 3.0 3.5 4.0
2.5 1.0 0.8 1.3
6
Causse2000_1
1.5  1.8  1.9  2.0  2.1  2.2
0.25 0.40 0.60 0.20 0.50 0.55

SO77228821_2.dat (shortened from OP's file2 because OP's file1 was not complete)

#   TKfull   GKfull   Padefull_3   Padefull_7   TK1   Pade1_3   Pade1_7  Pade2_3   Pade2_7   GK1   Pade3_3   Pade3_7   GK2   PadeEnd_3 PadeEnd_7   GKEnd
GK                  0.071     0.028     0.025     0.156     0.023     0.004    0.045     0.012     0.232     0.013     0.090     0.492     0.064   0.064     0.384     0.046
GK_MOD1             0.055     0.030     0.053     0.156     0.012     0.008    0.036     0.050     0.185     0.025     0.169     0.497     0.107   0.125     0.375     0.075
Song                0.082     0.045     0.042     0.163     0.021     0.005    0.027     0.028     0.151     0.050     0.130     0.486     0.156   0.094     0.360     0.106
Causse2000_1        0.030     0.019     0.020     0.093     0.012     0.004     0.024     0.022     0.121     0.035     0.062     0.286   0.081     0.047     0.219     0.053

Script: (tested with gnuplot 4.6.0 and 5.4.5)

### create graphs from row/column headers 
### and graph inset from second file with row-wise data
reset
unset multiplot

FILE1 = "SO/SO77228821_1.dat"
FILE2 = "SO/SO77228821_2.dat"

# get all row-labels and column-labels
set datafile commentschar ''
set datafile separator "|"
rowLabels = ''
stats FILE2 u ($0==0? colLabels=strcol(1) : rowLabels=rowLabels.' '.word(strcol(1),1)) nooutput
set datafile separator whitespace

rowLabel(i) = word(rowLabels,i)
colLabel(i) = word(colLabels,i)
FileOut(s)  = sprintf("SO/SO77228821_%s_RMSE.png",s)

set boxwidth 0.7
set style fill solid 0.3
set key noautotitle

set term pngcairo size 640,384 font ",10" noenhanced

# create graphs by rowLabel
do for [row=1:words(rowLabels)] {
    set output FileOut(rowLabel(row))
    set multiplot
        set size 1,1
        set origin 0,0
        set bmargin 4.5
        set xrange[*:*]
        set yrange[0:*]
        set offset 0.5,0.5,0,0
        set xtic rotate by 90 right
        set title rowLabel(row)
        plot for [i=2:words(colLabels)] FILE2 every ::row::row u (i+1):i:xtic(colLabel(i)) w boxes lc rgb "red" notitle

        set datafile separator "|"
        c=NaN
        stats FILE1 u (word(strcol(1),1) eq rowLabel(row) ? c=$0 : 0, c+1==$0 ? Xs=strcol(1) : c+2==$0 ? Ys=strcol(1) : 0 ) nooutput
        set datafile separator whitespace
        set xrange[word(Xs,1):word(Xs,words(Xs))]
        set yrange[0:*] 
        set offset 0,0,0,0
        set xtic rotate by 0 center
        set size 0.4,0.4
        set origin 0.08,0.45
        set bmargin -1
        unset title
        set samples words(Xs)
        plot '+' u (i=int($0+1),x0=real(word(Xs,i))):(y0=real(word(Ys,i))) w histeps lc rgb "blue" lw 2
    unset multiplot
    set size 1,1
    set origin 0,0
    set output
}

# create graphs by colLabel
do for [col=2:words(colLabels)] {
    set output FileOut(colLabel(col))
    set title colLabel(col)
    set datafile commentschar    # reset to default
    set xrange[*:*]
    set yrange[0:*]
    plot FILE2 u 0:col:xtic(1) w boxes lc rgb "web-green" notitle
}
set output
### end of script

Result: (just two examples out of the many files generated)

enter image description here

enter image description here