Your first gnuplot plot takes four ideas: choose an output terminal, optionally name a file, label the axes, and call plot with a function. The script below draws a sine wave and saves it as a PNG image — you can run it unchanged in gnuplot 6.0.
set terminal pngcairo size 800,600 enhanced font "Sans,12"
set output "sine.png"
set title "Sine Wave"
set xlabel "x"
set ylabel "sin(x)"
set grid
plot sin(x) with lines linewidth 2 linecolor rgb "#5e2d79" title "sin(x)"
What each part does
set terminal pngcairo selects the output device. The pngcairo terminal renders a high-quality PNG using the Cairo library; size 800,600 sets the canvas in pixels and font "Sans,12" picks the default font. If you skip the terminal and output lines entirely, gnuplot opens an interactive window instead (for example the qt or wxt terminal), which is ideal while experimenting.
set output "sine.png" tells gnuplot where the rendered image goes. Everything after this line is written to that file until you change the output or close it. The next three lines are pure annotation: set title puts a heading above the plot, set xlabel and set ylabel label the axes, and set grid draws light grid lines behind the data.
The final line does the work. plot sin(x) evaluates the built-in sine function over the default x range of -10 to 10. The clauses after the function refine the curve: with lines connects the sampled points with a line, linewidth 2 doubles the line width, linecolor rgb "#5e2d79" sets an exact color, and title "sin(x)" sets the legend (key) entry. Run the script by saving it as first.gp and typing gnuplot first.gp in a terminal, or by pasting it line by line into an interactive gnuplot session.
Variants and going further
To explore interactively, delete the first two lines and run the rest in the gnuplot console: the plot appears in a window where you can zoom with the mouse and re-run a modified plot command instantly. To plot two functions together, separate them with a comma: plot sin(x) title "sin(x)", cos(x) title "cos(x)". To change the visible window, add set xrange [-6.28:6.28] before the plot. Saving to other formats works the same way: swap the terminal to pdfcairo and the output name to sine.pdf for a vector PDF.
Common mistakes
- Blank or zero-byte PNG: you changed the terminal or closed gnuplot before the plot was drawn. The file is only finished when a
plotcommand runs afterset output— and in interactive sessions, runset outputagain (orunset output) to flush and close the file cleanly. - “unknown terminal type”: the script names a terminal your build does not have. Type
set terminalin gnuplot to list the terminals compiled into your copy, then pick one from that list.
Related tutorials: Plot CSV Data in Gnuplot, Titles, Labels and the Key. See also the full Gnuplot Tutorials index.
Try it online: Every command in this tutorial runs in the online terminal, including the plot sin(x) line. Paste the script into the Gnuplot Online terminal and run it in your browser — no install needed. To run gnuplot locally instead, see the installation guide and the download page.
Go deeper: For a systematic walk through gnuplot from first plots to publication-ready figures, see Gnuplot in Action by Philipp K. Janert — Janert is a gnuplot developer, and the book is the most complete tutorial reference for the program. Affiliate link: see our Affiliate Disclosure.
