3

I have a datafile with multiple columns, the first two indicating the position and the others indicating other properties (such as number of items sent from this point). eg:

1  1  1  57.11
2  1  2  62.40
3  4  1  31.92

What I want to do is plot the points at the positions, but use values from the other columns to vary point type and size (for example). However I can't seem to find a way to reference columns in the plot. I know of the use of "variable", but I cant find a way to use multiple variables.

What I want is something like the following:

plot "mydata" using 1:2 notitle with points pt ($3) ps ($4/10)

so that pt and ps use the value for each point taken from the third and fourth columns respectively.

Is this even possible in gnuplot? Is there some sort of work-around?

1 Answer 1

5

You should be able to use the keyword variable to do something like this:

plot 'datafile' using 1:2:3:4 w points ps variable lc variable

Or possibly mapping the value to a palette:

plot 'datafile' using 1:2:3:4 w points ps variable lc palette

The keyword variable and/or palette causes gnuplot to read the properties from the file and they both require an extra column to be read via using. Of course all the usual stuff with using applies -- You can apply transforms to the data, etc:

plot 'datafile' using 1:2:3:($4+32.) w points ps variable lc palette

I don't remember off the top of my head whether the 3rd column will be the pointsize or the color here, and I don't have time right now to play around with it to figure it out. You can do the experimenting and post a comment, or I'll come back to this when I have time and add an update.


Some of the other properties (e.g. pointtype) can't be changed quite to easily using variable. The easiest way to do this is to use filters with the gnuplot ternary operator.

First, write a function that returns a pointtype based on the data from 1 column of the datafile:

my_point_type(x) = x   

Here I use a simple identity function, but it could be anything. Now, you can loop over the pointtypes you want (here 1-10) making a plot for each:

plot [for PT=1:10] 'datafile' u 1:((my_point_type($3) == PT) ? $2:NaN) with points pt PT

This assumes that the column with pointtype information is the 3rd column and that the second column holds the position information. This can also be combine with the stuff that I demonstrated above.

Sign up to request clarification or add additional context in comments.

5 Comments

I've tried using "variable" multiple times, however it appears that only some of the parameters accept it. For instance, ps is happy with variable, however pt is not, and using it with other parameters gives the same error: undefined variable: variable Along those line, ive also tried naming varaibles as found in an example somewhere: (I found the example last night, but I couldnt find it again this morning) plot "datafile" using 1:2:(myvar1=$3):(myvar2=$4) notitle with points pt myvar1 ps myvar2 which just gives the same undefined variable error.
@OverlordAlex -- Ahh, sorry, I saw pt and I just assumed you were trying to change the color. No, there is no way to change the point-type like that. The only solution is to filter the data into the various point-types. I'll update when I get time...
@OverlordAlex -- I've updated. Maybe you meant ... using 1:2:(myvar=$3,$3):(myvar=$4,$4) ... I'm not sure if that'll work though.
You're last edit seems to be the closest that I can get. Luckily/Unfortunately I've been asked to rewrite it in python using matplotlib, but thank you for the help
@OverlordAlex -- Python's a really nice programming language. It's the first thing that I reach for these days for quick projects. I've used matplotlib a little, but not a whole lot (mostly because I love gnuplot so much and wrote my own python wrapper for it). Good luck.

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.