#compsci #physics #software Gnuplot is a command-line program that can generate 2- and 3- dimensional plots of [[Числовая функция|functions]], data, and data fits. ## Syntax [[Python core data types summary|Python]]-esque syntax: ```gnuplot f(x)=sin(x) plot f(x) ``` ### Arrays ```gnuplot array A[6] A[1]=1 A[2]=2.0 A[3]={3.0, 3.0} A[4]="four" A[6]=A[2]**3 do for [i=1:6] { print A[i], B[i]} ``` Result: ```gnuplot 1 2.0 {3.0, 3.0} four 8.0 ``` (as seen here, gnuplot supports [[iteration]] and block-structured if/else/while/do constructs) Array indices run from 1 to N for an array with N elements. Element i of array A is accessed by A[i]. Function index(Array, %value%) returns an intenger i such that A[i] is equal to %value%. Return of 0 indicates that no match was found ![[Pasted image 20241007213921.png]] ### Setting output Output defined by **set term %value%** For list of values, use **set terminal** Common values: qt windows (on [[Windows]]) png pdf pm3d (for 3dimensional graphs) (**set pm3d**, not set term) Setting fonts: ```gnuplot set term png font "arial" set term png font "/usr/loca/fonts/ttf/arial.ttf" ``` Setting font size: ```gnuplot set term png tiny/small/medium/large/giant ``` Setting output file: ```gnuplot set output "filename" ``` For tables: ```gnuplot set table "output.dat" replot unset table ``` ### Linetype and linecolor ```gnuplot plot sin(x) linetype %value% ``` for different line types/colours where %value% is a number ```gnuplot plot sin(x) linecolor rgb "#FF00FF" plot sin(x) lc rgb "#FF00FF" ``` Most terminals allow you to set an explicit background color: ```gnuplot set term png background rgb "gray75" ``` ### Linewidth ```gnuplot plot sin(x) linewidth 5 ``` ### Dashtype The dash pattern is a separate property associated with each line: ```gnuplot plot f(x) dashtype %value% ``` where %value% is a number ### Strings Title: ```gnuplot plot 'data.4' title "Title for plot 4" ``` ```gnuplot set title "Graph something" offset -10 font "Arial, 20" textcolor "red" ``` Axes labels: ```gnuplot set xlabel "This is x" font "Helvetica, 14" set ylabel "This is y" font "Helvetica, 14" ``` Marks on axes: ```gnuplot set xtics 0, pi/2 // step pi/2 with starting point 0 ``` Logarithmic scale: ```gnuplot set logscale xy 10 ``` ### Plotting styles Gnuplot supports many plotting styles: - **boxes** - **lines** - **arrows** - **vectors** - **candlesticks** - **circles** - **dots** - **points** And other, for more information consult the documentation. For points: ```gnuplot plot sin(x) with points pt 12 ps 1.5 ``` where pt = pointtype (value means symbol name), ps = pointsize ### Commands #### cd Change directory ```gnuplot cd '%directory-name' ``` #### fit Fits a user-supplied real-valued expression to a set of data points, using the [[Regression line|least squares]] algorithm. ![[Pasted image 20241007221929.png]] ![[Pasted image 20241007233238.png]] #### plot Main command. Modes: **set polar** (unset polar) **set parametric** (![[Pasted image 20241007224004.png]]) For 3D plots: ```gnuplot set pm3d splot ... ``` #### Other Other commands include if, for, print, pwd, quit, reset, etc. ### Ranges ```gnuplot set xrange [0:12] set yrange [0:3] ``` ### Multiplot mode Multiplot mode allows for the plotting of several graphs in one image ```gnuplot set multiplot set size 0.5, 0.5 // relative size set origin 0., 0. // relative origin plot sin(x) set size 0.5, 0.5 set origin 0.5, 0.5 plot cos(x) unset multiplot set size 1., 1. // resetting size set origin 0., 0. // resetting origin ``` ![[Pasted image 20241007230145.png]] ### Data manipulation Say you have a file "data.dat" with 2 columns and you want to multiple the values in the first column by 2: ```gnuplot plot 'data.dat' using ($1*2):2 with points ``` ### Histograms ```gnuplot bin_width=0.1 // select bin width set boxwidth 0.9*bin_width absolute // histogram style set style fill solid 1 noborder // style bin_number(x)=floor(x/bin_width) // box distribution rounded(x)=bin_width*(bin_number(x)+0.5) // box middle coord plot 'data.dat' using (rounded($2)):(1) smooth frequency with boxes ``` ![[Pasted image 20241007232656.png]]