15

Does anybody know how to extract some data of smooth cspline curve for a given data?

For instance, there is a data file which has 2 columns corresponding to x and y values. I can draw the data with smooth cpline curve by the following commands

p 'data' w lp, ""  smooth csplines

I want to extract the smooth cpline curve as an another data file.

Christoph
  • 47,569
  • 8
  • 87
  • 187
user4914499
  • 344
  • 1
  • 3
  • 12

2 Answers2

13

This can be done by setting a table. Consider the following data file:

0 1
1 2
2 3
3 2
4 2
5 4
6 8
7 5
8 3
9 1

The data itself and its csplines interpolation look like this:

enter image description here

To print the interpolation to a table one does the following:

set samples 100
set table "table_100"
plot "data" smooth csplines
set samples 20
set table "table_20"
plot "data" smooth csplines
unset table

set samples determines the number of points used to construct the spline curve. And you can visualize it:

set key left
plot "data" pt 7 t "Original data", \
     "table_100" w l t "Splines (100 samples)", \
     "table_20" w l t "Splines (20 samples)"

enter image description here

Miguel
  • 7,497
  • 2
  • 27
  • 46
6

Use set table 'temp.dat' to redirect the plotted data points to an external file

set table 'temp.dat'
plot 'myfile.dat' using 1:2 smooth cspline
unset table

To test it

plot 'myfile.dat' using 1:2 with points title 'original points',\
     'temp.dat' using 1:2 with lines title 'smoothed curve'
Christoph
  • 47,569
  • 8
  • 87
  • 187
  • Bad synchronization :-D I was writing my answer when you posted this. It's impossible to beat you! – Miguel May 27 '15 at 07:00
  • @Miguel :) Sorry, I think I should step back a bit to not disperse most of the other answerers... Nice, that you added some examples to your answer. I had been looking for a duplicate question, but didn't find an appropriate one. Now we have your answer as nice reference – Christoph May 27 '15 at 07:09
  • Well, at least with the gnuplot tag one has a chance to answer. Have a try at being the first to answer a python question... – Miguel May 27 '15 at 07:18
  • Does anyone know how to overlap the "dot" and the "line" in the legend, so that I will get a single legend entry instead of two different entries for the same data? – Sufyan Sep 20 '20 at 12:02