Gnuplot ‘Undefined Variable’ Error: Causes and Fixes

Your script stops with a message like this:

gnuplot> plot "data.dat" using 1:temperature with lines
                       ^
         undefined variable: temperature

The caret points at a name gnuplot has never seen. Unlike a spreadsheet, gnuplot does not know the names of your data columns — and it also does not invent variables you forgot to define.

What this message means

While evaluating an expression, gnuplot met an identifier that is not a built-in function, not a user variable defined earlier in the session, and not something it can resolve. The fix depends on what you meant that name to be, which is why this error has a few distinct causes.

Causes, most likely first

  1. You used a column name or bare column reference in using. In a plot command, using 1:2 refers to columns by number. A word, or a bare $2-style reference outside an expression, does not do what users coming from other tools expect.
  2. A string and a variable got mixed up. Quoted text is a literal; the same word unquoted is a variable name. A filename or label stored without quotes — or quotes lost in an edit — triggers this error.
  3. A fit parameter was never defined. In fit f(x) "data.dat" via a, b, the parameters a and b must exist (or at least the function must be defined in terms of known quantities) before the fit runs.
  4. A typo or a variable defined after use. Gnuplot reads scripts top to bottom; a definition that sits below the plot command that uses it is, at that point, still undefined.

Fixes, step by step

1. Refer to data columns by number, or with column()

Inside a using expression, $2 means “the value in column 2 of the current row”, and column(2) is the clearer equivalent. If your file has a header row you want to use as the plot title, ask for the header text explicitly:

plot "data.dat" using 1:2 with lines title columnheader(2)

For arithmetic on columns, keep everything inside the parentheses of a using expression:

plot "data.dat" using 1:($2*1000) with lines   # column 2, scaled
plot "data.dat" using 1:(column(2)/column(3)) with points

2. Quote strings, define variables before use

Filenames, labels and titles are strings and stay in quotes. Anything you intend as a variable must be assigned earlier in the script:

filename = "data.dat"
scale = 1000.0
plot filename using 1:($2*scale) with lines title "scaled data"

3. Define fit functions and give parameters starting values

f(x) = a*x + b
a = 1.0
b = 0.0
fit f(x) "data.dat" using 1:2 via a, b
plot "data.dat" using 1:2 with points, f(x) with lines

Defining the function alone is not enough for a smooth fit — without starting values, gnuplot begins from defaults that may be far from your data. Our curve fitting tutorial covers the full workflow, including reading the fitted values back afterwards.

A minimal script that works

reset
x0 = 2.0
f(x) = x0 * x
set terminal pngcairo size 800,600
set output "line.png"
plot f(x) with lines title "y = 2x"
set output

This defines every name before it is used, so it runs cleanly. If your script still fails, run it line by line in an interactive session: the first line that raises the error tells you exactly which name is undefined, and you can check whether it was supposed to be a column, a string, or a variable you meant to set.

Still stuck?

The official documentation at gnuplot.info includes the full command reference, and the gnuplot-info mailing list linked there is the best place to ask when a script misbehaves in a way this page does not cover — include the exact error message and a small sample of your data when you post.

Related troubleshooting

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *