15

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?

sodiumnitrate
  • 2,899
  • 6
  • 30
  • 49
  • gnuplot will probably never implement a "real" internal sort feature. I guess the idea is that you are supposed to use existing tools e.g. `sort` under Linux (for Windows you have to install comparable tools, e.g. http://gnuwin32.sourceforge.net/packages/coreutils.htm). There are cumbersome attempts with gnuplot-only, e.g. https://stackoverflow.com/q/54144027/7295599. Although, in gnuplot 5.4 there is `zsort` which, however, is not maintaining the order in a two column sort (however only under Windows), see: https://stackoverflow.com/q/67801386/7295599 – theozh Aug 11 '22 at 18:58

1 Answers1

18

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'

Christoph
  • 47,569
  • 8
  • 87
  • 187
  • 4
    `plot '< sort -nk2 file.dat' u 2:3` will use a numerical sort on column 2. This way the header will stay in its place, and you can use choose the column for x-values. – arekolek Jun 27 '16 at 19:13