My program has gnuplot script and its first line is
set data s l
But this makes error
"cdia.GNUBAND", line 1: unrecognized option - see 'help set'.
I think it's the version problem and I have to change the "set data s l" command in right way. I searched on web but couldn't find it.. How do I solve this?
That is the shortcut for set data style lines, which is deprecated since version 4.0 (released in 2004). To replace this line, use
set style data lines
This sets the default plotting style for data set to lines, i.e. the commands
set style data lines
plot 'data.dat'
and
plot 'data.dat' with lines
are equivalent.
Related
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
I want to generate a gnuplot plot command programmatically, like:
plotline = sprintf("'datafile1.dat' using %d:3 with points, '%s' using %d:3 with points",i,targfile,i)
plot plotline
Where 'plotline' in the second line is expanded to produce and execute a full command like:
plot 'datafile1.dat' using 8:3 with points, 'datafile2.dat' using 8:3 with points
I want to do this in order to echo 'plotline' in the terminal and so be certain exactly what is being shown while cycling through a set of columns / datafiles / whatever inside a loop in a gnuplot script.
Is there / what is the syntax to do this, or can you suggest another approach to report the plot command as executed (without splitting into a plot command and a separate set of commands to report the current variable states).
Thanks!
In order to construct such a plot command from some strings, you can use eval to execute the commands contained in a string:
plotline = 'x title "mytitle"'
eval('plot '.plotline)
Alternatively you can use set macros:
set macros
plotline = 'x title "mytitle"'
plot #plotline
This replaces #plotline with the content of the string variable plotline before executing the command. Using plot plotline interpretes the content of plotline as file name. Note, that as of version 4.6 macros don't work properly in loops, but eval works fine.
BTW: If you don't specify your own title, then the actual plot statement is written in the plot legend. But that can't be written to the terminal output.
Just thought I'd document this (self-answer to follow):
When working with gnuplot in terminal, one can use up and down arrows on keyboard to iterate through the typed commands history - just like in gdb.
However, sometimes there may be a sequence of commands I repeat often - which I'd like to call by issuing a single command. For instance, one works with interactive x11 terminal in gnuplot, and wants to obtain a "screenshot" in png format. That requires the terminal to be changed to png, output to be set, plot to be issued, and terminal reverted back to x11; or:
set terminal png
set output 'gnuplot.png'
replot
set terminal x11
I'd like this sequence to be called with a single command - even though I'm aware that these could fit on a single line, by using semicolon as separator:
set terminal png ; set output 'gnuplot.png' ; replot ; set terminal x11
In gdb, there is a command, define, which allows for user-defined commands; one simply issues in the gdb terminal:
(gdb) define fn
> finish
> next
> end
... and from that point on, one can type fn in that terminal to call the sequence of finish and end.
Is there something similar to that in gnuplot?
Yes, there seems to be - there is a facility called macros (help macros in gnuplot), where a string variable can be expanded by prepending the # ("at" character) to its name.
This facility is disabled by default, so one needs to think about enabling it. Which is why its best to save that sequence in an init script file, named for instance init.gp:
print ""
print "init.gp starting"
set terminal x11
# define capt string variable as sequence
# of commands, semicolon separated
capt = "print 'Saving...' ; set terminal png ; set output 'gnuplot.png' ; replot ; set terminal x11"
print "macros state: "
show macros
print "enabling macros state:"
set macros
show macros
print "The #capt command should be available now."
print ""
print "init.gp ending"
print ""
Then one can do a terminal session like this in gnuplot:
$ gnuplot
G N U P L O T
[...]
Terminal type set to 'wxt'
gnuplot> load "init.gp"
init.gp starting
macros state:
command line macros will not be expanded
enabling macros state:
command line macros will be expanded
The #capt command should be available now.
init.gp ending
gnuplot> plot sin(x)
gnuplot> #capt
Saving...
Terminal type set to 'png'
Options are 'nocrop font /usr/share/fonts/truetype/ttf-liberation/LiberationSans-Regular.ttf 12 size 640,480 '
Terminal type set to 'x11'
Options are ' nopersist'
gnuplot> plot cos(x)
Closing gnuplot.png
gnuplot> exit
$ identify gnuplot.png
gnuplot.png PNG 640x480 640x480+0+0 8-bit PseudoClass 102c 5.58KB 0.000u 0:00.000
Well, hope this helps someone,
Cheers!
Short question:
How do I display the _ (underscore) character in a title in gnuplot that is assigned from a variable name in gnuplot?
Details:
I have something like the following code:
items = "foo_abc foo_bcd bar_def"
do for [item in items] {
set title item
set output item.eps
plot item."-input.txt" using 1:2 title item with linespoints
}
This works fine with gnuplot except that the title get changed from foo_abc to fooabc. I don't know if I want to use an escape character because I don't want that to be in the file name. I've tried a couple of different options with single vs. double quotes but I haven't found what I need yet.
Instead of foo_abc, write foo\\\_abc.
Most gnuplot commands which generate labels accept a noenhanced keyword which will prevent gnuplot from using enhanced text for just that string. In this case, it should be sufficient to just do:
set title item noenhanced
An alternative is to create a function which will remove the unwanted text from the string when passing it to set output:
remove(x,s)=(i0=strstrt(s,x),i0 ? remove(x,s[:i0-1].s[i0+strlen(x):]):s)
# Makes me wish gnuplot syntax was more pythonic :-p
#TODO: Write a `replace` function :-). These just might go into my ".gnuplot" file...
I use an inline function to find the index of the first occurrence of x in the string s. I then remove that occurrence via string concatenation and slicing and recursively call the function again to remove the next occurence. If the index isn't found (strstrt returns 0) then we just return the string that was put in. Now you can do:
set output remove('\',item)
set title item
The underscore comes from treating titles as "enhanced text". Turn that off using
set key noenhanced
If you are using the enhanced eps terminal, that is the reason you need to escape the underscore in the first place. There was another related question today which explains the issue a bit. When you set the terminal, try:
set terminal postscript noenhanced <whatever else here...>
That works for me (Arch linux, gnuplot 4.7.0). If the enhanced terminal is essential, below is a partial solution I found. The assumption is that the underscore always appears in the same place in the string.
set terminal postscript enhanced
items = 'foo\_abc foo\_bcd bar\_def'
do for [item in items] {
set output item[1:3].item[5:*].'.eps'
set title item
plot sin(x)
}
This way you can escape the underscore and not have the \ appear in the filename. Note the use of single quotes for the 'items' string; see the previously linked question for details.
I had the same problem about the underscore in the title: such as I needed to write 4_3 subframe and I needed the enhanced postscript. The SIMPLEST way turned out to be from the adjacent post: ``If you are using the enhanced eps terminal, that is the reason you need to escape the underscore in the first place. There was another related question today which explains the issue a bit." - How is # produced in gnuplot?
So, I followed their advice and this worked:
plot 'LC.stats' u 3:4 ti "{/=15 1350 stars in C18 4\_3 subframe}" -
Double escape character before the underscore.
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).