Gnuplot - Using replot with png terminal - gnuplot

I am trying to use replot with png terminal in Gnuplot.
If I do the following I get two plots on one graph without any problem:
plot sin(x)/x
replot sin(x)
Now if do the same for a png terminal type the resulting png file only contains the first plot.
set terminal png
set output 'file.png'
plot sin(x)/x
replot sin(x)
Am I missing something at the end to get the second plot in my png file?

This is actually a very good question, and the behavior here is terminal dependent. Some terminals (e.g. postscript) will give you a new page for each replot. You have a couple of solutions...
First Option: You can make your plot prior to setting the terminal/output and then replot again after you set the terminal/output:
plot sin(x)/x
replot sin(x)
set terminal png
set output 'file.png
replot
This option is sometimes convenient if you want to plot the same thing in multiple terminals, but I rarely use it for anything else.
Second (better) Option: You can pack multiple plots into one command separating each with a comma.
set terminal png
set output 'file.png'
plot sin(x)/x, sin(x)
I very much prefer the second way -- when in a multiplot environment, this is the only way to put multiple graphs on the same plot. If you have very long functions to plot, you can break the line with gnuplot's line continuation (\ at the end of the line -- Nothing is allowed after the \, not even whitespace)
plot sin(x)/x with lines linecolor rgb "blue" linetype 7 lineweight 4, \
sin(x), \
cos(x)

Related

Spurious '-' text with epslatex and multiplot

The following gnuplot snippet generates a multiplot showing six plots of data ported via stdin, but the special filename '-' used is also printed on the output:
set term epslatex color
set output 'mwe.tex'
set multiplot layout 3,2 scale 1,1 columnsfirst
set xrange [-3.1415:3.1415]
set yrange[-1.0:1.0]
set cbrange [-1:1]
set size ratio -1.0
set palette rgb 33,13,10
unset colorbox
plot '-' with image
-3.1416 -1.00 0.00
-3.1089 -1.00 0.00
(...)
e
(...)
unset multiplot
(The 'plot' command and what follows until and including 'e' is repeated six times with different input before the unset multiplot command.)
The output is shown here. The special filename '-' must be included in the plot command to plot inline data, but it should not be shown in the resulting plot. How to avoid this behavior?
The problem persists when using the 'standalone' term option with epslatex, but it does not show up when using other terminals.
I use gnuplot 4.6 patch 2.
According to the StackOverflow rule "no answer in the comments", here again as answer. Also check help key and the options there.
Try:
set key noautotitle
or
plot '-' with image notitle

Gnuplot: Change Density of dotted line in splot

I am trying to plot a dotted line within an splot with the following code in Gnuplot 4.6 patchlevel 4:
set terminal "pdfcairo" enhanced dashed size 15,10
set pm3d map
set output "test.pdf"
splot 'map.dat' using 1:($2/1000):3 notitle, \
'line1.dat' using 1:($2/1000):1 notitle with lines ls 2, \
'line2.dat' using 1:($2/1000):1 notitle with lines ls 2
unset output
The heat map works and so does line1.dat. However, the second line appears mostly solid. The difference is that line1.dat has 70 entries and line2.dat has 900. The second line has a jump between two points and there it is dotted.
Does somebody know how I can change the dot density so that the whole line appears dotted. Changing the original data file is not an option.
Thank you for your help,
noes
EDIT:
One workaround I found is
splot 'line2.dat' every ...
but that can get unconvenient at the jump in the data.
The command (s)plot 'line.dat' with lines first plots the datapoints and then connects the datapoints using lines with the respective linestyle. If the datapoints are too close to each other, there is no place for some gaps when a dashed linestyle is used.
To display a dotted/dashed line, you can try to replace the points by a function or to reduce the number of points.
Try dotted lines instead of dashed lines. Linestyle and linecolor can be set independently: splot 'line.dat' with lines ls 0 lc 2. 900 points might be too many for this approach.
Fitting a function would work, but probably it is too difficult to find a suitable function.
The every option reduces the number of points.
Another possibility to reduce the number of points would be to interpolate the points using the smooth option. This requires a temporary file and works as follows:
# [prepare plot]
set samples 100
set table "line2.dat.tmp"
plot 'line2.dat' using 1:($2/1000) smooth mcsplines with lines ls 2
unset table
set terminal "pdfcairo" enhanced dashed size 15,10
set pm3d map
set output "test.pdf"
# [plot]
splot 'map.dat' using 1:($2/1000):3 notitle, \
'line1.dat' using 1:($2/1000):1 notitle with lines ls 2, \
'line2.dat.tmp' using 1:2:1 notitle with lines ls 2
unset output
In the [prepare plot] section a temporary file "line2.dat.tmp" is created which contains datapoints interpolating line2.dat. You have to play with set samples to get the right number of points. In the example we have 100 equidistant points instead of 900 points with different distances.
The option smooth mcsplines preserves the monotonicity and convexity of the original data points, see help smooth mcsplines in a gnuplot shell.
In the [plot] section the original "lines2.dat" is replaced by the interpolated data.
This approach works if the original data is smooth enough so that replacing 900 points by 100 points does not skip important information. Maybe you want to plot both "lines2.dat" and "lines2.dat.tmp" in a single diagram to compare them.
User the every key-word, like this:
'line2.dat' every 20 using 1:($2/1000):1 notitle with lines ls 2

Gnuplot script to output eps or svg - how to write?

I have the following gnuplot script:
set autoscale
unset log
unset label
set xtic auto
set ytic auto
unset title
set xlabel "Number of clusters"
set ylabel "Accuracy of classifier (%)"
plot "cluster.dat" using 1:3 title "PART" with lines, \
"cluster.dat" using 1:4 title "JRip" with lines, \
"cluster.dat" using 1:5 title "FURIA" with lines
I would like this script, when run, to output SVG or EPS - what would I need to add or modify to make this happen?
In gnuplot the output type is called terminal.
In your script, before the plot command use
set term svg
set output "output.svg"
or
set term eps
set output "output.eps"
both terminals have several options. Type help eps (on some gnuplot this is help epscairo) or help svg.
To get the list of available terminals for your build, type set terminal.

Remove redundant file names in gnuplot figure

I wrote a simple gnuplot shell script
#!/usr/bin/gnuplot
reset
set terminal png
set xlabel "Time"
set ylabel "Prime and Probe #"
plot "file.dat" with points
Notice that there is an annoying "file.dat" on the upper right corner. How can I remove that?
gnuplot Version 4.6 patchlevel 4
Use the title parameter to change the key
plot "file.dat" title "my title"
or remove it for a single plot
plot "file.dat" notitle
or remove it for all plots with
unset key

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