gnuplot v4.4: Problem plotting using timeseries x axis - gnuplot

could someone please help me. I'm trying to create a simple chart.
set datafile separator ","
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set autoscale y
plot ["13:00":"14:00"] './data.csv' using 1:2 with lines
data.csv:
26/10/2010 13:30:01,1
26/10/2010 13:30:12,2
26/10/2010 13:30:23,3
26/10/2010 13:30:34,4
gives me the error:
line 6: all points y value undefined!
I've tried all manners of timefmt settings!
Many thanks

The problem is defining the xrange, it needs to be the same format timefmt (see ?time_axis)
The following works for me
set datafile separator ","
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set autoscale y
set xrange["26/10/2010 13:00:00":"26/10/2010 14:00:00"]
plot './data.csv' using 1:2 with lines
Oh, and I got rid of the blank lines in between each line of data.
If you don't want to edit the file to get rid of the blank data, then you can use awk in gnuplot like so,
plot "<awk '$0!~/^$/ {print $0}' ./data.csv" using 1:2 with lines
for the final command.
EDIT: further info in case there is still a problem (see the comments)
I needed to use the awk command to get the data to plot. This was to remove the blank lines in the data. Combining awk and gnuplot in this fashion works on linux systems, I'm not certain about gnuplot on windows. It could be that certain piping isn't happening, in which case you would have to remove the blank lines before using gnuplot. (you could still use awk for this, but perhaps not in the plot command?)
If you are using linux and the above is not working, then something else is the problem. Perhaps there are old commands stored in the gnuplot session? To make sure we are doing exactly the same thing, I give the shell script that I used (I changed the xrange to fit the data better and make a nicer plot, also notive the \$ instead of $, otherwise the shell misinterprets the $ sign).
Okay: I made the file data.csv.sh in a new folder (in case you have data.csv or data.csv.png files already),:
#!/bin/bash
echo "26/10/2010 13:30:01,1
26/10/2010 13:30:12,2
26/10/2010 13:30:23,3
26/10/2010 13:30:34,4" > "data.csv"
gnuplot<<EOF
set term png small
set output "data.csv.png"
set datafile separator ","
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set autoscale y
set xrange["26/10/2010 13:30:00":"26/10/2010 13:31:00"]
plot "<awk '\$0!~/^\$/ {print \$0}' ./data.csv" using 1:2 with lines
set output
set term pop
EOF
Then on the terminal I typed:
chmod +wrx data.csv.sh && ./data.csv.sh
to make the shell script executable and then to run it.
The file data.csv.png looks like the following
All the best
Tom

I just wanted to add to the above discussion in case someone was having the same issues. I was trying to plot a time series, such as:
07/22/13 01:00 120
When I tried to plot using the above procedure I got the bad time format error. I changed my input data to:
07/22/2013 01:00 120
After the change, it ran perfectly. This would make sense because to gnuplot the 07/22/13 is vague. That is, is it 1913, 1813, or 2013 (or any other yy13).

Related

Drag and drop script for gnuplot

I have some data I would like to plot in gnuplot. For being able to plot them I wrote a script called "plot_A.gp". In it I set the axis values, labels and the output terminal.
In order not to have it modificated for every data file, I was wondering if it is possible to make the script able to handle drag and drop-files.
As example script:
set xrange [0 to 100]
set xlabel "x-axis"
set ylabel "y-axis"
set terminal eps
set output %1.pdf
plot %1 using 1:($2*$2) with lines
How can I set %1 to the file name I just dropped onto the script?
Please execute the script with call instead of load at the gnuplot prompt. call accepts upto 10 arguments. Before gnuplot 5.0, these arguments are called $0, $1, ..., $9.
In gnuplot 4.6, your script would look like this:
datafile="$0"
outputfile="$0".".pdf"
set xrange [0 to 100]
set xlabel "x-axis"
set ylabel "y-axis"
plot "hello" using 1:($$2*$$2) with lines
set terminal eps
set output outputfile
replot
set terminal wxt
set output
Because $2 refers to the third parameter to the script, to access the second column use $$2 instead. Alternately, you can also use 1:(column(2)*column(2)).
You would call it like this.
gnuplot> call "plot_A.gp" "hello"
This will plot the data in the file "hello" and create a pdf called "hello.pdf". I have also reset the terminal back to wxt, as a best-practice.
From gnuplot 5.0, using $0, $1 etc is deprecated. Instead you should use the special variables ARG0, ARG2, ..., ARG9. I don't have access to gnuplot 5.0. But I think you just need to use ARG0 instead of $0. Please you could refer to this answer to How to pass command line argument to gnuplot?

set multiple delimiter with gnuplot

I'm doing some simulations in OpenFOAM, using probes to get a time series of the velocity in a point. The output file has the following delimiter setup.
if it is possible, what would be the command to set the delimiter ?? when using gnuplot
set datafile separator '???'
Bonus info if I remove all ( and ) and use the default command the plot "plots"
plot "U" using 1:2
You can use
plot "U" using 1:2 "%lf (%lf %lf %lf)"
This format specifies your data format in the plot command. See help using for more details on this and the using examples for more complex examples.
If you don't want to type this each time, and you have a copy of gnuplot compiled with support for string macros, you also can do
dformat = "\"%lf (%lf %lf %lf)\""
plot "U" using 1:2 #dformat
which will expand the format specifier into the command. See help macros for more on this.
I would suggest to use sed :
plot "< sed 's|[()]||g' U" u 1:2

How does gnuplot understand command line arguments from stdin?

I generated a graph using gnuplot with following command:
echo 'plot [0:14][0:1000] "source_data_file" with steps title "example graph"; pause 15' | gnuplot
I would like to change the name of the X- and Y-axes with xlabel and ylabel arguments, but if I execute:
echo 'plot [0:14][0:1000] "source_data_file" with steps title "example graph" xlabel 'X-axe label' ylabel 'Y-axe label'; pause 15' | gnuplot
..I receive an error message pointing to xlabel. I have tried to separate arguments with semicolons, but this had no affect. In addition, I would like to change the format of the Y-axe from exponent(for example 1.8232e+06) to integers(for example 1823200), but I don't know how to pass the format y/format x argument to gnuplot.
How does gnuplot understand command line arguments if those are passed to gnuplot from stdin?
#arbautjc is correct--you have a problem with using single quotes for the x and y axis labels. Also, those need to be specified before the plot command is run. So, a better way would be
echo 'set xlabel "X-axe label"; set ylabel "Y-axe label"; plot [0:14][0:1000] "source_data_file" with steps title "example graph"' | gnuplot -p
Also, is the pause command necessary? (It may have some reason to be there, but I cannot see it from your example.)

How to add data from gnuplot to an existing file without overwiting the contents of said file

I have some gnuplot code as follows:
gnuplot> h=1
gnuplot> a=2
gnuplot> set print "additional numbers.txt"
gnuplot> print h,a
gnuplot> set print
Now I want to add more data to this file without overwritng the previous entry. I can do it in C, but I want to keep everything in 1 script that I can run through gnuplot
I hope you can help.
Use the append flag:
set print "additional_numbers.txt" append

plot audio data in gnuplot

how could I convert an audio file such as a aiff into a svg using gnuplot? I used sox (sound exchange) to convert an .aiff into a .dat, which I can load now in gnuplot.
I did something similar to:
set terminal svg
set output "test.svg"
plot "test.dat"
I get a svg file, but only with dots / or a lot of x.
How could I connect the dots?
To draw lines between the points, use
plot "test.dat" with lines
Or to keep the point markers as well as the lines, use
plot "test.dat" with linespoints
So your example becomes
set terminal svg
set output "test.svg"
plot "test.dat" with lines
Further tips:
Don't consider every sample:
With large files you may also find it useful to plot only every nth sample with "every n". This will make the plot much faster to generate and will also yield a smaller (but less detailed) svg file.
e.g.
plot "test.dat" every 100 with lines
Ignore .dat file header:
If your sox-produced .dat file has some lines of introductory metadata, such as
; Sample Rate 44100
; Channels 2
you can add the following to have gnuplot consider those lines comments and ignore them.
set datafile commentschars ";"
This will save you having to pre-process your .dat file in order to remove those lines before gnuplot chokes on them.
Plot both left and right channels of stereo audio:
If you're working with a stereo file, you probably want to see both channels.
We can use "multiplot" to lay out the following two plots (of left then right channel) one above the other on a shared x-axis, as many sound-editing programs do.
set multiplot layout 2,1
plot "test.dat" using 1:2 with lines
plot "" using 1:3 with lines
The 1:2 and 1:3 instruct gnuplot which columns of the dat file to use as x and y sources. I'm assuming your stereo .dat file produced by sox looks as mine does, with columns for
- 1: time since beginning of first sample
- 2: normalized sample value of left channel
- 3: normalized sample value of right channel
example snippet:
10.840113 0.20101929 0.17840576
10.840136 0.26062012 0.14831543
10.840159 0.23779297 0.13146973
Putting it together:
Here's a script which puts all of the above together. If you don't have a stereo data file to try this with, you'll want to remove the plot of 1:3 and the multiplot setting.
#!/usr/bin/env gnuplot
set datafile commentschars ";"
set terminal svg
set output "test.svg"
set multiplot layout 2,1
plot "test.dat" using 1:2 every 100 with lines
plot "" using 1:3 every 100 with lines
unset multiplot
Prettification
Finally, I've tweaked the script for presentation (borrowing heavily from the excellent "gnuplot in action" book by Philipp K. Janert):
#!/usr/bin/env gnuplot
set datafile commentschars ";"
set terminal svg
set output "test.svg"
set multiplot layout 2,1
set ylabel "sample value"
set bmargin 0
set format x ""
set ytics -0.8,0.2
set key bottom
plot "test.dat" using 1:2 every 100 with lines lc rgbcolor "#a0a0b0" title "left channel"
set xlabel "time (s)"
set bmargin
set tmargin 0
set format x "%g"
set ytics -1.0,0.2,0.8
set key top
plot "" using 1:3 every 100 with lines lc rgbcolor "#a0a0b0" title "right channel"
unset multiplot
Here's an example output (albeit png):
How to make a .dat file
For anyone following along at home, you can use sox to generate a .dat file from an audio file with the following command:
sox input.wav output.dat
Big file warning: Converting even just 10 seconds of stereo audio at 40kHz will produce a 25Mb output file.
Note that you can also plot the binary data directly:
set terminal svg
set output "test.svg"
plot '< sox test.aiff -t s32 -' binary format='%int32' using 0:1 with lines
Just wanted to document this - well, I was looking for a long time for a Linux command line audio waveform viewer, which could be called from the command line, with a raw binary file as input, and where the format of the data could be specified on the command line.
Audacity can import raw data, but only from the GUI (there is no way to specify raw datafile format through its command line options); while wave viewers like gwave, gtkwave or Gaw - Gtk Analog Wave viewer can either read proper .wav, or SPICE based formats.
And thanks to the answer by #Thor, now I know I can use gnuplot for the purpose. Here is an example command line, which interprets the raw binary data as 16-bit stereo:
gnuplot -p -e "set terminal x11 ; set multiplot layout 2,1 ; plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:1 with lines ls 1; plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:2 with lines ls 1 ; unset multiplot"
... or broken in several lines:
gnuplot -p -e "set terminal x11 ; set multiplot layout 2,1 ; \
plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:1 with lines ls 1; \
plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:2 with lines ls 1; \
unset multiplot"
Note that:
you should only use pipe "< ..." if you want to output from a shell command - if you have a file (like above), don't use the pipe (else getting permission denied)
Note the format '%int16%int16' will cause the byte stream to be "grouped" as 2 bytes representing column (channel) 1, the next 2 bytes as column (channel) 2, the next 2 bytes again as column 1, and so on... see gnuplot docs_4.2: Binary General - Format (also related: Gnuplot: How to plot multiple time series from a binary format)
Finally with two independent plots, one using 0:1 and the other using 0:2, we can get a typical waveform rendering (as in accepted answer) - with one channel above the other
Since the --persist option is used above, gnuplot will exit, while the (x11 or wxt) window will remain - and so, the typical gnuplot interaction with the window will not work
Anyways, glad I found this, will save me quite a bit of time, I think :)

Resources