gnuplot - Plot data and fit-functions of multiple files into one plot -
i have n input files , want plot data of these files together fit function 1 single plot (i.e. 1 plot files, data , fit-function).
after long time of fiddling found solution (see below), find "cumbersome , ugly" , i'm wondering if there better, more elegant way of achieving same thing.
i should i'm on gnuplot 5.0 under windows. test script below doesn't specify terminal (i'm testing windows , wxt), final script use pngcairo terminal.
things find sub-optimal solution:
- i need 2 intermediary tables $data , $fit. original attempt use for{} loop read each file in turn perform fit , generate plot, didn't work out.
- rather using fit function, plot fit curve (in simple case straight line) data table. experimented creating on-the-fly user functions using eval couldn't quite figure out (especially how keep them in sync data).
- i want fit-equation displayed in chart. setting labels, nicer if part of key.
my test data:
data1.dat 100 0.15 200 0.29 300 0.46 400 0.58 data2.dat 100 0.12 200 0.22 300 0.35 400 0.48 data3.dat 100 0.1 200 0.22 300 0.29 400 0.40
my gnuplot script:
set key left set xrange [0:*] set yrange [0:0.5] # user function linear fit lin(x) = slope * x + offset max(a,b) = ((a>=b)? : b) file_list = "data1 data2 data3" x_max = 0 # first write data of interest (memory) table set table $data [name in file_list] { filename = name . ".dat" plot filename u 1:2 print "" print "" x_max = max(gpval_data_x_max, x_max) } unset table x_max = max(gpval_data_x_max, x_max) num_indices = words(file_list) # calculate linear fit each dataset set sample 2 set table $fit [i = 0:(num_indices-1)]{ fit lin(x) $data index using 1:2 via slope, offset plot [0:x_max][0:0.5] lin(x) set label (i+1) sprintf("%s = %.3g*x + %.3g", word(file_list, i+1)."(x) ", slope, offset) @ 200,(0.20 - 0.05*i) } unset table set title "data , linear fit" set xlabel "x" set ylabel "y" #now got both data , fit files, plot @ once plot [i = 0:(num_indices-1)] $data index title word(file_list,i+1) points lc i+1, [i = 0:(num_indices-1)] $fit index lines lc i+1 noti
Comments
Post a Comment