Search code examples
gnuplot

Gnuplot: add a filled rectangle to highlight region of plot (date/time on x axis)


I have a file with the following contents that looks like the following line. Data is comma delimited, but the first column has the Date Time as follows:

20201-03-08 18:03:01, SAT1, 1002, 900, 10
.
.
.

Here is my plot of my data:

enter image description here

Here is what I would like to have: two regions highlighted, with labels. First region would span from +/-50Hz on Y axis and all along the x-axis. Second region would be everything else.

Wanted Plot Features:

enter image description here

When I try to simply draw a rectangle, I could do set object rectangle from 0, 50 to *,-50 but that doesn't work... Any advice ? I think I can figure out the rest if somebody could help me how to specify x-range if it is Date/Time....

EDIT: Here is my full gnuplot code:

set term pngcairo size 1600,900
set datafile sep ','
set datafile missing "NaN"

set key font ',17'
set key left top
#set key opaque

set object 1 rectangle from graph 0,50 to graph 1,-50
set object 1 fillstyle solid fillcolor "light-green"
set object 1 behind

set title "Offsets adjust" font 'Ubuntu-Mono-Regular, 24'

set timestamp "Graph Generated (UT):  \n%Y-%m-%d   %H:%M:%S" top font 'Arial,18'
set timefmt "%Y-%m-%d %H:%M:%S"
set xdata time
set xlabel "Date\nTime" font 'Consolas,12'
set ylabel 'Amount of Change (Hz)' font 'Consolar,16'
set xtics font 'Consolas,16'
set ytics 20 font 'Consolas,16'
set yrange [-150:150]
set grid
set border back

set xzeroaxis linetype 2 linewidth 2.5

set output "/home/temp/temp/abode/beacon_offsets.png"

plot "/home/temp/temp/abode/SAT_1_2200_offset_trend.txt" using 1:7 title 'SAT_1 Offset' with points ps 2 lw 4 lc 8, \
    "/home/temp/temp/abode/SAT_1_2200_offset_trend.txt" using 1:7:5 with labels font ',18' offset 1,1 notitle, \
    "/home/temp/temp/abode/SAT_2_2200_offset_trend.txt" using 1:7 title 'SAT_2 Offset' with points ps 2 lw 4 lc 7, \
    "/home/temp/temp/abode/SAT_2_2200_offset_trend.txt" using 1:7:5 with labels font ',18' offset 1,1 notitle, \
    "/home/temp/temp/abode/SAT_3_2200_offset_trend.txt" using 1:7 title 'SAT_3 Offset' with points ps 2 lw 4 lc 6, \
    "/home/temp/temp/abode/SAT_3_2200_offset_trend.txt" using 1:7:5 with labels font ',18' offset 1,1 notitle, \
    "/home/temp/temp/abode/SAT_4_2200_offset_trend.txt" using 1:7 title 'SAT_4 Offset' with points ps 2 lw 4 lc 5, \
    "/home/temp/temp/abode/SAT_4_2200_offset_trend.txt" using 1:7:5 with labels font ',18' offset 1,1 notitle, \
    "/home/temp/temp/abode/SAT_5_2200_offset_trend.txt" using 1:7 title 'SAT_5 Offset' with points ps 2 lw 4 lc 4, \
    "/home/temp/temp/abode/SAT_5_2200_offset_trend.txt" using 1:7:5 with labels font ',18' offset 1,1 notitle, \
    "/home/temp/temp/abode/SAT_6_2200_offset_trend.txt" using 1:7 title 'SAT_6 Offset' with points ps 2 lw 4 lc 3, \
    "/home/temp/temp/abode/SAT_6_2200_offset_trend.txt" using 1:7:5 with labels font ',18' offset 1,1 notitle

Here is a screenshot:

enter image description here


Solution

  • In this case you want to fill the entire width of the plot, so you can use graph rather than the time coordinates along the x axis. The y axis (actually the "first y axis") coordinates can be used directly. You also want to make sure the rectangle is drawn behind the plot.

    set object 1 rectangle from graph 0, first 50 to graph 1, first -50
    set object 1 fillstyle solid fillcolor "light-green"
    set object 1 behind