4

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 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

5 Comments

While your code may correct as answer But Elaborating what your code does, It can improve the quality of your answer. Checkout the Article : How do I write a good answer?
@Ethan: Is there a way not to use the + sign and only draw only lines. I even tried other symbols, such as *, - and x. None worked. It seems only the + symbol works.
@Asarkar, this '+' above has nothing to do with the symbol used in the plot. Use with lines instead of with linespoints. Check help with how to change lines, points, colors, linewidth, pointsize, etc.
@theozh, Thanks. But what does the + sign do then?
In gnuplot I also tried using help plot sample. It was Sorry, no help for 'plot sample'. So a little bit of explanation of the sample keyword and + sign would be helpful.
4

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])

Comments

3

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

1 Comment

Thanks for your answers. I think I prefer answer 1 for its simplicity.
0

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

1 Comment

Currently I am writing a fortran module to call gnuplot for plotting in real time. I initially thought passing x and y arrays would be helpful. However, this creates a problem when replot command is used with new input data. So I have to save the data to file and plot them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.