Vector
Main

Temp

This How-To is about making 2-D Cartesian graphs as they are usually found in math textbooks. In other words, the aim is to produce a presentation-quality graphic.

The key word is presentation. Much of what is here is devoted to making the result pretty. For most purposes many of the settings used here are unnecessary.

Know what you want

This How-To assumes you want to plot several basic functions. The functions used are {$ x^2,\ \sin x,\ e^x,\ \cos x,\ \log x.$} In their FORTRAN-like form these are: x**2, sin(x), exp(x), cos(x), log(x) (log means the base-e logarithm unless another base is explicit).

If this plot is done in gnuplot with the defaults (as it is when it is first opened) plot x**2,sin(x),exp(x),cos(x),log(x) the exponential functions triggers autoscaling so that all the other functions shrink to invisibility.

This plot is not wrong, and it does make a valid point about the exponential function. The defaults and autoscaling are very good for exploring unknown functions, plots from data points, and overviews. Many of the plotting windows are mouseable, so parts of the plot can be zoomed, and the values read. If the functions were really new to us, we might explore them further with set logscale y exp(1). For an education in logscale, set samples 1000 and set samples 10000 may be useful.

With these simple functions it is easy to see we probably want to show the behavior of the functions around the origin. The point here is that the choice is not always obvious, and the many settings required for making a presentation graphic are necessary because of the strength and versatility of gnuplot.

Set X and Y ranges

Eventually, I want the scales of the axes to be the same, and we want the x-axis to go somewhat more than -pi to pi to illustrate that the trigonometric functions are periodic. So, -5 to 5 seems like a good guess for the ranges. This will not bring about a square graph with equal scales yet.

set xrange [-5:5]   
set yrange [-5:5]

Notice that there is colon between the values in the range. The word to will also work, but a comma will not.

What we are doing here is building up a script that will eventually produce the graph we want, making adjustments to the script as necessary. It is useful to have an editor open and copy snippets to the script in the editor as we go. Use reset session and copy the developing script to the gnuplot command line from time to time to see how the graph develops. The last line of the script is a plot command, and it has to have a linefeed at the end to draw the plot. If you do not manage to copy that linefeed from your editor, you can simply press Enter at the end of the command line. This is a sample of what your script might look like so far.

reset session
set xrange [-5:5]   
set yrange [-5:5]
plot x**2,sin(x),exp(x),cos(x),log(x)

Some lines may be refined as we go along. Further examples of the whole script will not be offered until the end.

Unset borders and remove tics

To remove the borders and their tics:

unset border
unset ytics
unset xtics

Create axes on the zeros

To draw the axes, this will work:

set xzeroaxis ls 8
set yzeroaxis ls 8

This really just draws the lines; gnuplot assumes the axes are where the zero lines are, whether you draw them or not. ls stands for linestyle and the 8 is the number of the linestyle. Since we have not defined any linestyles, this falls through to the linetype of the same number. Linestyles are generally use for temporary styles used with a particular plot. Linetypes have defaults, which can be altered for a whole session.

You can view the linetypes specifications with show linetypes, but unless you have set some linestyles, show linestyles will show nothing.

Instead, if you please, do not set the zeroaxes, but write arrows instead. This is the easiest way to get arrowheads on the axes, but you need to know your x and y ranges, and change the arrows if you change the ranges.

set arrow from -5,0 to 5,0 ls 8 
set arrow from 0,-5 to 0,5 ls 8

The arrowheads go on the point after the to, so if you ever want an arrow in the opposite direction, just exchange the starting and ending points. Notice that this puts the arrowhead at the end of the range, which is what we want, but if the range is changed, the arrow needs to be changed too.

Using both zeroaxis and arrow will do no harm.

Place tics on the axes

set xtics axis -5,1,5
set ytics axis -5,1,5

The axis means these tics go on the axes, not on the boarders. The numbers are starting tic, increment, and end tic. The 0 tics are not very useful. The way to get rid of them is a little counter-intuitive. The way is to add empty tics at the 0s.

We did not really want the labels for the 5 and -5 tics, but we entered them because we do want the minor tics up to them. So we can also remove the 5 and -5 labels from the tics.

set xtics add ("" 0, ""-5, "" 5)
set ytics add ("" 0, ""-5, "" 5)

Adding tics like this removes the labels, but does not remove the tics.

Possibly you would prefer:

set xtics axis -2*pi,pi/2,2*pi
set xtics add ("" 0)
set xtics add ("{/Symbol p}" pi, "-{/Symbol p}" -pi) 
set xtics add ("-{/Symbol p}/2" -pi/2, "{/Symbol p}/2" pi/2 )
set xtics add ("-3{/Symbol p}/2" -pi*3/2, "3{/Symbol p}/2" pi*3/2 )
set mxtics 4

If you use this, be careful not to set mxtics again in the next step. The last setting in a script wins. If you cannot decide what you want, you can temporarily disable lines in your scripts by placing a # (hash) as the first character in the line.

Add minor tics

set mxtics 10
set mytics 10

The number is the number of intervals between major tics, not the number of minor tics. The number of tics is one fewer than the number of intervals.

Turn on grid if you wish

set grid xtics ytics mxtics mytics back ls -1 lc rgb "#00FF99", \
 ls -1 lc rgb "#99FFCC"

Default grid lines are determined by the tics. In this case, the major tics and the minor tics are both entered explicitly so the major and minor grid lines can be given different colors. If only one linestyle is given, both major and minor grid lines get the same color. I have added the linecolor (lc) to the linestyle (ls) -1, which falls through to the hidden linetype -1.

Incidentally, this is an example of how to break lines. The backslash must be the last thing on the line before the linefeed (which is to say, must not be followed by a space), and the line resumes with the first non-space character on the next line. If space is wanted at a line break, it must be entered before the backslash. This is a common way of breaking lines in many programming languates and Unix-like shells.

Label the axes

Do not label the axes using xlabel and ylabel. I mention this first because it seems to be a common error.

# Wrong, wrong, wrong!
set xlabel "Y"
set ylabel "X" norotate

Yes, it looks like this is backwards, which ought to be a clue that something is wrong. Recall that in its native form a gnuplot has x values running along the bottom border. So it makes some sense to label these according to what the x values represent, such as "furlongs/fortnights," and this label would be centered below the bottom border. But with the centered y axis, the xlabel is directly under the y axis.

Likewise, ylabel was intend for a legend that ran up the y axis. As such it is normally rotated 90 degrees, so reads sideways and bottom to top. But we want it to be upright so we add norotate.

The problem with this quick and dirty procedure is that ylabel and xlabel are glued to the middle of left side of the plot and the center of the bottom of the page. If the axes are not centered on the page (that is, if 0 is not the center of both ranges) the labels will not will not follow the axes.

(There are also labels for the top boarder and the right border.)

Instead,

set label 1 "Y" at 0.05,4.9
set label 2 "X" at 4.8,-0.1

This places the labels near the positive ends of the axes. These lines need modification for different ranges, and perhaps slight adjustments for various fonts and font sizes.

In a prettier form, using enhanced text, with the origin label:

set label 1 "{/=14{/:Bold{/:Italic y}}}" at 0.1,4.7 
set label 2 "{/=14{/:Bold{/:Italic x}}}" at 4.6,-.4  
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.4

Again, the positioning of the labels may need adjustment according to font and terminal. The co-ordinates of the label are in the co-ordinate system of the graph and are the upper left corner of the text. There is small amount of space around most characters in most fonts, so this is not exactly where the "ink" hits the "paper."

Add title if you wish

set title "Some Simple Functions"

Adjust key

The key can be turned off with unset key or set key off, and it can also be moved around. But it is in a pretty good place. The annoying thing is the FORTRAN-like notation. We want a more mathematical notation. This is fixed by adding titles to the plot command.

plot x**2 title "y=x^2", \
 sin(x) title "y=sin x" , \
 exp(x) title "y=e^x", \
 cos(x) title "y=cos x", \
 log(x) title "y=log_e x"

This is also a good place to adjust the styles of the lines. The example below illustrates some ways of making these adjustments.

plot x**2 title "y=x^2" lw 2, \
 sin(x) title "y=sin x" lc rgb "orchid3", \
 exp(x) title "y=e^x" lc rgb "web-green" , \
 cos(x) title "y=cos x" ls 0 lc rgb "#FF0000", \
 log(x) title "y=log_e x"  lc rgb "dark-violet" dt 2

lw is linewidth, dt is dashtype, lc is linecolor, rgb indicates the color coding (gnuplot has other ways to specify colors). Use show colors to see all the color names, which are not always the same as names used by CSS for web page styling. ls is linestyle.

Without these modifications, the linestyles will cycle through 1 to 8 and fall through to linetypes of the same number unless the linestyle has been set.

Summary

reset session
set xrange [-5:5]   
set yrange [-5:5]
unset border
unset ytics
unset xtics
set arrow from -5,0 to 5,0 ls 8 
set arrow from 0,-5 to 0,5 ls 8
set xtics axis -5,1,5
set ytics axis -5,1,5
set xtics add ("" 0, ""-5, "" 5)
set ytics add ("" 0, ""-5, "" 5)
#set xtics axis -2*pi,pi/2,2*pi
#set xtics add ("" 0, ""-5, "" 5)
#set xtics add ("{/Symbol p}" pi, "-{/Symbol p}" -pi) 
#set xtics add ("-{/Symbol p}/2" -pi/2, "{/Symbol p}/2" pi/2 )
#set xtics add ("-3{/Symbol p}/2" -pi*3/2, "3{/Symbol p}/2" pi*3/2 )
#set mxtics 4
set mxtics 10
set mytics 10
set grid xtics ytics mxtics mytics back ls -1 lc rgb "#00FF99", \
 ls -1 lc rgb "#99FFCC"
set label 1 "{/=14{/:Bold{/:Italic y}}}" at 0.1,4.7 
set label 2 "{/=14{/:Bold{/:Italic x}}}" at 4.6,-.4  
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.4
set title "Some Simple Functions"
#plot x**2,sin(x),exp(x),cos(x),log(x)
plot x**2 title "y=x^2" lw 2, \
 sin(x) title "y=sin x" lc rgb "orchid3", \
 exp(x) title "y=e^x" lc rgb "web-green" , \
 cos(x) title "y=cos x" ls 0 lc rgb "#FF0000", \
 log(x) title "y=log_e x"  lc rgb "dark-violet" dt 2

Production

reset session
set term svg size 640,640 dynamic enhanced background rgb "#FFFFFF"
set output "simple_functions.svg"
set xrange [-5:5]   
set yrange [-5:5]
unset border
unset ytics
unset xtics
set arrow from -5,0 to 5,0 ls 8 
set arrow from 0,-5 to 0,5 ls 8
set xtics axis -5,1,5
set ytics axis -5,1,5
set xtics add ("" 0, ""-5, "" 5)
set ytics add ("" 0, ""-5, "" 5)
#set xtics axis -2*pi,pi/2,2*pi
#set xtics add ("" 0, ""-5, "" 5)
#set xtics add ("{/Symbol p}" pi, "-{/Symbol p}" -pi) 
#set xtics add ("-{/Symbol p}/2" -pi/2, "{/Symbol p}/2" pi/2 )
#set xtics add ("-3{/Symbol p}/2" -pi*3/2, "3{/Symbol p}/2" pi*3/2 )
#set mxtics 4
set mxtics 10
set mytics 10
set grid xtics ytics mxtics mytics back ls -1 lc rgb "#00FF99", \
 ls -1 lc rgb "#99FFCC"
set label 1 "{/=14{/:Bold{/:Italic y}}}" at 0.2,4.8 
set label 2 "{/=14{/:Bold{/:Italic x}}}" at 4.6,-.2 
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.2
set title "Some Simple Functions"
#plot x**2,sin(x),exp(x),cos(x),log(x)
plot x**2 title "y=x^2" lw 2, \
 sin(x) title "y=sin x" lc rgb "orchid3", \
 exp(x) title "y=e^x" lc rgb "web-green" , \
 cos(x) title "y=cos x" ls 0 lc rgb "#FF0000", \
 log(x) title "y=log_e x"  lc rgb "dark-violet" dt 2
unset output

Result

""
Big

Sources:

Recommended:

Categories: GnuplotHowTos


edit

This is a student's notebook. I am not responsible if you copy it for homework, and it turns out to be wrong.

Backlinks

This page is Temp