4

so I want to plot from a data file which has an undetermined number of fields of x and y data (of unknown but constant length). I want to plot them all together on one graph, BUT with different colors.

Without specifying the index, they all plot together with the same color.

Using a for-loop on the index or writing the indicies individually DOES plot them together with different colors, BUT the program will not know how many indicies to loop over.

Setting a really high number for the max of the for-loop works, but makes error messages when it runs out of data.

Is there any way to manipulate the data in gnuplot to derive the number of indices for the for-loop?

1
  • I think I can answer your question, but a little more information would be helpful. Which version of gnuplot are you using? It would also help if you could post some example code/data that you are using. Commented Feb 12, 2013 at 4:24

1 Answer 1

5

If you have gnuplot >= 4.6.0, you can use the stats command:

#!/usr/bin/env gnuplot

reset

datafile = 'data.dat'

set terminal png size 600,400
set output 'test.png'

stats datafile

plot for [i=1:STATS_blocks] datafile index (i-1) pt 7 ps 2 title 'record '.i

If your data looks like this (with two blank lines separating the data blocks)

1 1


2 2


3 3


4 2


0 3

That script will make this plot:

enter image description here

The same should apply for blocks of data with more than one (x,y) pair.

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

2 Comments

Records didn't work (that gave the total number of lines I think -- its the same in your example), but STATS_blocks did. Thanks for steering me that direction! I spent two hours searching the internet for solutions yesterday and didn't come across this stats tool -- very useful!
You are absolutely right, I should have used STATS_blocks. I updated my answer accordingly.

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.