Time series variable
set xdata time set timefmt "%Y-%m-%d" set format x "%Y/%m/%d"
- The first command - set xdata time - explicitly tells gnuplot that the "x" variable contains timestamps.
- The second command specifies how those timestamps are formatted in the input data file. For a list of timestamp component codes, please refer to the Time/date specifiers section (page 114) in the official gnuplot manual.
- The third command - set format x - specifies the format of tic labels on the "x" axis.
Narrow date range
xstart="2008-01-01" xend ="2014-06-01" set xrange [xstart:xend]
- The raw input data file contains data from 2005 to 2014, a span of about 9 years. You can plot a subset of the data by specifying a narrower date range.
- The above example defines two user variables - xstart and xend - to delimit the "x" date range for the plot. Later, we reuse the variables to specify the date range for the tic marks on the "x" axis.
Custom line styles
set style line 1 lt 1 lc rgb "#FFCC66" set style line 2 lt 2 lc rgb "#FF00FF" pt 6 set style line 3 lc rgb "#FFFFFF"
- The lines and point symbols in a plot have default properties such as color, and shape. The above commands define custom styles which can be assigned to a plot component to override the default property values.
- Line styles 1, 2, and 3 are created.
- As defined, line styles 1 and 2 are based on the default line types (lt) of the same number. A new line color(lc) is specified for the two line styles.
- For line style 2, the point type (pt) is also changed. The point type defines the shape of the point symbol: type 6 is a small circle.
To view the default line type properties, executing the following gnuplot commands, and examine the output png:
set terminal png set output 'test.png' test set output
Axis tic marks
set xtics textcolor linestyle 3 rotate set ytics textcolor linestyle 1 set y2tics textcolor linestyle 2 set xtics xstart, 7776000, xend set bmargin 8 set tics nomirror
- The tic marks on the "x" axis (xtics) adopt the text color of line style 3 defined earlier.
Similarly, the tic marks for the "y" and "y2" axes adopt the text color of line styles 1 and 2, respectively.
Note that "y2" is the vertical axis on the right hand side.
The color change is necessary because the default color for tic marks assumes a white background. Given that the background was modified to a dark color, the tic marks blend into the background, making them invisible without the change.
- By default, all axes are auto-scaled. The script specifies explicitly the scale for the "x" axis in the form of a triplet: xstart, 7776000, xend. The two user-defined variables, xstart and xend, contain the min and the max tic mark values on the "x" axis, respectively. The middle number is the increment in seconds (7776000 seconds equal 90 days).
- Normally, the labels on the "x" axis are written horizontally. Using the horizontal orientation results in labels overlapping each other. To solve this problem, the script rotates the "x" axis labels by 90 degrees, hence rendering them vertically.
A side effect is that the bottom margin (bmargin) needs to be increased to compensate for the longer "x" axis labels.
- By default, all tic marks are mirrored on the opposite border (left "y" axis to right and vice versa, and bottom "x" axis to top and vice versa). The set tics nomirror command configures tic marks to not mirror.
set xlabel "Date" tc ls 3 offset 0,-3 set ylabel "Count" tc ls 3 set y2label "Count" tc ls 3
- The axis labels are assigned the text color(tc) of line style (ls) 3. Note that you can abbreviate the names of common gnuplot objects: ls for linestyle.
- The color change is necessary to make the labels visible in a dark background.
- The "x" label is relocated downward by the offset to compensate for the vertical labels.
set key title "Legend" set key font "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf,16" set key textcolor ls 3 set key box ls 3 lw 2 height 2 spacing 3 set key top left set key Left
- The set key box command specifies that the legend is to be enclosed in a box.
The box is assigned line style 3. The line width(lw), the height, and the spacing are customized to make it look pretty.
- By default, the legend is placed at the top right corner inside the plot. The set key top left command relocates the legend to the top left.
- The Left keyword (with the capital L) left justifies the text in the legend body.
set title "Web Site Stats" textcolor ls 3 set grid set border linewidth 1 linestyle 3
- A title for the plot is specified, and assigned the text color of line style 3.
- Grid lines are enabled.
- Borders are enabled, and assigned a line width and style.
plot "report.dat" every ::1 using 1:2 \ title "#PageViews" axes x1y1 ls 1, \ '' every ::1 using 1:3 \ title "#Clicks" axes x1y2 ls 2
With the set-up complete, we are ready to issue the plot command.
- Two plots are defined, separated by a comma on the command line.
- Both plots are taken from the same input data file: report.dat.
As a shorthand, enter two quotes for the second plot instead of repeating the filename.
- every ::1 skips the first line in the input file which is the heading line. Instead, we specify explicitly the titles on the plot command line.
- The source of the variables "x" and "y" are specified by the two numbers in using x:y. The variable "x" in both plots is taken from the first field in the input file (the "1" in 1:2 and 1:3). The variable "y" is taken from field 2, and field 3 respectively.
- The 2 plots share the same x1 axis (the bottom axis). The number of page views are placed on the y1 (left) axis while the clicks are placed on the y2 (right) axis.
- The 2 plots are assigned the line styles (ls) 1 and 2 respectively.
Part 3 of the series illustrates how to plot a bar chart using gnuplot.