For-loops in gnuplot - for-loop

I'm getting confused with how to use for-loops in gnuplot.
The following code works fine:
plot for [quadIter=0:270:90] \
path using 1:(column(1 + quadIter))
It plots 4 curves on one graph.
I also want to plot 4 horizontal lines on the same graph. I have written the following script to do this:
plot for [quadIter=0:270:90] \
path using 1:(column(1 + quadIter)) , \
path_to_expt[1 + quadIter/3: 19] \
But it only plots one additional line on the graph, so it is not being included in the for-loop. Please can you tell me how to get the additional line included in the loop?
Also, the constant value that is plotted is not the value I was expecting it would plot. Please can you tell me how to print the value of quadIter to the screen, so that I can check it against the value in the file?
Finally, I tried just to plot the 4 horizontal lines with this script:
plot for [quadIter=0:270:90] \
path_to_expt[1 + quadIter/3: 19] \
But I got an error message, "Non-numeric string found where a numeric expression was expected". I find this strange, as I didn't get this error message when I ran the second script but, as the second script isn't working how I would like, hopefully by getting the second and third scripts to work, I will have a better understanding of how for-loops work in gnuplot.
Thank you for your help!

The for iteration applies only the the current plot expression. The line
plot for [i=1:4] i*x, i*x**2
creates five plots, whereas in order to get eight plots you must do
plot for [i=1:4] i*x, for [i=1:4] i*x**2
Concerning your last expression path_to_expt[1 + quadIter/3: 19]: array expressions aren't supported by gnuplot (however path_to_expt looks like one).

Related

gnuplot - plot difference between two columns with pseudocolumn 0

I have a .csv file organized as follows:
425;490
160;343
390;487
35;231
...
I want to plot the difference of (row1-row2) on the y-axis, and the pseudocolum 0 as the x-axis.
So far, i've tried
plot "test.txt" using $0:($1-$2)
which throws
column() called from invalid context
On the other hand
plot "test.txt" (using $0:($1-$2))
seems to work but throws
'plot "test.txt" (using $0:($1-$2))'
in the console.
So my question:
What would be the correct syntax?
plot 'text.txt' using 0:($1-$2)
or
plot 'text.txt' using ($1-$2)

Gnuplot fillstyle pattern with word function gives error

I am plotting a file with different columns and I am using the for loop of gnuplot. I want to completely control the look of my graph so I use set line style to control the color of my lines. I want to do the same with the pattern fills but it seems that it is not possible with gnuplot.
So far I have come to that solution, which does not work as well, for unknown reasons:
set terminal cairolatex standalone pdf
set style line 2 linecolor rgbcolor "#F9E0B0" linewidth 2 pt 13
# etc ... up to 10
pat="0 2 4 7 2 4 7 2 4 7" ## An attempt to define the pattern style I want
set style fill pattern 1 border ## this control the first pattern, then the next ones are incremented but it cannot control each index
set output "myplot.tex"
plot for [i=2:10] "myfile.dat" index 0 u i:xtic(1) fillstyle pattern int(word(pat, i)) ls i ti columnheader ## The color is controlled according to me via the linestyle, but the fillstyle does not work
## An alternate solution giving a little bit of control but not fully satisfactory since I want to avoid the pattern 3
# plot for [i=2:10] "myfile.dat" index 0 u i:xtic(1) fillstyle pattern i%3+1 ls i ti columnheader
unset output
With this solution I get the following error: unexpected or unrecognized token
Any idea why word fails after fillstyle pattern, or does anyone have an idea on how to specify for each data the pattern?
EDIT: I am using gnuplot 5.0
The parser is expecting the entity immediately following "fillstyle" to be a number, but it doesn't recognize int("foo") as a number. That's a bug. You can work around this by using instead the syntax
fillstyle pattern 0+word(pat,i)

Plotting multiple columns with a for loop in gnuplot, key doesn't work

I have file with many columns that I'd like to plot as follows:
plot for [i=1:30] 'test' using 1:i w lp
This gives the plot I want, but when I do set key, then the key I see has all lines labeled as 1:i:
How can I make this output more meaningful, by actually displayin the value of i?
If you don't set an explicit title, gnuplot selects an automatic title based on the plain plot command call. If you want a meaningful title, you must give it explicitly, like
plot for [i=1:30] 'test' using 1:i w lp title sprintf("column %d", i)

How do I make a plot in gnuplot with the lowest value automatically subtracted from the y data?

I am plotting the creation times of a large batch of files in gnuplot to see if they are created linearly in time (they are not).
Here is my code:
#!/bin/bash
stat -c %Y img2/*png > timedata
echo "set terminal postscript enhanced colour
set output 'file_creation_time.eps'
plot 'timedata'" | gnuplot
The problem I have is that the y data are the creation time in seconds since unix start time, so the plot just has 1.333...e+09 on the y-axis. I would like to have the creation time of the first file scaled to zero so that the relative creation times are readable.
I encounter this problem in a number of data-plotting contexts, so I would like to be able to do this within gnuplot rather than resorting to awk or some utility to preprocess the data.
I know the first time will be the smallest since the files are named serially, so is there a way to access the first element in a file, something like
`plot 'data' using ($1-$1[firstelement])`
?
I think you can do something like that...(the following is untested, but I think it should work...). Basically, you have to plot the file twice -- the first time through gnuplot picks up statistics about the dataset. The second time through, you use what you found on the first run-through to plot what you actually want.
set terminal unknown
plot 'datafile' using 1:2
set terminal post enh eps color
set output 'myfile.eps'
YMIN=GPVAL_Y_MIN
plot '' u 1:($2-YMIN)
If you have gnuplot 4.6, you can do the same thing with the stats command.
http://www.gnuplot.info/demo/stats.html
EDIT It appears you want the first point to provide the offset (sorry, misread the question)...
If you want the first point to provide the offset, you may be able to do something like (again, untested -- requires gnuplot >= 4.3):
first=0;
offset=0;
func(x)=(offset=(first==0)?x:offset,first=1,x-offset)
plot 'datafile' using (func($1))
Gnuplot accepts unix commands, so you can say something like
gnuplot> plot "< tail -3 test.dat" using 1:2 with lines
in order to plot just the last three lines. You can use something like this for your purpose. Moreover, if you want to plot let's say from line 1000 to 2000
plot "<(sed -n '1000,2000p' filename.txt)" using 1:2 with lines
You can check this website, for more examples.
I found a related stackoverflow question here and exploited the awk script from one of the answers:
#!/bin/bash
stat -c %Y img2/*png > timedata
echo "set terminal postscript enhanced colour
set output 'file_creation_time.eps'
unset key
set xlabel 'file number'
set ylabel 'file creation time (after first)'
plot \"<awk '{if(NR==1) {shift = \$1} print (\$1 - shift)}' timedata\"" | gnuplot
The output looks like this (these are not the data I was talking about in my question, but similar):
So, gnuplot can do what I want but it does depend on the UNIX environment...
I also tried mgilson's method:
plot 'timedata'
YMIN=GPVAL_Y_MIN
plot '' u ($1-YMIN)
but gnuplot (my version is 4.4.2) did not find the minimum correctly. It came close; it looks like it plotted such that the minimum of the y range is 0:

Matlab big matrix

I'm trying to use matlab for the first time but I'm having a problem because the matrix I'm using is too big, I think.. I command I'm trying is:
m=[1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;169201]7;531456;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017;1692017]
And I'm getting the following error: Error: Unexpected MATLAB expression.
Does anyone have a solution for this?
Thanks in advance !
That matrix is tiny :-) Matlab can handle millions of elements per matrix.
However, on the second line, you have an extra bracket that ruins things:
...17;1692017;169201 ] 7;5...
Get rid of it, and you'll be fine!
Seems like you are trying to do a matrix, but typing its code wrongly. First, the ; separates lines; if you want to separaate columns in a row you have to use a space.
And you have two closing ], while you have only one opening [ which is clearly incorrect

Resources