1
votes

I am drawing a simple graph using GNUPlot but output is not what I expected order.
Here is my script :

set title 'cost function vs clusters'
set xlabel '#clusters'
set ylabel 'cost function'
set terminal postscript
set output '| ps2pdf - output.pdf'
plot filename using 1:2 title "x" with linesp  

Data on which I am plotting the data is :

13  0.004945370902817711
8   0.06739505462909719
2   0.28378378378378377
17  0.004657849338700402
5   0.015181138585393904
20  0.0018401380103507763

And here is my ouput :
enter image description here

I want points to be joined in sequential order of x.
How I can achieve this?

1
could you please send also the data? I bet there is some quirk there. And the gnuplot version and OS are valuable information. Besides the filename should have quotes around. Anyway AFAIK gnuplot does not sort data, but you could use some script to do this on the fly. On uniixes, replacing filename by "< sort -n filename" is a good starting point - bibi
I am sending the filename through arguments, even after sorting it is showing the same result. gnuplot 4.6 patchlevel 3 Linux 13.10 - neel
that's weird because for me the sort -n works pretty well. The problem might be "sending the filename through arguments". Could you provide a complete script so that we can test it? - bibi
I am running like this : gnuplot -e "filename='aboveFile'" plot.gpi - neel

1 Answers

2
votes

For the data you showed, you can use smooth unique. This sorts the data and replaces the same x-values with a single point having the averaged y value. If you can be sure, that you'll never have two equal x-values, then you can use this:

set title 'cost function vs clusters'
set xlabel '#clusters'
set ylabel 'cost function'
set terminal pdfcairo
set output 'output.pdf'
plot filename using 1:2 smooth unique title "x" with lp

And call it with gnuplot -e 'filename="aboveFile"' plot.gpi.

The other variant using sort also works fine:

plot '< sort -n '.filename using 1:2 title "x" with lp