To plot a CSV file in gnuplot, tell gnuplot the separator is a comma, then name the file and the columns you want with using. The script below plots two temperature series from one CSV file, using the header row for the legend.
Suppose measurements.csv looks like this — a header row followed by time and two sensor columns:
time,sensor_a,sensor_b
0,21.4,21.1
1,21.9,21.5
2,22.6,22.0
3,23.1,22.8
4,23.4,23.2
set datafile separator comma
set key autotitle columnhead
set title "Sensor Readings"
set xlabel "Time (s)"
set ylabel "Temperature (C)"
set grid
plot "measurements.csv" using 1:2 with linespoints, \
"measurements.csv" using 1:3 with linespoints
What each part does
set datafile separator comma switches the field separator from the default whitespace to a comma, so each comma-separated value lands in its own column. The keyword comma is built in; for other files you can pass a character such as set datafile separator ";" for semicolon-separated exports.
set key autotitle columnhead handles the header row: gnuplot reads the first line as column titles instead of data, and the plot command automatically uses those titles (“sensor_a”, “sensor_b”) as legend entries. Without this setting, gnuplot would try to parse the text header as numbers and warn about bad data.
In the plot command, using 1:2 means “x from column 1, y from column 2”. The second plot clause re-reads the same file with using 1:3 to draw the second sensor against the same time axis. with linespoints draws a line with a marker at each data point, which suits measured data. The backslash at the end of the first plot line is a line continuation — it lets one long command span two lines.
Variants and going further
If your CSV has no header, drop the autotitle columnhead line and give each series an explicit legend text instead, for example plot "data.csv" using 1:2 with lines title "Sensor A". If a header exists but you prefer your own titles, keep set key autotitle columnhead and add explicit title clauses in the plot command — an explicit title overrides the column title for that series. You can also compute on the fly: using 1:($2 * 1.8 + 32) converts column 2 from Celsius to Fahrenheit while plotting — the $2 form means “the value of column 2” inside an expression.
Common mistakes
- Straight diagonal or nonsense points: the separator was not set, so gnuplot read each whole line as one whitespace-separated field. Confirm
set datafile separator commaappears before theplotcommand in the same session. - “Bad data on line 1” warning: the header row is being parsed as data. Add
set key autotitle columnheadso line 1 is treated as titles, which is the intended mechanism for headed CSV files.
Related tutorials: Your First Gnuplot Plot, Axis Ranges, Tics and Log Scales, Plotting Date and Time Data. See also the full Gnuplot Tutorials index.
Try it online: The online terminal accepts pasted data through a heredoc or an inline datablock, so you can adapt this script with your own small CSV sample. 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.
