Key features:
Thousands separator, comma separator, print fitting variable on graph, fit multiple curves in same image, fit curve to only one range of full dataset, read data from CSV filetype, sprintf, formatted printing
I created this image during some personal calculations. The figure has several useful features:
Data was obtained from CSV files, unlike the simple whitespace-separated files I usually use. Reading CSV files is trivially activated by set datafile separator ","
Numbers >1,000 have a comma as the thousands' separator. This requires two lines of code: set decimalsign locale and set format "%'.f" Note: I generated my plots on a Mac; these commands may not work in all operating systems.
You can use only a certain range of a dataset during fitting by using fit [begin:end]
You can display the actual values of parameters computed during the script's execution! (Of course, the printing command must occur after the computation command within your script.) Use sprintf("Text description %format", VariableToPrintValueOf) e.g. sprintf("lin1 slope = %.2f",a1e) in my script below.
Each of my data files looked something like this:
#Date,Days Running,Category,Running total
2021-03-05,7,"Expense, misc",2.42
2021-03-08,10,"Expense, misc",6.75
2021-03-09,11,"Expense, misc",46.75
2021-03-10,12,"Expense, misc",74.21
2021-03-11,13,"Expense, misc",114.21
2021-03-15,17,"Expense, misc",135.58
2021-03-16,18,"Expense, misc",248.19
2021-03-16,18,"Expense, misc",258.73
2021-03-16,18,"Expense, misc",274.45
2021-03-17,19,"Expense, misc",319.83
2021-03-19,21,"Expense, misc",354.39
And here is the figure I created (script below):
reset
set terminal pngcairo truecolor nocrop enhanced font "arial,70" size 3200,2400 ## good ratio for publication: 1.336
set output "proj_v2_tmp.png" ## output filename
set fit logfile "proj_v2_tmp.log" ## output fitting log data filename
set datafile separator "," ## Read data from CSV filetypes
unset grid ## alternatively, use "with lines" after "plot"
set samples 10000 ## default=100... improves smoothness
set border 15 lw 9
set tics scale 1.25
set decimalsign locale ## REQUIRED TO GENERATE THOUSANDS SEPARATOR COMMA!
set format "%'.f" ## REQUIRED TO GENERATE THOUSANDS SEPARATOR COMMA!
set xlabel "days running" offset 0,.5
set xrange [-10:8*365] ## options: "[min:max]" , "[:]" is "perfect" range,
set xtics 0,365,10000 offset -.25,.25 mirror ## this is the spacing between tics on the x-axis
set mxtics 4 ## frequency of the MINOR x-tics;
set ylabel "total money" offset 1,0
set yrange [-2000:2e5]
set ytic 0,4e4,1e7 offset .25,0 mirror ## spacing between tics on the y-axis
set mytics 4
set key spacing 1.25 ## vertical spacing between legend lines.
set key samplen .5 ## length of line sample shown in legend.
#set key maxrows 5
set key maxcolumns 1
set key left top ## where to put legend: left right center top bottom center
set key font "arial,50"
#----------------------------------------------
## Just helpful to remind me of the datafile structure/format:
#COLUMN: 1 2 3 4
#DATA: Date Days Running Category Running total
#----------------------------------------------
## DEFINE THE FUNCTIONS TO BE FIT
lin1e(x) = a1e*x + b1e
lin2e(x) = a2e*x + b2e
lin3e(x) = a3e*x + b3e
## FIT PREVIOUSLY DEFINED FUNCTIONS TO DATA
fit [:365] lin1e(x) "data_file_first.csv" u 2:4 via a1e,b1e ## fit only points 1-365 to function
fit [366:730] lin2e(x) "data_file_first.csv" u 2:4 via a2e,b2e ## fit only points 366-730 to function
fit [731:] lin3e(x) "data_file_first.csv" u 2:4 via a3e,b3e ## fit only points 731-end to function
lin2(x) = a2*x + b2
lin3(x) = a3*x + b3
fit [:730] lin2(x) "data_file_second.csv" u 2:4 via a2,b2
fit [730:] lin3(x) "data_file_second.csv" u 2:4 via a3,b3
set pointsize 5
plot \
"data_file_first.csv" u 2:4 pt 6 lc rgb "red" lw 10 title "some data",\
lin2(x) w lines lc rgb "red" lw 10 title sprintf("lin2 slope = %.2f",a2),\
lin3(x) w lines lc rgb "red" lw 10 title sprintf("lin3 slope = %.2f",a3),\
"data_file_second.csv" u 2:4 pt 6 lc rgb "web-green" lw 10 title "other data",\
lin1e(x) w lines lc rgb "web-green" lw 10 title sprintf("lin1 slope = %.2f",a1e),\
lin2e(x) w lines lc rgb "web-green" lw 10 title sprintf("lin2 slope = %.2f",a2e),\
lin3e(x) w lines lc rgb "web-green" lw 10 title sprintf("lin3 slope = %.2f",a3e)
##################
# pointtype ("pt")
# 0 = none
# 1 = horz.line
# 2 = X
# 3 = X & horz.line
# 4 = open box
# 5 = closed box
# 6 = open circle
# 7 = closed circ.
# 8 = open up triang.
# 9 = closed up triang.
# 10= open down triang.
# 11= closed down triang.
# 12= open diamond
# 13= solid diamond
# linetype ("lt")
# -1 = black
# 0 = grey dotted
# 1 = purple
# 2 = green
# 3 = sky blue
# 4 = orange
# 5 = yellow
# 6 = navy blue
# 7 = red
# \305 = Angstrom
# {/Symbol *} = Greek *