There are a lot of blank areas in the image generated by using gnuplot

46 Views Asked by At

When I use gnuplot to draw a picture, there is a lot of blank space in the bottom, I would like to make that space disappear or at least make it as minimum as possible. How can I do it by modifying my code?

enter image description here

My code is as follows:

set terminal postscript portrait color enhanced 15
set output "sites_nh3-2-daily_v3.ps"
set multiplot
set size 1, 0.2

cd "../../obs_mod/daily/"

set lmargin 3
set rmargin 1
set tmargin 2
set bmargin 1.5

set datafile missing ''
set datafile missing '-'

set autoscale
set xdata time
set timefmt "%Y%m%d"
set format x "%m/%d"
set xrange["20220601":"20221230"]
set ytics offset 0.5,0

set ytics nomirror
set ytics font ",12"
set ytics offset 0.5,0
#set autoscale y                       # 根据数据动态调整y轴范围
set yrange [0:40]
set ytics 0,8,40
set xtics nomirror
set xtics font ",12"
set xtics offset 0.3,0
set border
set origin 0.0, 0.83
set label "JM-XQ" left at graph 0.03, graph 0.92 font ",12"
unset xtics
set key left at graph 0.72,0.94 font ",12"
set key reverse
plot "JM-xiqu.txt" u 1:($2) w p lc -1 pt 7 ps 0.3 t "obs", "" u 1:3 w l lt 1 lc rgb "red" lw 1 t "mod\\\_base", "" u 1:4 w l lt 1 lc rgb "blue" lw 1 t "mod\\\_agr-NH_3"
unset label

set origin 0.0, 0.7033
set label "SZ-LHS" left at graph 0.03, graph 0.92 font ",12"
unset key
set yrange [0:10]
set ytics 0,2,8
plot "SZ-lianhuashan.txt" u 1:($2) w p lc -1 pt 7 ps 0.3 t "Obs", "" u 1:3 w l lt 1 lc rgb "red" lw 1 t "mod_base", "" u 1:4 w l lt 1 lc rgb "blue" lw 1 t "mod\\\_noNH_3"
unset label

set origin 0.0, 0.576
set label "ZH-MH" left at graph 0.03, graph 0.92 font ",12"
unset key
set yrange [0:20]
set ytics 0,4,16
plot "ZH-meihua.txt" u 1:($2) w p lc -1 pt 7 ps 0.3 t "Obs", "" u 1:3 w l lt 1 lc rgb "red" lw 1 t "mod_base", "" u 1:4 w l lt 1 lc rgb "blue" lw 1 t "mod\\\_noNH_3"
unset label

set origin 0.0, 0.449
set label "ZQ-SHWZ" left at graph 0.03, graph 0.92 font ",12"
unset key
set yrange [0:20]
set ytics 0,4,16
plot "ZQ-sihuiweizheng.txt" u 1:($2) w p lc -1 pt 7 ps 0.3 t "Obs", "" u 1:3 w l lt 1 lc rgb "red" lw 1 t "mod_base", "" u 1:4 w l lt 1 lc rgb "blue" lw 1 t "mod\\\_noNH_3"
unset label

set origin 0.0, 0.322
set label "ZS-ZML" left at graph 0.03, graph 0.92 font ",12"
unset key
set yrange [0:15]
set ytics 0,3,12
set xtics nomirror
set xtics font ",12"
set xtics offset 0.3,0
set arrow from "20220701",0 to "20220701",75 nohead lt 0 lc rgb "gray0"
set arrow from "20220801",0 to "20220801",75 nohead lt 0 lc rgb "gray0"
set arrow from "20220901",0 to "20220901",75 nohead lt 0 lc rgb "gray0"
set arrow from "20221001",0 to "20221001",75 nohead lt 0 lc rgb "gray0"
set arrow from "20221101",0 to "20221101",75 nohead lt 0 lc rgb "gray0"
set arrow from "20221201",0 to "20221201",75 nohead lt 0 lc rgb "gray0"
plot "ZS-zimaling.txt" u 1:($2) w p lc -1 pt 7 ps 0.3 t "Obs", "" u 1:3 w l lt 1 lc rgb "red" lw 1 t "mod_base", "" u 1:4 w l lt 1 lc rgb "blue" lw 1 t "mod\\\_noNH_3"
unset label

How should I set it in the code so that the generated picture will automatically not show the blank area?

2

There are 2 best solutions below

0
Ethan On

You are giving the program explicit instructions how to place the lower limit of each plot:

set origin 0.0, 0.83
set origin 0.0, 0.7033
set origin 0.0, 0.576
set origin 0.0, 0.449
set origin 0.0, 0.322

If you want to move everything down, you will have to give smaller numbers for the y offsets.

On the other hand, if you are asking how to reserve less vertical space at the lower part of each one-fifth (from set size 1.0, 0.2) sub-plot, then maybe you want to replace the command set bmargin 1.5 with set bmargin 0. That will reduce appoximately 1.5 character heights of space at the bottom of each subplot.

0
theozh On

As Ethan already mentioned, you are setting the sizes and origins of the subplots manually. There is a more convenient option in gnuplot. Check the option spacing and margins in help multiplot:

Syntax:

  set multiplot
      { title <page title> {font <fontspec>} {enhanced|noenhanced} }
      { layout <rows>,<cols>
        {rowsfirst|columnsfirst} {downwards|upwards}
        {scale <xscale>{,<yscale>}} {offset <xoff>{,<yoff>}}
        {margins <left>,<right>,<bottom>,<top>}
        {spacing <xspacing>{,<yspacing>}}
      }

Some comment:

  • since you want zero spacing between the subplots, depending on the ytics it might happen that the first y-tic of the upper plot and the last y-tic of the lower plot might overlap. You avoided this, e.g. in the second plot, by explicitly setting set ytics 0,2,8 although you have a range of set yrange[0:10].With this, you hide the 10-tic. Another way would be to use autotics but remove the 10 via set ytics add ('' 10). I don't have yet a good idea when using autoscale/autotics but only remove the last tic label without knowing in advance what the range or value is.

  • the multiplot margins by default are given in screen coordinates, however, you can also give the bottom margin in character dimensions char 2 to provide just enough space for the xtic labels.

Check the following minimized and simplified example. I hope you can adapt it to your needs.

Script: (works for gnuplot>=5.0)

### multiplots with zero spacing and only one x-axis label
reset session

set key top left
set xrange[0:100]
set format x ' '
set yrange[0:1.5]
set ytics 0.5
set key noautotitle
set grid x,y

set multiplot layout 5,1 spacing 0,0 margins 0.07, 0.97, char 2, 0.98

    set label 1 "JM-XQ" at graph 0, graph 1 offset 1.5,-0.7
    plot '+' u 0:(rand(0)) w l ls 1
    
    set label 1 "SZ-LHS"
    set ytics add ('' 1.5)
    plot '+' u 0:(rand(0)) w l ls 2
     
    set label 1 "ZH-MH"
    plot '+' u 0:(rand(0)) w l ls 3
    
    set label 1 "ZQ-SHWZ"
    plot '+' u 0:(rand(0)) w l ls 4
    
    set label 1 "ZS-ZML"
    set format x "%g"
    plot '+' u 0:(rand(0)) w l ls 5

unset multiplot
### end of script

Result:

enter image description here