MATLAB Programming/Plot

The plot command plots a 2D line plot of the data in Y versus the corresponding values in X.

Plotting linear lines
For the first MATLAB plot, we are going to plot the classical linear equation

In MATLAB, to do plotting we need to follow the specific workflow

(a) Specify the range of values of x to be used (b) Specify the equation of the y with x variables (c) plot the graphs



For the examples, we need to plot of an equation $$y = 5x + 8$$ for x range from 0 to 30 In MATLAB, we type in the following:

We can get the simple line chart as shown on below :

As you see on the left, we can get the of chart plot for linear lines. Congrats, you have plotted the first chart in MATLAB.

This is the basic idea of how to plot charts in MATLAB.

In next section, we are using the same charts to spruce up the charts to make it more custom made/unique

Enhance the chart plots
Below are some of the ways to enhance the chart plots (it is to make it more understandable/presentable to the readers)

Note that all of the chart plot enhancers are needed to be defined after/below the plot commands with some properties need to be defined at the plot functions.

These are few properties that can be used to enhance the plot charts namely

(a) Axis Label

(b) Grid

(c) Legend

(d) Marker

(d) Title

Axes labels
We can label the axes by using xlabel and ylabel with examples as below.

For the customization, we use as followed conventions

As shown below, we can customized the axis label .with multiple properties.

Grid
As shown on original chart shown above, it is hard to see which whether the certain line plots are references to the axes on the right.

We can use the grid to trace the certain values of x is correlating to which y values.

We can use grid functions and uses the following MATLAB commands

There is another properties that is grid minor which will plot out the smaller grid scales, that is grid minor

In order to use grid minor, you need to turn on grid by typing grid on first.

Legend
We can put legends on the chart plot by using following command: legend

For example below, we need to label the plot line as followed We can see the legends shown in northeast location which is the default locations for the graph

We can define the locations of the legends within the chart area or even outside of the chart by adding keyword outside to the locations

Marker


You can add a marker in the plot chart inside the plot command. For the example, let's say we wanted have diamond shaped marker, hence we type in following command We would have a chart with diamond-shaped marker as shown.

The following marker_commands that can be used in plotting such as follows :

Title
We can add the title to the plot chart by adding title command

Same as axis label above, the convention for title command follows the same conventions

As shown, we can customize the title as followed

Other examples
In this example, we are going to plot a simple circle.

In this example, the following code will create a graph comparing the speedup of two algorithms on increasing number of processors.

The graph created is shown below.