2
votes

Gnuplot version 5.2 supports arrays. As given here, one can declare 1D arrays and plot them

array A[100]
do for [i=1:100] { A[i] = sin(2*pi*i/100.) + 0.1*rand(0) }
plot A

This plots the matrix A with the index i.

Is there a way to have two 1D arrays (Eg: x and y) and plot them y vs x.

OR

Declare a 2D array A and plot the 2nd column of A with respect to the first column of A?

4

4 Answers

2
votes

The trick is to have gnuplot generate a set of samples to plot. Instead of a filename you can provide the string '+' to generate a set of samples along one dimension or '++' to generate a set of samples along two dimensions. Gnuplot calls these "special file names". In your case you want to generate 100 samples (integers from 1 to 100) and use each sample as an index into your arrays.

array A[100]
array B[100]
do for [i=1:100] {
    A[i] = something
    B[i] = something else
}

plot sample [i=1:100] '+' using (A[i]):(B[i]) with linespoints

The keyword "sample" guarantees that the term in square brackets will not be mis-interpreted as setting the horizontal range ("set xrange") of the plot.

Documentation entries

  • help +
  • help special-filenames
  • help sampling
2
votes

Answer #2 If the two arrays A and B are guaranteed to have the same size, a simpler plot command is possible. We start by noting that all of the following plot commands are equivalent.

plot A
plot A using 1:2
plot A using (column(1)):(column(2))
plot A using ($1):($2)
plot A using ($1):(A[$1])

This is because for purposes of plotting an array A is treated as providing two columns of information, the index i (column 1) and the value A[i] (column 2). Following standard gnuplot syntax, each field in the "using" specifier of a plot command may contain either a bare column number or an expression in parentheses. Inside an expression the value of a column can be referred either by prefixing a $ sign or by using the function column(i).

With this in mind, it follows that the command below plots the values of array B against the values of array A.

plot A using (A[$1]):(B[$1])
2
votes

Answer #3 You ask whether there is an alternative to make A a 2-dimensional array. Not exactly, but remember that in gnuplot floating point numbers are actually complex values. So you could use the real and imaginary components of each A[i] to place it in the x/y plane:

array A[36]
set angle degree
i = {0,1}       # i = sqrt(-1) as a complex value
do for [n=1:36] {
    A[n] = cos(real(10.*n)) + i * sin(real(10.*n))
}
plot A using (real(A[$1])):(imag(A[$1])) with lp

enter image description here

0
votes

Is there any special reason why you want to have data in arrays first?

As you are filling your arrays with values of functions, you can also plot two functions (or as you say two columns of a 2D-array) directly against each other without first defining arrays in a do for loop. Just define some functions and plot them against each other. Use set samples to define the number of points and use plot sample [] for setting the range. I guess this would be easier than setting the array size, doing the loop and "messing" around with the index i and the range and/or offsets.

### plot one function vs. another function
reset session

f(x) = sin(x) + 0.1*rand(0)
g(x) = cos(x) + 0.1*rand(0)

set samples 100
plot sample [0:2*pi] '+' u (f($1)):(g($1)) w lp pt 7 lc rgb "red"
### end of code

enter image description here