Search code examples
gnuplot

Four graphs in one picture


What is wrong please with this graph? I would like to plot four graph in one picture. It displays two graphs. I would like to have one legends with 3 titles, because there are three color used. Then I don't know how to write two titles - one for graphs in left and one for graphs in right.

I would like to have 4 differents plot 2x2 - I have 4 plot in script. I there is lots of mistake.

set tics out nomirror
set encoding iso_8859_1
unset xtics
set ylabel "{/:Italic F} [a. u.]" font "Segoe UI,12" offset 2,0
set ytics nomirror font "Segoe UI,12"
set lmargin screen 0.2 #levý prostor vedle graf
set rmargin screen 0.9 #pravý prostor vedle grafu
set multiplot layout 2,2
set bmargin screen 0.40
set key Left reverse out horiz
set format y "%.2f"
set key tc variable
set xrange [4272:4500]
set yrange [0.7:1.02]
set title "Title 1" font "Segoe UI,12"
set title "Title 2" font "Segoe UI,12"
plot \
x title "Fitted" with lines linecolor rgb "red" lw 1.5,\
x title "Measured" with lines linecolor rgb "black" lw 1.5
unset ytics
set y2tics
set link y2
unset ylabel
set y2label "{/:Italic F} [a. u.]" font "Segoe UI,12" offset 1,0
set tmargin screen 0.4 #posun horní čáry dolního graf
set bmargin screen 0.15 #posun dolní čáry dolního graf
unset key
set tics out nomirror
set xlabel "{/:Italic {/Symbol l}} ({\305})" font "Segoe UI,12"
set xrange [4272:4500]
set yrange [-0.05:0.03]
plot x title "Measured - fitted" with lines linecolor rgb "navy" lw 1.5 

set margin
set margin
plot \
x title "Fitted" with lines linecolor rgb "red" lw 1.5,\
x title "Measured" with lines linecolor rgb "black" lw 1.5
set margin
set margin
plot x title "Measured - fitted" with lines linecolor rgb "navy" lw 1.5 

Solution

  • I could see that you used some symbols from symbol font. If you want, use set encoding utf8 option and write symbols is directly. You could set font as a terminal option also. For example: set terminal pngcairo size 800,600 font "Segoe UI,8" enhanced.

    To help you to understand as margins screen works, take a look on this figure. This grid divides the screen (size 800,600 on terminal command) each 10% (0.1 screen unit).


    grid reference


    In order to have graphs with the same size and align them each other we have to set the margins of the individual graphs manually. To make it more easy I used macros commands. Below a complete example of how to create a 2×2 graph using the multiplot.

    reset
    set encoding utf8                                               # Encoding
    set terminal pngcairo size 800,600 font "Segoe UI,8" enhanced   # Terminal settings
    set output "multiplot_2x2.png"                                  # Output file name
    
    set grid ls -1 lc "gray"        # grid lines
    set tics out nomirror           # tics marks
    
    # Line styles
    set style line 1 lc "#e41a1c"   # red
    set style line 2 lc "#377eb8"   # blue
    set style line 3 lc "#4daf4a"   # green
    set style line 4 lc "#984ea3"   # purple
    
    # Margins for each row and column
    Row1 = "set tmargin screen 0.90; set bmargin screen 0.56"   # Top and bottom margins
    Row2 = "set tmargin screen 0.42; set bmargin screen 0.08"
    Col1 = "set lmargin screen 0.08; set rmargin screen 0.48"   # Left and right margins
    Col2 = "set lmargin screen 0.57; set rmargin screen 0.97"
    
    # Multiplot option with main title
    set multiplot layout 2,2 rowsfirst title "{/:Bold=12 Multiplot 2×2}"
    # -------------------------------------------------------------------
    @Row1; @Col1                            # Calling the macros
    set title "{/:Bold=10 Row 1, Col 1}"    # Title for plot
    set xrange [-10:10]                     # x-range
    set yrange [0:50]                       # y-range
    set xtics 2                             # Increment for x-tics
    set ytics 10                            # Increment for y-tics
    set xlabel "Crazy distance / Å"         # x-label
    set ylabel "Crazy values / a.u."        # y-label
    plot x**2 w l ls 1 title "x^{2}"        # The plot
    # -------------------------------------------------------------------
    @Row1; @Col2
    set title "{/:Bold=10 Row 1, Col 2}"
    set xrange [-15:20]
    set yrange [-4000:8000]
    set xtics 5
    set ytics 2000
    set xlabel "Crazy temperature / °C"
    set ylabel "Crazy pressure / Pa"
    plot x**3 w l ls 2 title "x^{3}"
    # -------------------------------------------------------------------
    @Row2; @Col1
    set title "{/:Bold=10 Row 2, Col 1}"
    set xrange [-15:15]
    set yrange [-0.4:1.2]
    set xtics 5
    set ytics 0.2
    set xlabel "Crazy energy / kJ"
    set ylabel "Crazy volume / m^{3}"
    plot sin(x)/x w l ls 3
    # -------------------------------------------------------------------
    @Row2; @Col2
    set title "{/:Bold=10 Row 2, Col 2}"
    set xrange [0:14]
    set yrange [-10:15]
    set xtics 2
    set ytics 5
    set xlabel "Crazy value {/:Italic N}_{A} / 10^{23}"
    set ylabel "Crazy property / cd sr kg^{−1} m^{−2} s^{3}"
    plot cos(x)*x w l ls 4
    # -------------------------------------------------------------------
    unset multiplot
    

    The result:

    Result

    Exactly the same result can be achieved using approach on @Ethan's answer, with the advantage dismiss macros use, just write:

    set multiplot \
        layout 2,2 rowsfirst \
        title "{/:Bold=12 Multiplot 2×2}" \
        margins screen 0.08,0.97,0.08,0.90 \
        spacing screen 0.09,0.14