Line Styles, Markers and Colours

3.4.1 Introducing Line Styles, Markers and Colours
When creating plots, you may want to customise their appearance. You can do that by adding a parameter to the plot command. This was shown earlier in Figure 3.1 and in Figure 3.2. The general command would be:

plot(x,y,'colour_style_marker')

 

The colour style marker can be used to describe a colour, a line style and a marker type. Some of the arguments that can be used for each category are below:

Colour strings: ‘c’, ‘m’, ‘y’, ‘r’, ‘g’, ‘b’, ‘w’ and ‘k’. These correspond to cyan, magenta, yellow, red, green, blue, white and black.

Line style strings: ‘-‘ for solid lines, ‘–’ for dashed lines, ‘:’ for dotted, ‘-.’ dash-dot, and ‘none’ for no line.

Marker types: ‘+’, ‘o’, ‘*’ and ‘x’ and the filled marker types ‘s’ for square, ‘d’ for diamond, ‘^’ for up triangle, ‘v’ for down triangle, ‘>’ for right triangle, ‘<’ for left triangle, ‘p’ for pentagram, ‘h’ for hexagram, and none for no marker.
 

Example 3.1
To plot black squares at each data point without a line connecting them in between, use the command:

plot(x,y,'ks')

 

Example 3.2
To plot a red dotted line with plus sign markers at each data point, you’d use the command:

plot(x,y,'r:+')

 

3.4.2 Using Line Styles, Markers and Colours
Say you wanted to compare the accuracy of the same function with different spacing (and hence a different number of points). The code below (that output Figure 3.5) is a good example of this:

figure
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
Figure 3.5

Figure 3.5

 

Note: the command “figure” is used to open up a new figure window so that any following commands do not affect previous figures. The new figure window will then become the “current figure” in MATLAB, which you can make additions to with relative ease through the command window.

If you want to open up a previous figure, use the command ‘figure(n)’, where n is the figure’s number. You could also manually click within the window of the figure that you would like to be your current figure, but assuming that you do not switch figures, the last opened figure is by default your current figure.

You could even use the ‘subplot’ command to show multiple plots in the same figure window (to output multiple graphs into one picture), but more on that next!