All points y value undefined gnuplot - csv

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)

Related

GNUplot script that works with tab seperated .txt but not with .csv even when the datafile separater is changed

I have a GNUplot script that works perfectly with tab separated data, but my data comes in .csv files and it would be really handy to read them direct, any ideas?
Here is a sample of data in comma seprated.csv format, the other is exactly the same but in a .txt and obviously tab by a comma rather than a tab
HEADER
HEADER
Timestamp,Date,Time,Value,Units
1413867843,21/10/2014,05:04:03,0.053,µA
1413867243,21/10/2014,04:54:03,0.091,µA
1413866643,21/10/2014,04:44:03,0.084,µA
1413866043,21/10/2014,04:34:03,20.000,µA
1413846241,20/10/2014,23:04:01,0.041,µA
1413845641,20/10/2014,22:54:01,0.056,µA
1413845041,20/10/2014,22:44:01,0.123,µA
1413844441,20/10/2014,22:34:01,20.000,µA
1413824638,20/10/2014,17:03:58,0.075,µA
1413824038,20/10/2014,16:53:58,0.073,µA
1413823438,20/10/2014,16:43:58,0.103,µA
1413822838,20/10/2014,16:33:58,20.000,µA
Here is the problematic GNUPlot script I use
CSV MIN
#!/gnuplot
set terminal pdf enhanced font "sans,6"
#Filename
set output "BIOSENSE GRAPH TEMPLATE.pdf"
set size ratio 0.71
set pointsize 0.1
set datafile separator ","
#DATA FILES
plot 'ACT.csv' using 1:4 every::6 title 'Active' with points pt 5 lc rgb 'red' axes x1y1
And finally this is the error message I get from GNUplot
line 15: x range is invalid
Any help appreciated!
Thanks

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.

Setting xrange in gnuplot producing strange result

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'

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)

Large text substitutions in Tcl

Within my Tcl script I'm building source code in another language. Let it be gnuplot source for example. I have Tcl code like this:
# `script' variable contains gnuplot source code
set script {
set terminal pdf
set output "chart.pdf"
set title "[makeTitle]"
plot "$dataFile" using 1:2 title ""
}
# Then I write `script' to file for later execution
Notice that script variable contains command call (makeTitle) and variable substitution (dataFile). The source code itself contains new lines, double quotes.
Question: how can I simply "evaluate" this variable to substitute command calls by their results and variables by their values? Expected result should look like this:
set terminal pdf
set output "chart.pdf"
set title "R(S) Dependence"
plot "r_s.txt" using 1:2 title ""
You're looking for the subst command:
set result [subst $script]
One approach I commonly use in this type of situation is using [string map] with special symbols. For example:
set script {
set terminal pdf
set output "chart.pdf"
set title "%MAKETITLE%"
plot "%DATAFILE%" using 1:2 title ""
}
set script [string map [list %MAKETITLE% [makeTitle] %DATAFILE% $datafile] $script]
While glenn's answer of using [subst] is a good one and will work for the sample code you tested, it can run into issues as the original string gets more complex. Specifically, if it winds up containing characters that Tcl would interpret as commands to run or variables to substitute, you wind up needing to escape them, etc. By using string map and very specific character sequences to replace, you can limit the things that are changed to exactly what you need.