Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Plotting data array as variable (not in files)

9,889 views
Skip to first unread message

MamboKnave

unread,
Mar 7, 2012, 2:57:07 PM3/7/12
to
I have hard time to understand how I can do the following.

I'm starting gnuplot with an input command file:

gnuplot -persist < inputFile.gpl

Within the inputFile.gpl I want to have two arrays X and Y as gnuplot
variables. For instance:

Y = [0.8117, 0.944, 0.9937, 1.0145, 0.9369, 0.9574, 0.9861, 0.9484,
0.9891, 0.9987, 1.0485, 0.6612, 0.6899, 0.87]
X = [817, 818, 819, 822, 823, 824, 825, 826, 829, 830, 831, 901, 902,
906]

I need gnuplot to plot Y(X) but I do not know how to set these arrays
as variables within the command file inputFile.gpl

Basically, the two data arrays X, Y are not stored in external files.
They are variables within the inputFile.gpl and gnuplot should plot
Y(X)

Can someone please give me a hint?

Thanks!

Péter Juhász

unread,
Mar 8, 2012, 2:13:56 PM3/8/12
to
The short answer is that you can't. There is no syntax like that in
gnuplot, nor is there an array data type.

I also don't really understand your statement "the two data arrays X,
Y are not stored in external files". They *are* stored in an external
file, right there in inputFile.gpl. And once the data are there in the
file, it is possible to use a script to convert them in a format that
gnuplot understands (columns of ascii data), or to generate the data
in that format in the first place.

Now if you want to ask how to incorporate data into your command file,
that's easy:

plot '-' using 1:2
817 0.8117
818 0.944
819 0.9937
822 1.0145
823 0.9369
e

See "help special-filenames".

Péter Juhász

Petr Mikulik

unread,
Mar 13, 2012, 12:01:17 PM3/13/12
to MamboKnave
It's a strange data set-up, but try this:

X="0.1 0.2 0.3 0.4 0.5"
Y="-1 0 2 4 8"

set parametric
set trange [1:words(X)]; set samples words(X)
plot (0+word(X,int(t))),(0+word(Y,int(t)))

---
PM

MamboKnave

unread,
Mar 14, 2012, 5:06:52 PM3/14/12
to
On Mar 13, 9:01 am, Petr Mikulik <miku...@physics.muni.cz> wrote:
> It's a strange data set-up, but try this:

Thanks guys. Much appreciated.

Actually, I dug more into the issue and I solved the problem similarly
to what Peter suggested.
Problem is that I must read the data more than once. Hence they must
be duplicated.

Thanks again.

Kevin

unread,
Oct 4, 2012, 10:55:59 PM10/4/12
to MamboKnave
This is a brilliant workaround for gnuplot not having arrays. This snippet should be part of every gnuplot tutorial in christendom.

amelco...@gmail.com

unread,
Jun 8, 2015, 7:51:31 PM6/8/15
to
3 year has been passed and gnuplot still doesn't have arrays.

This workaround still works and it is awesome!

HALSA

unread,
Oct 22, 2015, 6:09:21 AM10/22/15
to
Extremely useful trick:)

xlr...@gmail.com

unread,
Sep 6, 2016, 5:11:18 PM9/6/16
to
Absolutely brilliant!

I just used it to get contour labels on a contour plot of a nonuniform matrix data file. I was about ready to give up on this.

Here's the gist of it.
X="0.1 0.2 0.3 0.4 0.5"
Y="-1 0 2 4 8"
set multiplot
splot 'dfile' matrix u (0+word(X,int($1+1))):(0+word(Y,int($2+1))):3 with lines
splot 'dfile' matrix u (0+word(X,int($1+1))):(0+word(Y,int($2+1))):3 with labels

Ethan A Merritt

unread,
Sep 6, 2016, 6:22:51 PM9/6/16
to
I do not see anything here that would require multiplot.


Have you looked at the on line demos for contouring nonuniform data?

http://gnuplot.sourceforge.net/demo_cvs/scatter.html

xlr...@gmail.com

unread,
Sep 7, 2016, 3:33:35 PM9/7/16
to
I don't either, but a single splot command to plot both lines and labels generated an error message. I forget what it was. splot would merrily plot lines OR labels, but not both in one command. So I tried multiplot, and that worked. :)
0 new messages