I'm trying something very simple ...
#!/usr/bin/gnuplot
reset
filename = "something_or_other"
set terminal pngcairo dashed size 800,400 enhanced font 'Verdana,10'
set output filename.".png"
set title filename."\n"
set xlabel "probably time"
set ylabel "probably something else" offset graph 0.2,0.6 rotate by 0
plot filename.".dat" using 1:2 lc rgb "#00E80000" title "measurements"
!display filename.".png"
except filename stays just like that and doesn't get interpreted as a string.
How do I get this working?
The exlamation mark ! at the beginning of the line makes gnuplot send this whole line as it is to a shell. So, here you cannot use any gnuplot variables and string concatenation. For this you can use system():
system(sprintf("display %s.png", filename))
Related
I don't know why but I have an error. When I write all this things in terminal everything is ok, but when I use script I have this error
gnuplot> load wykres_sin.gp
internal error : STRING operator applied to undefined or non-STRING variable
This is my script
# Skrypt gnuplot
plot sin(x) w l
set title "Wykres sinusa od wartosci kata"
set xlabel "Wartosc kata (stopnie)"
set ylabel "Wartość sinusa"
set xrange [0:750]
set yrange [-1.2:1.2]
set term png large size 800,600
set output "sin.png"
replot
exit
You need quotes around the file name:
gnuplot> load "wykres_sin.gp"
I'm creating an interactive script that allows me to plot specific parts of a data file using Gnuplot. These files contain multiple channels of instrumentation data and I'd like to plot specified channels together on the plot. At the moment I can only plot each channel independently with a script.
for ycolumn in $ycolumns; do
gnuplot -p << EOF
set datafile separator ","
set autoscale
set grid
set xlabel "Time"
set ylabel "Data"
set title "$graphTitle"
plot "$FILE" using $xcolumn:$ycolumn lt rgb "red" with lines t " "
EOF
This is the code that plots each channel independently. To plot them together I need the the plot line to look like this
plot "$FILE" using $xcolumn:$ycolumn1 lt with lines t " ","$FILE" using $xcolumn:$ycolumn2 lt with lines t " ", etc...
My question is how, if possible, could I have a for loop outside gnuplot that appends each column command ("$FILE" using $xcolumn:$ycolumn1 lt with lines t " ") to a variable, and then that variable gets passed to the plot command inside Gnuplot?
I was thinking about using expect but I can't figure out a way to implement that in the way I'm trying to automate this script.
Probably the most straightforward approach is to first assemble the entire plot command (as mentioned in the comments by 'that other guy') and then use it in the here document:
plotCmd=""
plotCmdDelim="plot"
for ycolumn in $ycolumns
do
plotCmd="${plotCmd}${plotCmdDelim} '${FILE}' using $xcolumn:$ycolumn lt rgb 'red' with lines t ' '"
plotCmdDelim=","
done
gnuplot -p << EOF
set datafile separator ","
set autoscale
set grid
set xlabel "Time"
set ylabel "Data"
set title "$graphTitle"
${plotCmd}
EOF
I worte a script for gnuplot in my university and took the data to my computer at home.
Here is the relevant code:
set terminal postscript eps enhanced color font 'Helvetica,20';
set encoding utf8;
do for[i=2:12] for[j in "3 4 6"]{
set autoscale x;
set xr[-3:3];
set autoscale y;
set key right bottom;
set terminal postscript eps color ;
set termoption dash;
set xlabel "bla";
set ylabel "bla";
set output 'home/Plots/test-'.i.'-'.j.'.eps';
plot 'home/Daten/test-'.i.'-'.j.'.dat' with lines lw 4 title gprintf("homogen q=%.3f",(1/(j-1)-1/(10*(j-1)))/2); }
The gprintf() doesnt work..it only gives me 0.00 but at the computer in my uni it does it right...help anyone :(
edit:
I found out that gnuplot rounds...when i type in
gprintf("homogen q=%.3f",j)
it gives me q=3 for j=3.00
If i use:
gprintf("homogen q=%.3f",j/5)
it gives me q=0.00
it has to be a problem with gnuplot..because, as i said, on the other computer it worked :/
Answer
Use
... gprintf("homogen q=%.3f",(1.0/(j-1.0)-1.0/(10.0*(j-1.0)))/2.0)
The loop variables are integers by default and gnuplot is quite literal in its use of integer arithmetic, as you have already discovered...
Unrequested Advice
As a matter of style, I'd suggest to simplify the individual line's title to q = ... and use a descriptive plot title, e.g.,
Response curves for different values of the specific energy "q"
Based on this post (I am using gnuplot gnuplot 4.6 patchlevel 1):
gnuplot: max and min values in a range
I am trying to use set yr [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX] in my .pg script that plots data from this .dat file:
100 2
200 4
300 9
But I get:
undefined variable: GPVAL_DATA_Y_MIN
This is my script:
set terminal png
set output 'img.png'
set xlabel 'x-label'
set ylabel 'y-label'
set boxwidth 0.5
set style fill solid
set yrange [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
plot 'sample.dat' with boxes title ""
Any ideas?
The gnuplot-defined variables are available only after a plot command (In the question you linked to, a replot is used to plot again).
Basically you have different options:
First plot to terminal unknown, and then change to the real terminal, set the range (now the variables are available), and replot:
set xlabel 'x-label'
set ylabel 'y-label'
set boxwidth 0.5 relative
set style fill solid
set terminal unknown
plot 'sample.dat' with boxes title ""
set terminal pngcairo
set output 'img.png'
set yrange [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
replot
Note, that I used the pngcairo terminal, which gives much better results, than the png terminal. And I used set boxwidth 0.5 relative.
Use set autoscale to fix the ranges instead of setting an explicit yrange. You can use set offset to specify a margin based on the autoscaled values:
set autoscale yfix
# set offset 0,0,0.5,0.5
plot 'sample.dat' with boxes title ''
Use the stats command to extract the minimum and maximum values:
stats 'sample.dat' using 1:2
set yrange[STATS_min_y:STATS_max_y]
plot 'sample.dat' with boxes title ''
I have a file (called print_1012720.txt) that looks like the text shown below.
1133254688 5698771509078629376
1150031904 5698771509371165696
1150031904 5698771510035551232
4170258464 5698771510036082688
2895583264 5698771510036715520
1620908064 5698771510037202176
346232864 5698771510037665280 <----
3366459424 5698771510038193664
2091784224 5698771510332259072
817109024 5698771510332816128 <-----
3837335584 5698771510333344512
2562660384 5698771510339882240
1287985184 5698771510340411392
13309984 5698771510340939776 <-------
3033536544 5698771510348048896
1758861344 5698771510348577280
484186144 5698771510349228800
3504412704 5698771510632804864
2229737504 5698771510633441792
955062304 5698771510634390272
3975288864 5698771510638858496
2700613664 5698771510639347712
1425938464 5698771510642663168
134486304 5698771510643387136
3154712864 5698771510643808768
I am running the following commands in bash.
gnuplot
reset
set terminal png
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set xlabel "time"
set ylabel "highest seq number"
set yrange [0:65535]
set title "seq number over time"
set key reverse Left outside
set grid
set style data linespoints
plot "print_1012720" using 1:2 title "Flow 1"
It gives garbage in my shell. I suspect that it is the spacing between the columns that is causing this. However if I correct the spacing then the file is correctly plotted. Any clue how this can be rectified?
I suspect your main problem is that you are not setting an output. After your set terminal command you should set an output file, otherwise gnuplot will spit out the .png to your terminal, which looks like gibberish. Try putting the following in a file (saved as 'plot.plt' for instance) and then run gnuplot plot.plt at the bash command line.
set terminal png
set output 'output.png'
set xdata time
set timefmt "%s"
set format x "%H:%M"
set xlabel "time"
set ylabel "highest seq number"
set title "seq number over time"
set key reverse Left outside
set grid
set style data linespoints
plot "print_1012720.txt" using 1:2 title "Flow 1"
Creating a plot file this way will save you a lot of time typing at the gnuplot command line.
A couple of other comments:
1) You set your yrange to be 0:65535 when your y values from the datafile are much larger.
2) It looks like you are trying to extract a time in UNIX format from the first column in your datafile. For this you would want the line set timefmt '%s' as I have above. However, the time data in the data file appear to be all over the place, as do the y data.
3) You do not need the reset command at the beginning, since running gnuplot this way will create a new instance of gnuplot, so there will be nothing to reset.
4) You also had 'print_1012720' without the '.txt' extension; I'm not sure this was an error on your part.