# Plot Formatting

### Summary

• Familiarize yourself with the various plot entities.
• Control axis limits and change axis ticks on your plot.
• Add a plot legend and grid.
• Format line and markers on a plot.
• Save your plot to a file.

Most of the time, MATLAB plots need to be formatted for professional presentation and clear communication. We covered the basics of plotting in MATLAB in the previous lesson (LINK TO LESSON). In this lesson, we will review how to format a default plot into something that is more useful and presentable.

### Plot Nomenclature

Figure 1 shows MATLAB’s naming convention for plotting. Most of the nomenclature is common sense and similar to other software, but it is important to know in order to understand how to change the different properties. Figure 1: A MATLAB plot with common plot properties annotated.

In this lesson, we will cover the two most commonly used groups of properties that define how plotted graphics look in MATLAB. Those are as follows:

• Line Properties define chart line appearance and behavior. For example, the line style and thickness.
• Axis Properties define axes appearance and behavior. For example, axis limits, title, and legend.

The properties are not mandatory as you could see from the last lesson. In other words, you could make a plot without MATLAB requiring you to see a title, line width, axis labels, etc.

### Figure Title and Axis Labels (Axis Properties)

One of the most common additions to a plot is adding a plot title (title()) and axis labels (xlabel(), ylabel()). The plot title should summarize what the data presented on the plot is, while the axis labels explain what variable changes across the range of the axis. Example 1 shows how to implement a plot title and axis labels. Note that xlabel() always corresponds to the horizontal (x) axis, while the ylabel() always corresponds to the vertical (y) axis.

Example 1: Plot Title and Axis Labels
Editor
x = [0 1 2 3 4 5];  %Defining the domain of the data to plot
y = x.^2;           %Calculating the value of the function (x^2) for each data point in x

figure      %Creating a blank figure
plot(x, y)  %Plotting the function across the specified domain

xlabel('Independent Variable, x')  %Adding label for the horizontal x-axis
ylabel('Dependent Variable, y')    %Adding label for the vertical y-axis

%Figure 2 shows the output for this code.

[Try this code yourself with Octave Online! Click Here] Figure 2: Output from Example 1 showing plot title and axis labels on a MATLAB plot.

### Axis Limits and Tick Labels (Axis Properties)

MATLAB makes it easy to adjust the limits of your axes to fit your data. The xlim() function adjusts the displayed domain of the horizontal axis, while the ylim() function adjusts the displayed range of the vertical axis.

For some data, such as the sinusoidal wave shown in Example 3, it can be useful to change the tick increments. xticks() and yticks() will redefine the tick increment. That is, how far apart the ticks are on the axis. The corresponding tick labels can be changed with the xticklabels() and yticklabels() functions. These will allow you to change the tick label to any custom text compatible with MATLAB.

Example 2: Adjusting Axis Limits and Ticks
Editor
time    = [0 1 2 3 4 5];  %Define the domain/range of the data to plot
voltage = 0.3*sin(time);  %Calculating the square for the data range

figure               %Creating a blank figure
plot(time, voltage)  %Plotting the function across the specified domain

xlim([0 2*pi]);                                      %Setting the axis limits
xticks([0, pi/2, pi, 3*pi/2, 2*pi]);                 %Setting the x tick values (position of ticks)
xticklabels({'0','\pi/2','\pi', '3\pi/2', '2\pi'});  %Setting custom x tick labels (displayed below
%	 each x tick)

%Note: the value \pi will result in the greek symbol pi
%Figure 3 shows the output for this code.

[Try this code yourself with Octave Online! Click Here] Figure 3: Plot with custom axis limits and axis labels on the horizontal axis (output from Example 1).

You can see the greek letter “pi” in Example 1 and the resulting Figure 3. Review the documentation for a full list of Greek Letters and Special Characters in Graph Text.

### Plot Legend and Grid (Axis Properties)

It is important to provide the reader of your plot with clear representation of the data you are trying to communicate. When presenting multiple data sets/lines/markers on the same plot, things can get a little confusing as to what dataset is presented where. That is where a plot legend can come into use. The legend() function describes the various plotted data sets by setting a plot legend. You can use properties in the legend to move it to the best location on the plot (not covering data) as seen in Example 3 and Figure 4 below.

Important Note: Legend entries must be provided in the order that they were plotted in the code.

Interpreting a plot with data that is scattered all over the plot can be made a little easier by placing a grid onto the plot. The grid command imposes a faint grid over the plot that can make interpreting the plot easier. See the documentation on Axis Properties for a complete list.

Example 3: Adding a Plot Legend and a Plot Grid
Editor
x    = linspace(0, 2*pi, 80);  %Define the domain of the data to plot
ySin = sin(x);                 %Calculating the sine for the domain
yCos = cos(x);                 %Calculating the cosine for the domain

figure                    %Creating a blank figure
plot(x, ySin, '-')        %Plotting first function with a solid line
hold on                   %Telling MATLAB to place new plots on the same figure.
plot(x, yCos, '--')       %Plotting second function with a dashed line

legend('Function: sin(x)','Function: cos(x)','location','North')  %Enabling the plot legend
grid on                                                           %Show a grid on the plot

%Note:  MATLAB will automatically pick unique colors if not specifically defined.
%Figure 4 shows the output for this code.

[Try this code yourself with Octave Online! Click Here] Figure 4: Plot legend and plot grid from Example 3.

### Line and Marker Formatting (Line Properties)

Two of the most common ways to display data on a plot are by representing the data as a continuous line or discrete markers. A line can be changed in color, thickness, or continuity (dotted, dashed, etc.), while markers can be changed in outline color, fill color, size, and shape.

Changing the properties of the plotted data typically follows the trend shown in Figure 5. Other than the first three items (dependent variable values, independent variable values, and LineSpec), these properties do not have to come in some specific order and can be added or removed anytime. Figure 5: plot() function with adjusted marker properties.

Figure 5 shows identifies common marker properties; however, the same concept is applied for a line and its line properties. The most commonly changed line property is 'LineWidth', while the most commonly changed marker properties are 'MarkerSize', 'MarkerEdgeColor', and 'MarkerFaceColor'. See the documentation on Line Properties for a complete list.

Example 4 shows three different datasets presented with continuous lines and discrete markers. The example applies some of the more common property changes.

Example 4: Line and Marker Formatting
Editor
x = linspace(0, 2*pi, 10);  %Define the domain of the data to plot
yFunction1 = sin(x);        %Calculating the sine function for the domain
yFunction2 = cos(x);        %Calculating the cosine function for the domain
yFunction3 = 0.1*x;         %Calculating the linear function for the domain

figure  %Creating a blank figure

%Plotting a black colored (k) dashed style (--) line with a line thickness of 1.5
plot(x, yFunction2, '--k', 'LineWidth', 1.5)
hold on   %Telling MATLAB to place new plots on the same figure.

%Plotting the data with round (o) blue (b) markers with magenta (m) colored fill
plot(x, yFunction3, 'ob', 'MarkerSize', 7, 'MarkerFaceColor', 'm')

%Plotting the data with a line-marker hybrid
plot(x, yFunction1, '-r', 'Marker', 's', 'MarkerFaceColor', 'k')

grid on  %Show a grid on the plot
legend('cos(x)','0.1*x','sin(x)','location','SouthWest')  %Note the order of the entries!!
%Figure 6 shows the output for this code.

[Try this code yourself with Octave Online! Click Here] Figure 6: Demonstrates different types of line and marker formats from Example 4.

### Saving a Figure

Once your figure/plot is nicely formatted, you can save it by simply selecting File > Save As… as shown in Figure 7. This will allow you to save your figure as a .jpg, .png, .pdf, and more! In case you would like to have the ability to edit the figure, you can also save the figure as a MATLAB figure file, .fig. Figure 7: Saving a MATLAB figure.

### Next Lesson

In the next lesson, we will cover more advanced (and cooler!) plots and plotting methods.