I am new to gnuplot and using Ubuntu 12.04. I want to create a graph on the fly when the information is coming in. So i have a data.dat file which looks like:
time server1 server2
0 0 0
1 2000 3000
2 3000 4000
3 4000 5000
After that i have a script file, loop.plt, that is used to reread the file:
pause 2
replot
reread
And finally, the command I use in in a bash file:
gnuplot -persist -e "plot 'data.dat'" loop.plt
The result comes back as dots - not a line that I expected. But this is only for server1.
How can I change this to create the graph with a line, and also to show the server2 in the same graph? Can someone help me with this?
Change your command to the following:
gnuplot -persist -e "plot 'data.dat' using 1:2 with lines ,'' using 1:3 with lines" loop.plt
This plots columns 1 and 2 and columns 1 and 3 using lines
Related
I am trying to plot some data with gnuplot. I want to use a static script.gp file and feed it from stdin with my data. I also have multiple datasets that I need to pass to the script.
script.gp:
set term jpeg
set encoding utf8
plot '<cat' index 0 with lines, plot '' index 1 with lines
data:
1 2
2 2
0 0
7 4
command:
cat data | gnuplot script.gp
This doesn't work, since I'm guessing it tries to reread from stdin. Is there a way I can do this, or do I have to use temporary files to store my data?
The solution I 've found so far has plenty of drawbacks, however it kind of does the job:
Use 'gnuplot -e' and cat the script file into the command:
cat data | gnuplot -e "$(cat script.gp)"
change script.gp using ; at the end of every line, remove all comments and change the plot command using '-' instead of '<cat' and removing index:
set term jpeg;
set encoding utf8;
plot '-' with lines, '-' with lines
change the data format, seperating each dataset with a line with e:
1 2
2 2
e
0 0
7 4
I would suggest to use the special file '<' which allows you to call the php script and you can have your gnuplot file plot.gp:
set term jpeg;
set encoding utf8;
set output file_out
my_cmd1=sprintf('< my_script %s', my_file1)
my_cmd2=sprintf('< my_script %s', my_file2)
plot my_cmd1 with lines, my_cmd2 with lines
and you call in a shell with:
gnuplot -e 'my_file1="input1.txt"; my_file2="input2.txt"; file_out="my_out.jpg"' plot.gp
In my bash script, I am doing a bunch of operations before starting my gnuplot script. My bash script looks like this:
#!/bin/bash -e
# Do some bash operations here
# Create a file containing a list of Gnuplot commands called arrow_coords
# All the commands are the same: just drawing a group of arrows
# Every line in "arrow_coords" looks like this:
# set arrow from -500,-100 to 500,-100 as 1
# There are hundreds of those line commands
# Entering Gnuplot script
gnuplot <<- EOF
reset
set terminal pngcairo transparent nocrop enhanced font '$font_type, 22' size 1800,1800
set output "$output_file"
set yrange [-1:18]
set xrange [-1:18]
? <----------------------------------- HOW TO INSERT COMMANDS FROM ANOTHER FILE?
plot '$dat_file' using 1:2 with points ls 1
EOF
I could not find a way to insert the commands written in arrow_coords into the Gnuplot script I have in my bash file. Is this possible? Are there other suggestions to what I am trying to do?
If your file contains only gnuplot instructions, you can run it with the load or call commands:
gnuplot <<- EOF
# your gnuplot configurations
load 'arrow_coords' # calling external file
plot '$dat_file' using 1:2 with points ls 1
EOF
Here's an example that illustrates the solution:
#!/bin/bash
# prepare file
echo "Test!" > test.txt
a=`cat test.txt`
cat <<- EOF
File contents: $a
Again: `cat test.txt`
EOF
So in your code, you could replace the line starting with ? with:
`cat the_file_you_generated`
I'm trying to get part of the answer in https://stackoverflow.com/a/27534509/257924 to work but am running into a Bad format character error.
Only when I comment out the set table command below, do I get the error (execute this as a script on Linux). Below, I'm expecting /tmp/temporaryfile.dat to be generated and not a plot image file:
#!/bin/bash
datafile=/tmp/mydata.1
cat > $datafile <<EOF
2013/5/1 1
2013/5/1 1
2013/5/1 1
2013/6/1 2
2013/6/1 2
EOF
gnuplot_in=/tmp/gnuplot.in
gnuplot_out=/tmp/gnuplot.png
cat > $gnuplot_in <<EOF
set xdata time
set timefmt '%Y/%m/%d'
set table '/tmp/temporaryfile.dat'
set format x '%Y/%m/%d'
plot "$datafile" using (timecolumn(1)):2 title "thetitle" with linespoints;
unset table
EOF
# Show the input with line numbers for debugging:
grep -n '.*' $gnuplot_in
gnuplot $gnuplot_in
#eog $gnuplot_out
The error is:
1: set xdata time
2: set timefmt '%Y/%m/%d'
3: set table '/tmp/temporaryfile.dat'
4: set format x '%Y/%m/%d'
5: plot "/tmp/mydata.1" using (timecolumn(1)):2 title "thetitle" with linespoints;
6: unset table
"/tmp/gnuplot.in", line 5: Bad format character
I've already checked the script for accidental UNICODE characters as indicated by gnuplot error: Bad format character
What would cause this error and how do I fix it?
I've concluded that this is due to a bug in the version of gnuplot on my RHEL 6.6 machine. On my RHEL 6.6 machine we have this version of gnuplot:
gnuplot 4.2 patchlevel 0
On my Debian system, the script works without errors, using this version of gnuplot:
gnuplot 4.6 patchlevel 0
I want to do something similar to this question: gnuplot : plotting data from multiple input files in a single graph.
I want to plot simultaneously all the files in a directory, without having to explicitly write their names. The column numbers are the same for all the files. What can I do?
Doing plot for [file in *] file u 3:2 doesn't work.
Also, I don't want each file to have a different legend. All points from all files should be treated the same, as if they all came from a single file.
As an alternative to Jonatan's answer, I would go with
FILES = system("ls -1 *.dat")
plot for [data in FILES] data u 1:2 w p pt 1 lt rgb 'black' notitle
or
plot '<(cat *.dat)' u 3:2 title 'your data'
The first option gives you more flexibility if you want to label each curve. For example, if you have several files with names data_1.dat, data_2.dat, etc., which will be labeled as 1, 2, etc., then:
FILES = system("ls -1 data_*.dat")
LABEL = system("ls -1 data_*.dat | sed -e 's/data_//' -e 's/.dat//'")
plot for [i=1:words(FILES)] word(FILES,i) u 3:2 title word(LABEL,i) noenhanced
You could try something like:
a=system('a=`tempfile`;cat *.dat > $a;echo "$a"')
plot a u 3:2
This uses the command line tempfile command to create a safe, unique, and disposable temporary file. It mashes all of the data files into this file. It then echoes the file's name so gnuplot can retrieve it. Gnuplot then plots things.
Worried about header lines? Try this:
a=system('a=`tempfile`;cat *.dat | grep "^\s*[0-9]" > $a;echo "$a"')
The regular expression ^\s*[0-9] will match all lines which begin with any amount of whitespace followed by a number.
I like to be able too choose the files to plot with wildcards, so if you like that you can do as follows, though there are many ways. Create the following script.
script.sh:
gnuplot -p << eof
set term wxt size 1200,900 title 'plots'
set logs
set xlabel 'energy'
plot for [ file in "$#" ] file w l
eof
do chmod u+x script.sh
Run like ./script.sh dir/* *.dat
If you need it often make an alias for it and put it in some reasonable place:)
Cheers /J
Try the following command:
gnuplot -e 'plot for [file in system("find . -depth 1 -type f -print")] file u 3:2'
Note: Add -p to keep the plot window.
I know I can plot with data from stdin by using '-' as the data source, but is there any way I can plot data output from a command the same way? I.e., instead of running the command and piping to the gnuplot script, can I specify the command in the gnuplot script itself?
Something like this (but this doesn't work):
set terminal pngcairo
set output 'test.png'
cmd = "`./mycmd`" # running ./mycmd in terminal gives my data to stdout.
# the command can be several other commands, piped together.
# i'm only interested in whatever comes to stdout after running
# the entire thing.
plot cmd u 2:3 w lines # etc...
The above makes cmd contain a single long line with all the lines of output smashed together.
Yes, you can:
plot '< ./mycmd'