Setting xrange in gnuplot producing strange result - csv

I was trying to use gnuplot to graph a CSV file containing date-time and temperature but it was producing some strange results when it worked (mainly just one line straight up in the middle of the graph). This is the code:
set xdata time
set timefmt '%Y-%m-%d %H:%M:%s'
set xrange["2013-05-29 00:00:00":"2013-06-04 00:00:00"]
set datafile separator ','
plot 'weather.csv' using 1:2
This is a sample of the data:
2013-05-29 18:30:00,20.0
2013-05-29 21:29:00,14.0
2013-05-29 22:29:00,13.0
2013-05-29 23:29:00,12.0
2013-05-30 08:28:00,13.0
2013-05-30 09:30:00,14.0
It was getting an error:
Can't plot with an empty x range!
So I typed the commands at the command line:
gnuplot> set xdata time
gnuplot> set timefmt '%Y-%m-%d %H:%M:%s'
gnuplot> set xrange["2013-05-29 00:00:00":"2013-06-04 00:00:00"]
gnuplot> show xrange
set xdata time
set xrange [ "1970-01-01 00:00:-946684800" : "1970-01-01 00:00:-946684800" ] noreverse nowriteback
gnuplot> show
What am I doing wrong?
Thanks

It's your timefmt definition.
According to this documentation, %s is interpreted as
seconds since the Unix epoch (1970-01-01 00:00 UTC)
This explains the output from your show xrange as well. For this date interpretation, your xrange will come up empty.
If you use %S (second, 0-60) instead, your example will plot fine:
set timefmt '%Y-%m-%d %H:%M:%S'

Related

How do I perform a command substitution in tclsh?

I want to capture the stdout of a command in a variable, similarly to command substitution in Bash:
#!/bin/bash
x="$(date)"
echo $x
I tried doing the same in tclsh but it doesn't do what I want:
#!/bin/tclsh
set x [date]
echo $x
If I execute the script withtclsh myscript.tclsh it gives an error:
invalid command name "date"
while executing
"date "
invoked from within
"set x [ date ]"
On the other hand, if I open a TCL interactive shell with tclsh, it does not give an error and the echo line prints an empty string.
Why is my program giving different results when I execute the script with or without the REPL? And it there a way to capture the output of a shell command and store it in a variable, similarly to command substitution in Bash?
When not using Tcl interactively, you need to explicitly use the exec command to run a subprocess.
set x [exec date]
# Tcl uses puts instead of echo
puts $x
In interactive use, the unknown command handler guesses that that's what you wanted. In some cases. Be explicit in your scripts, please!
You should probably replace running a date subprocess with the appropriate calls to the built-in clock command:
# Get the timestamp in seconds-from-the-epoch
set now [clock seconds]
# Convert it to human-readable form
set x [clock format $now -format "%a %d %b %Y %H:%M:%S %Z"]
(That almost exactly matches the output of date on this system. The spacing isn't quite the same, but that doesn't matter for a lot of uses.)

All points y value undefined gnuplot

I'm having trouble with a time based graph on gnuplot. My graph plots the data of a CSV file from a noise sensor. My CSV file is written in the following format:
Time,Decibel
08:00:28,56.5
08:00:30,55.5
08:00:31,59.6
08:00:33,61.8
And so on.
My gnuplot script looks like this:
set datafile separator ","
set autoscale fix
set key outside right center
set xdata time
set timefmt '"%H:%M:%S"'
set xrange ['"08:00"':'"18:00"']
set terminal png
set output "/home/pi/Desktop/Codes/Graph/Picture/graph.png"
set title "Graph"
plot '/home/pi/Desktop/Codes/Graph/Values/2017-02-08.csv' using 1:2 with lines
What am I doing wrong? And is it possible to set the X to be every hour between 08:00 to 18:00?
that seems to be caused by the double quotes in:
set timefmt '"%H:%M:%S"'
set xrange ['"08:00"':'"18:00"']
try something like:
set timefmt "%H:%M:%S"
set xrange ["08:00:00":"18:00:00"]
As for the x-tics, one can set the beginning in the same notation as the xrange and then specify the step in seconds. For example to show a tic every 2 hours:
set xtics "08:00:00",2*3600
(Or, in fact, even set xtics "08:00:00","02:00:00" seems to work)

Plotting CSV with semi-colons and time formatted data with Gnuplot

I have 2 CSV files with over 80k strings in each.
The first file have this structure:
12.11.12 - 00:59:58;428,8;
12.11.12 - 00:59:59;428,9;
...
12.11.12 - 21:53:32;592,7;
12.11.12 - 21:53:35;596,4;
...
14.11.12 - 12:31:41;510,0;
14.11.12 - 12:31:41;510,0;
And the second have another scructure:
1;428.9;
1;428.9;
5;428.9;
...
117109;673.6;
117110;672.8;
117111;672.8;
...
214241;497.2;
214241;497.2;
214258;507.3;
How I can plot both of this CSV files in Gnuplot?
P.S. The first column must be x and the second must be y.
First, apparently you can set the delimiter thus:
set datafile separator ";"
Then set the time format for your first file, and set x to be a time axis:
set timefmt "%d.%m.%y - %H:%M:%S"
set xdata time
Plot the first file
plot "data1.csv" using 1:2
The second file x values don't seem to have a date format, but instead perhaps seconds elapsed? For that, just do
set datafile separator ";"
plot "data2.csv" using 1:2
and don't set xdata time. Then you should have an x axis in seconds. If you need to plot both at the same time, it would be simplest to pre-process one to look like the other.

Plotting using a CSV file

I have a csv file which has 5 entries on every row. Every entry is whether a network packet is triggered or not. The last entry in every row is the size of packet. Every row = time elapsed in ms.
e.g. row
1 , 0 , 1 , 2 , 117
How do I plot a graph for e.g. where x axis is the row number and y is the value for e.g. 1st entry in every row?
This should get you started:
set datafile separator ","
plot 'infile' using 0:1
You can also plot to a png file using gnuplot (which is free):
terminal commands
gnuplot> set title '<title>'
gnuplot> set ylabel '<yLabel>'
gnuplot> set xlabel '<xLabel>'
gnuplot> set grid
gnuplot> set term png
gnuplot> set output '<Output file name>.png'
gnuplot> plot '<fromfile.csv>'
note: you always need to give the right extension (.png here) at set output
Then it is also possible that the ouput is not lines, because your data is not continues. To fix this simply change the 'plot' line to:
plot '<Fromfile.csv>' with line lt -1 lw 2
More line editing options (dashes and line color ect.) at:
http://gnuplot.sourceforge.net/demo_canvas/dashcolor.html
gnuplot is available in most linux distros via the package manager (e.g. on an apt based distro, run apt-get install gnuplot)
gnuplot is available in windows via Cygwin
gnuplot is available on macOS via homebrew (run brew install gnuplot)

How can I create an RFC 3339 timestamp in TCL?

I inherited a TCL script (I have zero familiarity with the language) and need to add an RFC 3339 timestamp to it:
2012-04-05T12:13:32.123456-08:00
After searching Google, I haven't found any means of displaying the microseconds or the timezone offset (I have found a way to show the timezone name, but that doesn't help).
Is there a way to do this without calling an external process?
In TCL8.5, you can try the following command:
% clock format [clock seconds] -format "%Y-%m-%dT%T%z"
2012-04-05T16:06:07-0500
That gives you everything except the sub-second resolution. The clock microseconds command will give you the time in microseconds, but I can't find a format string identifier that matches it. You can use this to build your own command from scratch:
proc timestamp_rfc3339 {} {
set us [clock microseconds]
set sec [expr {$us / 1000000}]
set micro [expr {$us % 1000000}]
set ts [clock format $sec -format "%Y-%m-%dT%T"]
regexp {(...)(..)} [clock format $sec -format "%z"] matched tzh tzm
return [format "%s.%06s%s:%s" $ts $micro $tzh $tzm]
}
Running this results in a timestamp like 2012-04-05T16:35:06.366378-05:00.
Edit: Updated code sample to incorporate user1179884's tweaks (see comments) and to wrap in a proc.