2
votes

Here is my code:

set title "Samples plot"
set xlabel "Samples"
set ylabel "Volts"
set yrange [15:30]
set grid
unset mouse
set timestamp
set key center left
plot "py.out" using 1:2 skip 2 with lines lw 2 t "Battery", \
     "py.out" using 1:3 skip 2 with boxes t "Inverter", \
     26.0 t "Inverter on", \
     24.8 lw 4 t "Nominal", \
     23.0 t "Inverter off"

The data file looks something like this:

1     22.50    0
2     22.56    0
  (etc, until the inverter turns on at 26) 
5199  25.85    0
5200  26.45    23
5201  24.80    23
  (etc, until it drops below 23 - then the controller cuts it off)

Obviously, the sample number is the independent variable. The sampling runs at about every 2 seconds.

I am mostly where I want things to be, except for one nagging question about Gnuplot.

Here is the plot I am getting:

enter image description here

So my question is:

How can I utilize Gnuplot to convert the sample numbers into times? This system takes about 1800 samples per hour. I am willing to add code that sets the first sample to be a specific time and to adjust the number of samples per hour to dial it in to match actual times.

More information - New Day:

So today I have 9,000 samples covering from 6:00 to 11:00, and my plot looks like this:

enter image description here

Then with the plot command changed to this:

    plot "py.out" using (int($1/1800)+6):2 skip 2 with lines lw 2 t "Battery", \
         "py.out" using (int($1/1800)+6):3 skip 2 with boxes t "Inverter", \

Using the same data, I get a graph that looks like this:

enter image description here

Doing math within the plot command is causing it to change more than just the axis markings.

2
This question contains unnecessary level of detail. Consider that many of us have limited time to help on SO, so narrowing down your question to the actual problem you need solved and a providing minimum working example of your code will improve the chances that someone will help you. Incidentally, it will also improve the chances that this question and any eventual answers will be useful to other people too.Miguel
Thank you for your comment, @Miguel. You will see the changes you suggested. Upvote.SDsolar
you might want to remove the call to int in int($1/1800)+6 - otherwise, all data points within one hour are squashed into one vertical line segment as seen on, e.g., the red line in your last plotewcz
That makes sense. Thanks for the tip. Upvote.SDsolar

2 Answers

1
votes

[solved]

Rather than have GNUPLOT handle this alone with math, I first changed the way the data is coming in then let GNUPLOT process it.

Originally I was using this simple python program to save it to a file for plotting:

import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
while 1:
    c = ser.readline()
    f = open("py.out","a")
    f.write(c)
    f.close

So I changed it to include the time in the incoming data stream:

import serial
from datetime import datetime
ser = serial.Serial("/dev/ttyUSB0",9600)
while 1:
    c = str(datetime.now().time()) + '\t' + ser.readline()
    f = open("py.out","a")
    f.write(c)
    f.close

The data looks like this:

07:01:37.06514  1    24.39    0      <--  \t in between each
07:01:39.10456  2    24.42    0
   etc

Then in GNUPLOT the code is:

set title "28 Dec 16"
set xlabel "Time"
set ylabel "Volts"
set yrange [15:32]
set grid
unset mouse
unset log
set timestamp
set key bottom left

set xdata time                    <---  flag independent variable as time
set timefmt '%H:%M:%S'            <---  timefmt is used for reading the file
set xtics format '%H:%M'          <---  format for display on plot output

plot "py.out" using 1:3 skip 2 with lines lw 2 t "Battery", \
     "py.out" using 1:4 skip 2 with boxes t "Inverter", \
     29.5 t "PWM LIMIT", \
     24.8 lw 6 t "NOMINAL", \
     23.8 t "Inverter limit"

Now the plot looks like this: enter image description here

The answer is to provide the data to GNUPLOT initially.
Then GNUPLOT has the tools to process it properly.

GNUPLOT is awesome. Much easier than SAS.

This was a real learning experience.

Thanks to all who looked at this.

1
votes

Just

 set xtics format "%H:%M" timedate

and use a "using" specifier that converts the sample number into the number of seconds since midnight (actually since Jan. 1st 1970, 0:00h, but that doesn't matter here, as you don't display a date). The specifier using (int($1/1800)+6) is complete nonsense, i don't understand what you expected that to do.