how to automatically sort the x values in gnuplot

sodiumnitrate picture sodiumnitrate · May 25, 2015 · Viewed 7.7k times · Source

When I try to plot something with linespoints, unless the values that go to the x axis are already sorted within the file, each point gets connected to the point that is on the next line of the file:

enter image description here

Only when I sort the values in the file I can get the desired effect, which is that each point gets connected to the point with one smaller and one larger x value:

enter image description here

Is there a way to do this within gnuplot, without having to sort the files in bash?

Answer

Christoph picture Christoph · May 25, 2015

Gnuplot offers some smoothing filters which as first step sort the data by their x-values. plot ... smooth unique first sorts the data points by their x-value and for equal x-values it computes the average y-value. So if you are sure that the x-values are unique, then you can use this option. Otherwise you must use an external tool or script to do the sorting with plot '< sort file.dat'