Vector
Main

How to Make a 2-D Cartesian Graph

This How-To is about making 2-D Cartesian graphs as they are usually found in math textbooks.

Know what you want

This How-To assumes you want to plot several basic function.

plot x**2,sin(x),exp(x),cos(x),log(x)

If you enter this plot with the defaults, the exponential function will cause autoscaling to shrink the other functions to invisibility. Such a plot makes a valid point about the exponential function, but that may not be what you are interested in showing. You either have to know what the functions do, or you have to play around with the plot to find out what you consider interesting about it.

Set the right terminal type

The default terminal is likely to be wxt which is good for investigating functions and plotting from data, but it not well behaved (as of gunplot 5.0) on Windows operating systems. The idea of this How-To is to make pretty pictures of mathematical functions. So, if you are on Windows, use the windows terminal until you are ready to switch to an output terminal.

set term wxt size 640,640 enhanced background rgb "#A0A0A0"

Terminals that support enhanced text generally have enhanced as the default, but it does not hurt to put it in.

Set X and Y ranges

This is something that can make the graph look bizarre if you choose the wrong values. These values may be useful for some functions that are defined around the origin, but in other cases, other values may be necessary to display various aspects of other function.

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.

These ranges are probably something like what you want to see when you plot these functions. The log functions fades out close to zero because the sampling rate (resolution) is not fine enough. This can be adjusted, but for present purposes, I will not go into that.

Create axes on the zeros

set xzeroaxis ls 8
set yzeroaxis ls 8

This sets the x and y axes through the origin. 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 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

Doing both will do no harm.

Unset border and remove tics

To remove the borders and their tics:

unset border
unset ytics
unset xtics

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 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.

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.

Turn on grid if you wish

set grid mxtics mytics back lc rgb "#FFFFFF"

Default grid lines are determined by the tics.

Use the same scale on both axes

You will not need this if you used the same range for x and y (as in the above).

set view equal xy

This for 2-D plots. In 3-D plots it is set view equal xyz which is no surprise.

Another way to do this in 2-D is

set size ratio -1

As might be suspected, this last is a special case of a more general command.

Label the axes

If the origin of your graph is at the center, there is a sneaky way to do this, but it is tricky.

set xlabel "Y"
set ylabel "X" norotate

Yes, it looks like this is backwards. 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.

Instead,

set label "Y" at 0.05,4.9
set label "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.

Add title if you wish

set title "Some Simple Functions"

Clean up the origin

The main point of explicitly naming the tics is eliminate the ugly clash of the 0 ticks at the origin.

set xtics (-4,-3,-2,-1,1,2,3,4)
set ytics (-4,-3,-2,-1,1,2,3,4)
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.25

The label for the origin is given the tag 3, because it probably will need some adjustment in various terminals. Using the tag allows you to edit it repeatedly without having to unset it. If you just keep entering label commands, you will not erase failed ones, and you will keep adding new ones.

This is also the first example of enhanced text here.

  • Enhanced text uses curly braces {}. It may be difficult to tell this in some tiny help text fonts.
  • There must be a forward slash immediately after the opening brace.
  • There must be a set of braces for each attribute.
  • The = means and absolute point size, * means a multiple of the default point size.

So we can improve the labels on the axes.

set label 1 "{/=14{/:Bold{/:Italic y}}}" at 0.1,4.9 
set label 2 "{/=14{/:Bold{/:Italic x}}}" at 4.8,-.2  
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.25

Improved Lines

No everyone is in love with the default line colors and styles. There are two ways to change them. The linetypes are the defaults, and they can be changed or augmented, but this is usually done by loading a configuration file at the beginning of a session.

Once the linetypes are defined (which is really from the beginning because there are default linetypes, you should use linestyles. When a linestyle is not defined, it falls through to the linetype of the same number. By default, the numbers of the linetypes and linestyles cycle so for any positive integer there is a linetype associated with it, and for any positive integer, the linestyle will fall through to a linestyle.'

The purpose of this arrangement is that you can set a linestyle and experiment with it, but you can unset it and it will fall through the corresponding linetype.

Rather than mess with the default linetypes, we will just define some more.

set linetype 9 lc "blue" dt solid
set linetype 10 lc "red" dt solid
set linetype 11 lc "dark-green" dt solid
set linetype 12 lc "brown" dt solid
set linetype 13 lc "green" dt solid
set linetype 14 lc "magenta" dt solid

lc is shorthand for linecolor. It can be any of the color names you can see with show colornames or an RGB color number such as "#00FF00" (there are other ways to specify colors). dt is short for dashtype which is solid for solid lines. Parts of linestyles can be modified by using the linestyle followed by the modification such as ls 9 lw 2 make linestyle 9 with a linewidth of 2.

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" ls 9, \
 sin(x) title "y=sin x" ls 10, \
 exp(x) title "y=e^x" ls 11, \
 cos(x) title "y=cos x" ls 12, \
 log(x) title "y=log_e x" ls 13

You probably do not want to explicitly indicate that the logarithm is base e very often, but it is included here to show how subscripts are written in titles.

Also illustrated here is how to break long command lines. The rules are: the backslash must be the last thing on a line (no space after it), and the line resumes with the first non-space character on the following line. This implies that if you want a space, you must include it before the backslash. The spaces are not actually need before the backslashes here because the commas are sufficient to separate the expressions.

The problems with breaking lines like this is that when lines are entered this way, the lines are difficult to edit, and scrolling back in the command line does not work as expected. (Yes, you can do these things; they are just more difficult.)

Now, to get really fancy, the variables can be set in italic:

plot x**2 title "{/:Italic y} = {/:Italic x}^2" ls 9 lw 2, \
 sin(x) title "{/:Italic y} = sin {/:Italic x}" ls 10, \
 exp(x) title "{/:Italic y} = e^{/:Italic x}" ls 11, \
 cos(x) title "{/:Italic y} = cos {/:Italic x}" ls 12, \
 log(x) title "{/:Italic y} = log_e {/:Italic x}" ls 13

Summary

set term windows size 640,640 enhanced background rgb "#E0E0E0"
unset border
unset ytics
unset xtics
set xzeroaxis ls 8
set yzeroaxis ls 8
set arrow from -5,0 to 5,0 ls 8 
set arrow from 0,-5 to 0,5 ls 8  
set xtics axis
set ytics axis
set xrange [-5:5]   
set yrange [-5:5]
set view equal xy
set title "Some Simple Functions"
set grid
set xtics (-4,-3,-2,-1,1,2,3,4)
set ytics (-4,-3,-2,-1,1,2,3,4)
set label 1 "{/=10{/:Bold{/:Italic y}}}" at 0.1,5
set label 2 "{/=10{/:Bold{/:Italic x}}}" at 5,-.1 
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.25
plot x**2 title "{/:Italic y} = {/:Italic x}^2" ls 9 lw 2, \
 sin(x) title "{/:Italic y} = sin {/:Italic x}" ls 10, \
 exp(x) title "{/:Italic y} = e^{/:Italic x}" ls 11, \
 cos(x) title "{/:Italic y} = cos {/:Italic x}" ls 12, \
 log(x) title "{/:Italic y} = log_e {/:Italic x}" ls 13

Production

set term svg size 640,640 dynamic enhanced background rgb "#E0E0E0"
set output "simple_functions.svg"
unset border
unset ytics
unset xtics
set xzeroaxis ls 8
set yzeroaxis ls 8
set arrow from -5,0 to 5,0 ls 8 
set arrow from 0,-5 to 0,5 ls 8  
set xtics axis
set ytics axis
set xrange [-5:5]   
set yrange [-5:5]
set view equal xy
set title "Some Simple Functions"
set grid
set xtics (-4,-3,-2,-1,1,2,3,4)
set ytics (-4,-3,-2,-1,1,2,3,4)
set label 1 "{/=10{/:Bold{/:Italic y}}}" at 0.1,5
set label 2 "{/=10{/:Bold{/:Italic x}}}" at 5,-.1 
set label 3 "{/=12{/:Bold{/:Italic O}}}" at 0.05,-0.25
plot x**2 title "{/:Italic y} = {/:Italic x}^2" ls 9 lw 2, \
 sin(x) title "{/:Italic y} = sin {/:Italic x}" ls 10, \
 exp(x) title "{/:Italic y} = e^{/:Italic x}" ls 11, \
 cos(x) title "{/:Italic y} = cos {/:Italic x}" ls 12, \
 log(x) title "{/:Italic y} = log_e {/:Italic x}" ls 13
unset output

π


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 GnuplotHowToCartesian2DGraph