Sunday, May 10, 2020

Gnuplot - Multiplot

Gnuplot has the option to combine multiple graphs in a layout.  The following picture has the terminology.

  • The whole map will be referenced as (0,0) starting with bottom-left, stretching upto (1,1) till top-right.
  • If we need to embed a graph inside this layout.  We have to specify the origin of the graph, i.e. where the top left of graph to be placed.  Also, we specify the size, i.e. how much area it is going to occupy.  All this has to be with reference to (0,0) and (1,1) size of multiplot.
  • From the picture you can see the margins: left margin(lmargin), right margin(rmargin), top margin(tmargin), bottom margin(bmargin).  These has to be mentioned starting from bottom left position of the layout.

Offset

How much ever we try to plot a layout, there will always be adjustment that has to be done for Labels, tics, titles.  In this case, we use the offset that specifies the alignment of that entity with reference from its default position.  

It definitely takes a trial and error approach to make a desired multiplot.  

Syntax

set multiplot { layout <rows>,<cols>
                {rowsfirst|columnsfirst} {downwards|upwards}
                {title <page title>}
                {scale <xscale>{,<yscale>}}
                {offset <xoff>{,<yoff>}}
              }
unset multiplot

Example

Following are the 4 data files

file1.dat

Time, read1,read2,read3
20,    4.9, 17.1, 78.1 
25,    4.0, 22.0, 74.0 
30,    2.0, 17.0, 81.0 
35,   11.5, 21.7, 66.8 
40,    4.7, 18.0, 77.4 
45,    3.8,  8.9, 87.3 
50,    0.6, 17.3, 82.1 
55,    2.0,  3.4, 94.6 
60,    1.0,  1.3, 97.6 

file2.dat

Time, read1,read2,read3
20,   5.3, 20.6, 74.2
25,   9.2, 27.2, 63.6
30,   9.5, 20.3, 70.2
35,   9.9, 22.1, 68.0
40,   5.3, 19.0, 75.7
45,   3.4,  9.6, 86.9
50,   2.3, 15.3, 82.4
55,   2.7, 10.6, 86.7
60,   1.7,  1.0, 97.3

file3.dat

Time, read1,read2,read3
20,    6.8, 20.6, 72.6
25,    6.2, 29.5, 64.3
30,    5.3, 23.6, 71.1
35,    4.5, 15.6, 79.9
40,    5.5, 17.4, 77.1
45,    3.7, 10.5, 85.8
50,    9.1, 16.6, 74.3
55,    2.8,  3.8, 93.4
60,    1.0,  1.6, 97.4

file4.dat

Time, read1,read2,read3
20,    9.1, 24.2, 66.7
25,    5.1, 26.2, 68.7
30,    9.2, 25.9, 64.9
35,    4.4, 18.4, 77.2
40,    7.6, 17.3, 75.2
45,    4.8,  8.2, 87.0
50,    2.0,  2.1, 95.9
55,    2.6, 10.3, 87.1
60,    2.8,  0.6, 96.6

Script is
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
set terminal pngcairo
set output "readings.png"

set style data histogram
set style histogram rowstacked
set yrange [0:100]
set xtics 5
set style fill solid
set key outside
set boxwidth 0.5 relative

set datafile separator ","

set multiplot layout 4,1 title "MultiArea\nReadings" font ",8" offset 21,0

set ytics border font ",6" offset 0.7,0
set key title 'Readings' font ",6"
set key font ",5"
set key bottom right

set size 1,0.25
set origin 0,0.25
set lmargin at screen 0.1
set tmargin at screen 0.23
set bmargin at screen 0.05
set rmargin at screen 0.85
set title "Area4" font ",6" offset 0,-0.7
set xtics border font ",6" offset 0,0.7
plot for [COL=2:4] 'file4.dat' using COL:xtic(1) ti col
unset xtics

set size 1,0.25
set origin 0,0.50
set lmargin at screen 0.1
set tmargin at screen 0.48
set bmargin at screen 0.27
set rmargin at screen 0.85
set title "Area3" font ",6" offset 0,-0.7
plot for [COL=2:4] 'file3.dat' using COL:xtic(1) ti col
unset key

set size 1,0.25
set origin 0,0.75
set lmargin at screen 0.1
set tmargin at screen 0.73
set bmargin at screen 0.52
set rmargin at screen 0.85
set title "Area2" font ",6" offset 0,-0.7
plot for [COL=2:4] 'file2.dat' using COL:xtic(1) ti col

set size 1,0.25
set origin 0,0.95
set lmargin at screen 0.1
set tmargin at screen 0.97
set bmargin at screen 0.77
set rmargin at screen 0.85
set title "Area1" font ",6" offset 0,-0.7
plot for [COL=2:4] 'file1.dat' using COL:xtic(1) ti col

unset multiplot
exit

Going through the steps:
  • Line 1-2: The output has to be an image png file
  • Lines 4-10: Histogram settings common for all the 4 plots.
  • Line 14: Title of the graph.  Offset is used to print it in the top right position.
  • Line 17-19: Individual plots key information display settings
  • Line 21: The size of the Area4 plot 
  • Line 22: The starting position of the Area4 plot
  • Line 23-26: Margins of the plot
  • Line 27: Title of the plot
  • Line 28: Font settings and position of Xtic labels
  • Line 29: Setting the plot
  • Line 30: Unsetting xtics so that xtic labels do not appear in all the above plots
  • Line 32-58: Drawing the other Area plots in the multiplot
  • Line 60: End of multiplot
  • Line 61: Only after exit is called, the output image file will be ready for viewing
Resultant graph is

No comments:

Post a Comment