How to Plot Date and Time Data in Gnuplot

Date plots in gnuplot rest on two settings that must agree: set xdata time declares the x axis as time, and set timefmt describes the exact date format used inside your data file. The script below plots daily visitors from a CSV file of ISO dates.

The file visitors.csv uses ISO dates (YYYY-MM-DD) in column 1 and a visitor count in column 2:

date,visitors
2026-09-01,412
2026-09-02,438
2026-09-03,501
2026-09-04,476
2026-09-05,529
set datafile separator comma
set key autotitle columnhead
set xdata time
set timefmt "%Y-%m-%d"
set format x "%b %Y"
set xtics rotate by -45
set title "Daily Visitors"
set ylabel "Visitors"
plot "visitors.csv" using 1:2 with linespoints title "Visitors"

What each part does

set xdata time switches the x axis into time mode: internally, dates are converted to seconds and the axis arithmetic works on those seconds. set timefmt "%Y-%m-%d" is the input pattern — it must match the file character for character in structure: %Y is a four-digit year, %m a two-digit month and %d a two-digit day, with the literal hyphens between them. A file with 01/09/2026 dates needs "%d/%m/%Y" instead.

The output side is separate: set format x "%b %Y" controls how the axis labels are printed — here an abbreviated month name and year, such as “Sep 2026” — regardless of the input format. set xtics rotate by -45 angles the labels so month labels do not collide. After the axis is declared as time, the plot command stays ordinary: using 1:2 reads the date from column 1 (parsed with timefmt) and the value from column 2. The first two lines are the same CSV handling as in any comma-separated file: separator plus header-as-titles.

Variants and going further

Timestamps with hours and minutes extend the pattern: set timefmt "%Y-%m-%d %H:%M" with labels from set format x "%H:%M". Multi-day data often reads better with one tic per week: in time mode, tic increments are in seconds, so set xtics 604800 places a tic every 7 days (7 × 24 × 3600). If your file already holds Unix epoch seconds, skip the pattern work: use set timefmt "%s" and gnuplot reads the numbers directly as seconds since 1970.

Common mistakes

  • All points pile onto one date or the plot is empty: timefmt does not match the file — wrong field order (month/day swapped), a two-digit year read with %Y instead of %y, or a separator mismatch such as slashes in the file but hyphens in the pattern. Compare them character by character; this is the most common date-plot failure by far.
  • Axis labels show huge numbers instead of dates: set xdata time was set but a later reset or a numeric set format x overrode the display. Re-assert set xdata time and set format x with a date pattern after any reset.

Related tutorials: Plot CSV Data in Gnuplot, Axis Ranges, Tics and Log Scales, Curve Fitting with fit. See also the full Gnuplot Tutorials index.

Try it online: Short date series paste cleanly into the online terminal, which makes it a safe place to debug a timefmt pattern. 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.

Similar Posts

Leave a Reply

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