Search code examples
gnuplot

How can I pair and rename x-axis double rename 2 by 1 in gnuplot


I want to add hours and group within itself by date. It is, but want to show on x-axis. I tried this but couldn't use properly. Getting following error.

"gnuplot.gnu" line 16: undefined variable: rowstacked

Edit:I have to show data whether intersect each other. Each individual draws line is independent in my scenario. So I've to stick to individual indicator lines. I have to use times and dates over x-axis in line plot mode below.

### plot easy!
reset session
set key left box
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ffffff" behind
set datafile separator tab

set grid x y my
set xlabel "Setted"
set ylabel "Getted "
set title "Sensor Datas"
set style lines rowstacked title offset 0,-1

set autoscale

#set xtics ("12:00 PM" 0, "6:00 PM" 1, "12:00 PM" 2, "6:00 PM" 4,"12:00 PM" 5, "6:00 PM" 6 )
#plot newlines" \n group 1" , "./Sensor Datas.txt" every ::0::1, \
#     newlines "group 2", "./Sensor Datas.txt" every ::2::3


plot "./Sensor Datas.txt" u 2 t "a"    w l,\
     "./Sensor Datas.txt" u 3 t "b"    w l,\
     "./Sensor Datas.txt" u 4 t "c"    w l,\
     "./Sensor Datas.txt" u 5 t "d"    w l,\
     "./Sensor Datas.txt" u 6 t "e"    w l,\
     "./Sensor Datas.txt" u 7 t "f"    w l,

x a b c d e f
9/24/2022 12:00 PM 68.60 64.30 61.60 54.70 91.40 148.00
6:00 PM 67.08 55.26 66.50 54.05 91.20 152.00
9/25/2022 12:00 PM 70.10 71.60 64.40 59.00 85.60 154.00
6:00 PM 74.40 80.40 67.60 58.80 96.90 156.30
9/26/2022 12:00 PM 66.20 90.60 65.70 55.60 67.90 156.00
6:00 PM 78.80 107.80 66.20 56.10 58.50 153.60

enter image description here


Solution

  • As I understand your question, you have basically two questions:

    1. how to plot a rowstacked line graph? As far as I know there is no dedicated gnuplot plotting style like for histograms. You have to add the values yourself. Check help sum. There is an earlier solution using a recursive function (however, which will be limited to about 250 instances, although enough for most cases).
    2. how to use date/time info from two columns?

    Your datafile separator is tab, however, I'm not sure if it is possible to post tab separated code here on SO. Anyway, that's why I've changed it here to comma, but you can easily change it back to tab for your original data. Furthermore, you are using this very unfortunate 12 h (am/pm) time format. A far as I know, gnuplot can read it as input only from gnuplot 5.4.0 on. For earlier versions you have to use a workaround.

    Since you skip the date every second row, you have to memorize it in d0 to plot the values at the right x-(time) position. Furthermore, you create the xticlabel yourself, by a function.

    If you don't need the data points as point, simply skip the second part of the plot command. Check the example below as starting point for further optimization.

    Script: (requires gnuplot>=5.4.0, because of %p as time input format)

    ### rowstacked lineplot with time axis
    reset session
    
    $Data <<EOD
             ,         ,     a,      b,     c,     d,     e,      f
    9/24/2022, 12:00 PM, 68.60,  64.30, 61.60, 54.70, 91.40, 148.00
             ,  6:00 PM, 67.08,  55.26, 66.50, 54.05, 91.20, 152.00
    9/25/2022, 12:00 PM, 70.10,  71.60, 64.40, 59.00, 85.60, 154.00
             ,  6:00 PM, 74.40,  80.40, 67.60, 58.80, 96.90, 156.30
    9/26/2022, 12:00 PM, 66.20,  90.60, 65.70, 55.60, 67.90, 156.00
             ,  6:00 PM, 78.80, 107.80, 66.20, 56.10, 58.50, 153.60
    EOD
    
    N  = 8
    myDateFmt = "%m/%d/%Y"
    myTimeFmt = "%H:%M %p"
    
    set key out box width 1 noautotitle
    set datafile separator comma
    set grid x y my front
    set style fill solid 0.7 border
    set tics out
    set format x "\n" timedate
    
    myDate(col)       = valid(col) ? d0=timecolumn(col,myDateFmt) : d0
    myTime(col)       = timecolumn(col,myTimeFmt)
    mySecs(col1,col2) = myDate(col1) + myTime(col2)
    mySum(n)          = sum [_i=3:n] column(_i)
    myTic(col1,col2)  = strftime("%l %p",strptime(myTimeFmt,strcol(col2)))."\n".strcol(col1)
    
    plot for [col=3:N] $Data u (mySecs(1,2)):(mySum(col)):(mySum(col-1)):xtic(myTic(1,2)) \
             w filledcurves ti columnhead(col), \
         for [col=3:N] $Data u (mySecs(1,2)):(mySum(col)):(mySum(col-1)) skip 1 w lp pt 7 lc col-2
    ### end of script
    

    Result:

    enter image description here

    Addition: (line plot without summation)

    Script:

    In your original script, you were actually plotting column 2, which is the time 12:00 pm and 6:00 pm and missing the 8th column. It seems like your your date and time actually is separated by tab as well, as shown in your table.

    ### lineplot with time axis
    reset session
    
    $Data <<EOD
             ,         ,     a,      b,     c,     d,     e,      f
    9/24/2022, 12:00 PM, 68.60,  64.30, 61.60, 54.70, 91.40, 148.00
             ,  6:00 PM, 67.08,  55.26, 66.50, 54.05, 91.20, 152.00
    9/25/2022, 12:00 PM, 70.10,  71.60, 64.40, 59.00, 85.60, 154.00
             ,  6:00 PM, 74.40,  80.40, 67.60, 58.80, 96.90, 156.30
    9/26/2022, 12:00 PM, 66.20,  90.60, 65.70, 55.60, 67.90, 156.00
             ,  6:00 PM, 78.80, 107.80, 66.20, 56.10, 58.50, 153.60
    EOD
    
    N  = 8
    myDateFmt = "%m/%d/%Y"
    myTimeFmt = "%H:%M %p"
    
    set key out box width 1 noautotitle
    set datafile separator comma
    set grid x y my front
    set style fill solid 0.7 border
    set tics out
    set format x "\n" timedate
    set yrange [0:]
    
    myDate(col)       = valid(col) ? d0=timecolumn(col,myDateFmt) : d0
    myTime(col)       = timecolumn(col,myTimeFmt)
    mySecs(col1,col2) = myDate(col1) + myTime(col2)
    mySum(n)          = sum [_i=3:n] column(_i)
    myTic(col1,col2)  = strftime("%l %p",strptime(myTimeFmt,strcol(col2)))."\n".strcol(col1)
    
    plot for [col=3:N] $Data u (mySecs(1,2)):col:xtic(myTic(1,2)) w lp pt 7 lc col-2 ti columnhead(col)
    ### end of script
    

    Result:

    enter image description here