0% found this document useful (0 votes)
0 views11 pages

Gnuplot Notes

The document provides a comprehensive guide on using Gnuplot for data visualization, function plotting, and curve fitting. It includes commands for plotting functions, changing axis ranges, creating piecewise functions, fitting data to user-defined functions, and plotting parametric equations. Additionally, it covers saving plots and customizing plot appearance with labels and styles.

Uploaded by

sarkarrounak407
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views11 pages

Gnuplot Notes

The document provides a comprehensive guide on using Gnuplot for data visualization, function plotting, and curve fitting. It includes commands for plotting functions, changing axis ranges, creating piecewise functions, fitting data to user-defined functions, and plotting parametric equations. Additionally, it covers saving plots and customizing plot appearance with labels and styles.

Uploaded by

sarkarrounak407
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Gnuplot

● Data Visualization

● Function Plotting

● Curve fitting
● Open command prompt/ terminal
● Type gnuplot and press ‘Enter’ key
● Try plotting any functions like, x, sin(x), log(x) etc using:
● plot sin(x), plot cos(x)....
● To label y- and x-axis: set ylabel “Sin(x)” ;
● set xlabel “x”
● Put title of your plot: set title “function plotting..”
● Type: replot
● To save the plot: set term jpeg
● set output “[Link]”
● replot
Changing x and y range
● To change the range of X axis:
● set xrange[min:max]

● For Y axis: set yrange [min:max]

● To change the tick-marks:


● set xtics (0.1,0.2,0.3)
● set xtics 0.2 # Places ticks at the interval of 0.2
● set ytics 0,10 # starts at 0 and increments by 10
Typical command for plotting
• f(x)=....
• set xrange[a,b]
• set yrange[c,d]
• plot f(x) w lp lw 4 lc 1
• set ylabel “f(x)”
• set xlabel “x”
• set title “function plotting..”
• set output “[Link]”
• replot
Typical command: Plot from script file
● Create a file with name “gnuplot_example.plt” using notepad
● Copy following and save the file:
● f(x)=exp(-x**2/2)
● plot [t=-4:4] f(t) title "Bell Curve", t**2/16 title "Parabola"
● set xtics font "Times-Roman, 14"
● set ytics font "Times-Roman, 14"
● set title font "Times-Roman,14"
● set title "Multi-plot Example"
● set xlabel"x"
● set ylabel "y"
● set xlabel font ",14"
● set ylabel font ",14"
● replot
● pause -1 "Hit any key to continue"
Piecewise function plotting
• Let, g(x)=0 for x<0
• =1 for x≥0
• It can be defined in gnuplot using ternary operator:
g(x)=x>=0?1:0
● The general form of ternary operator is: a?b:c

● Few more examples: f(x)= sin(x) for 0≤x<1

● = 1/x for 1≤x<2


● = ∞ elsewhere
● To define this in gnuplot:

● f(x)=0<=x && x<1 ? sin(x): 1<=x &&x<2?1/x:1/0


Curve fitting
● By fitting a set of data, saved in datafile, with a user defined
function, we can calculate the parameter value for the best fit.
● Suppose we have a set of data which is saved in a .dat file:
● x y
● 0 0
● 1 .25
● 2 .5
● 3 .75
● 4 1.25
● 5 1.30
● 6 1.55
● 7 1.80
● 8 2.05
● To plot the data: plot “datafile_name.dat”
● Now suppose we want to fit this data to a straght line:
f(x)=mx+b.
● In gnuplot, type:
● f(x)=m*x+b
● fit f(x) ‘[Link]’ u 1:2 via m,b
● plot ‘[Link]’ u 1:2 w p, f(x) w l
● set xlabel “...” etc....
Parametric Functions
• x=f(t), y=g(t)
• We want to draw a vertical line:
• set parametric
• const=3
• set trange[1:4] Spiral:
• set xrange [0:5] • x=r(t)*cos(t)
• set yrange [0:5] • y=r(t)*sin(t)
• plot const,t
• For spiral, r(t)=t
plot [0:2*pi] sin(t), cos(t)
Circle, polygons:
• x=sin(t), y=cos(t)
• Set xrange[-1:1] plot [0:2*pi] sin(t), cos(t)
• Set yrange [-1:1] set samples (n+1) for n-gon
• Plotting a circle using parametric plot of Using polar plot of gnuplot:
gnuplot: In this representation the cartesian x
coordinate is equal to r cos(t) and the
cartesian y coordinate is equal to r
• set parametric sin(t).
• set trange[0:2*pi] To draw a unit circle using the polar
coordinate system in gnuplot use the
• r=1.0 following simple command:
• set size ratio -1
• fx(t)=r*cos(t) set polar; plot 1
• fy(t)=r*sin(t)
• plot fx(t),fy(t)
linestyle, pointstyle, linewidth etc...

You might also like