Labeling the axes
In this recipe, we add labels to our axes to explain what is being plotted and the significance of the tics and numerical scales. We also add an overall title that will appear at the top of the graph.
Getting ready
Make sure the supplied datafile ch2.dat
is in your current directory. It is the result of adding the first three terms in the Fourier series approximation to the square wave. It is not important to understand what that means to follow the gnuplot recipes; we are using this file because it leads to a good graph for the purpose of illustrating annotations and labeling.
How to do it…
Following is the script that produces the previous annotated graph:
set yrange [-1.5:1.5] set xrange [0:6.3] set ytics nomirror set y2tics 0,.1 set y2range [0:1.2] set style fill pattern 5 set xlabel "Time (sec.)" set ylabel "Amplitude" set y2label "Error Magnitude" set title "Fourier Approximation to Square Wave" plot 'ch2.dat' using 1:2:(sgn($2)) with filledcurves,\'' using 1:2 with lines lw 2 notitle,\'' using 1:(sgn($2)) with lines notitle,\'' using 1:(abs(sgn($2)-$2)) with lines axis x1y2
How it works…
The highlighted lines in the code are the labeling commands being introduced in this recipe. The other commands are variations of code used in the recipes in the previous chapter. The xlabel
and ylabel
commands place the specified strings near the axes and should explain what the values on the axes mean, including units. The y2label
command labels the right-hand "second" y-axis, if there is one. The set title
command creates a title at the top of the graph.
There's more…
If you have a very long title (or label), gnuplot will not break it up into lines. It will just spill over into the margins of your graph and get truncated. You need to insert the line breaks manually by using the code \n
, and make sure to surround your title string by double quotation marks. If you use single quotation marks, the \n
s will be printed literally rather than being interpreted as the escape codes for a line break. For example, you can say set title "Line One of a Very Long Title\nLine Two of the Title"
.