# 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[edit | edit source]

For the first MATLAB plot, we are going to plot the classical linear equation `y = mx +c`

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 for x range from 0 to 30

In MATLAB, we type in the following:

```
x = 0:30; %declare x = 0 to 30
y=(5*x) +8 ; % declare equation
plot(x,y) %plot the line chart
```

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[edit | edit source]

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[edit | edit source]

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

For the customization, we use as followed conventions

**xlabel** or **ylabel**(text to display, Properties Name 1,Value Properties Name 1, ..., Properties Name n,Value Properties Name n)

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

```
xlabel('Population of dogs','Color','b','FontWeight','bold','FontAngle','italic')
% x label to display "Population of dogs" with blue colour fonts, bold and italic
ylabel({'Weeks','for year 2022'},'FontSize',15,'FontName','Times New Roman')
% y label to display 2 lines of "Weeks for year of 2022" with font size 15 and using font 'Times New Roman'
```

### Grid[edit | edit source]

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

```
grid on
%turn on grid
grid minor
% turn on the minor gridlines / subset of the major gridlines
```

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[edit | edit source]

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

```
legend('y=(5*x) +8','Location','northeast')
```

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[edit | edit source]

You can add a marker in the plot chart inside the **plot** command .

```
plot(x,y,marker_commands)
```

For the example , let's say we wanted have diamond shaped marker, hence we type in following command

```
plot(x,y,'-d') %the line - indicate the line to connect the diamond shapes
```

We would have a chart with diamond-shaped marker as shown.

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

marker_commands | Marker shapes |
---|---|

'o' | Circle sign |

'+' | Plus sign |

'*' | Asterisk |

'.' | Point |

'd' | Diamond |

'h' | Six-pointed star (hexagram) |

's' | Square |

'p' | Five-pointed star (pentagram) |

'x' | Cross |

'^' | Upward-pointing triangle |

'v' | Downward-pointing triangle |

'<' | Left-pointing triangle |

'>' | Right-pointing triangle |

### Title[edit | edit source]

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

**title**(text to display, Properties Name 1,Value Properties Name 1, ..., Properties Name n,Value Properties Name n)

As shown, we can customize the title as followed

```
title({'Populations of dogs in 2022 ','by weeks'}, 'Color','g','FontSize',14,'FontName','Lucida Console')
%display the title in two lines with green text , font size of 14 and with font Lucida COnsole
```

## Other examples[edit | edit source]

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

```
angles = linspace(0, 2*pi);
radius = 20; % radius of circle is 20
CenterX = 50; %coordinate of circle center point at x axis is 50
CenterY = 50; %coordinate of circle center point at y axis is 50
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(x, y, 'b-', 'LineWidth', 2);
title("Circle")
hold on;
plot(CenterX, CenterY, 'k+', 'LineWidth', 3, 'MarkerSize', 14);
grid on;
axis equal;
```

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.

```
>> x = [1 2 4 8];
>> y = [1 2 1.95 3.79];
>> z = [1 1.73 2.02 3.84];
>> h = plot(x,y,'--');
>> hold on; % Plot next graph on same figure
>> plot(x,z);
>> hold off;
>> xlabel('Number of processors');
>> ylabel('Speedup');
>> saveas(h,'graph.eps',eps);
```

## External Links[edit | edit source]

MATLAB category on ControlTheoryPro.com