0

I have the following data readout of a smart gasmeter, the file contains data of a full month.

date;time;natural gas consumption cumulative
2023-03-01;00:00;5434.291
2023-03-01;00:01;5434.292
2023-03-01;00:02;5434.293
2023-03-01;00:03;5434.294
2023-03-01;00:04;5434.295
...
...
2023-03-02;00:00;5435.123
2023-03-02;00:01;5435.124
2023-03-02;00:02;5435.125
2023-03-02;00:03;5435.126
2023-03-02;00:04;5435.127

I would like to plot a graph of the gas consumption per hour as a bar chart for the full month. Would like to have a separate bar per hour with the value of gas used in that hour. How to do this in Gnuplot?

gnuplot << eor
set xtics rotate # displays labels on x-axis vertical
set y2tics # y-ax values also on the right

set grid ytics mytics mxtics xtics  # draw lines for each ytics and mytics
set mytics 2           # set the spacing for the mytics
#set mxtics 2
set grid               # enable the grid


set xdata time
set timefmt '%Y-%m-%d;%H:%M'
set datafile separator ';'
set terminal pngcairo size 1400,600 enhanced font 'Helvetica,12'
set output '/dev/shm/images/p1_data_gas.png'

# create line chart
set style data line
set style line 2 lt 1 lw 1 pt 1 linecolor rgb "orange-red"set title "Gas consumption"
set xlabel "Time \n measurement interval (1 minute)"
set ylabel "Gas consumtion (m³)"

plot '/home/pi/p1_data.csv' using 1:3 title "Gas cumulative"
eor

This creates a line chart with data of a full month in an increasing line (as expected).

WOU
  • 1
  • 1
  • Welcome to StackOverflow! It looks like your data is in steps of 1 minute. Do you have strictly regular data points for every minute for the full month or could the intervals sometimes maybe be larger because of some missing data? – theozh Mar 11 '23 at 19:21
  • There is data for ever minute, there should not be missing data. – WOU Mar 12 '23 at 07:16

1 Answers1

0

You have data for the cumulative counter value for every minute. So, the straightforward method to get the consumption per time interval would be:

  • plot the derivative of your data, which is nothing else than consumption per timestep.

Since your timesteps are minutes but you want the hourly consumption, a simple derivative multiplied by 60 would give you the hourly consumption, but for each minute. However, I assume you want the hourly consumption only for every hour.

Hence, the logical step would be

  • to resample the data with hourly timesteps

However, unfortunately gnuplot has no resample function (except a cumbersome workaround). In your case I guess you don't need strict hourly data but it might be sufficient to consider the data points whenever the hour changes.

The following example will

  • create some random test data (you can skip this part and take your datafile instead of $Data)
  • write the first data point of the cumulative data, and every data point when the hour changes (t0!=t1), and finally the last data point into a new table. set datafile missing NaN will neglect the other data points.
  • plot the derivative of your hourly datablock

The time data in the datablock $HourChange will be in seconds from January, 1st 1970. If you need more explanations, let me know.

Script: (requires gnuplot>=5.2.2 because set table ... separator ";")

### plot hourly rate of cumulative minute input data
reset session

myFmt = "%Y-%m-%d;%H:%M"
set datafile separator ";"

# create some random test data
set print $Data
    t0 = strptime(myFmt,"2023-03-01")
    y0 = rand(0)*2000+2000
    secPerDay = 24*3600
    do for [t=0:31*secPerDay:1200] {    # data point every 20 minutes for 31 days
        print sprintf("%s;%.3f",strftime(myFmt,t0+t),y0=y0+rand(0)*(sin(t/2./secPerDay)+1))
    }
set print

myFmtH = "%Y-%m-%d;%H"      # timeformat in hourly precision
set datafile missing NaN
set table $HourChange separator ";"
    plot t1=NaN $Data u (n=$0, y1=$3, t0=t1, t1=timecolumn(1,myFmtH), t0!=t1 ? \
            (sprintf("%.0f;%.3f",t1,y1)) : NaN) w table, \
        '+' u (sprintf("%.0f;%.3f",t1,y1)) every ::::0 w table    # plot the last data point
unset table

set key top left noautotitle
set xrange [:] noextend
set xtics out
set ylabel "consumption per hour"
set ytics nomirror
set y2label "counter"
set y2tics
set grid x,y
set format x "%d\n%B" timedate
set style fill solid 1.0 noborder

plot t1=y1=NaN $HourChange u (t0=t1,t1=$1,dt=(t1-t0),(t0+t1)/2.): \
         (y0=y1,y1=$2,(y1-y0)/dt*3600) w boxes ti "hourly consumption", \
     $Data u (timecolumn(1,myFmt)):3 w l axis x1y2 lc "red" lw 2 title "cumulative"
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Use an older version of Gnuplot, but will upgrade asap and test the code. this looks exactly like what I need. – WOU Mar 12 '23 at 23:20