Matlab → Basics → Variables → Graphs

Graphs are very useful tool for presenting information as well as very important in analyzing the data. This is particularly true in Business and Science. In this chapter we will learn how to plot two and three dimensional graphs in Matlab.

**The plot Command**

Two axes are needed to plot a two-dimensional graph, the x-axis and the y-axis. In general, the x -axis is a horizontal line and the y-axis is a vertical line. In Matlab, the command to plot a two-dimensional graph is

`plot(x,y)`

Here x and y are two vectors, and the two vectors must have the same number of element.

Now, consider sales of a particular item from 2010 to 2018, i.e. 100 items were sold in 2010, 90 in 2011, 120 in the year of 2012, and so on. The graph of this sale can be easily plotted in Matlab using the command below. Note that both vectors x and y have the same number of elements.

__Command Window__

`>> x=[2010 2011 2012 2013 2014 2015 2016 2017 2018];`

>> y=[100 90 120 130 150 155 140 130 110];

>> plot(x,y)

The `plot`

command has option to amend the line specifications. The simplest command for setting the line specification is

`plot(x,y,'line specifications')`

For example, the command `plot(x,y,'--r*')`

plot the dashed line of red color and the points of intersection are marked with *. The Matlab command is

`>> x=[2010 2011 2012 2013 2014 2015 2016 2017 2018];`

>> y=[100 90 120 130 150 155 140 130 110];

>> plot(x,y,'--r*')

**Line Style, Color, and Marker**

In the above example,

- We used
`--`

for dashed line. Replace`--`

by`:`

and`-.`

For dotted and dash-dotted line respectively. - We used
`r`

for the red color. Replace this`r`

by`b, c, g, k, m`

, and`y`

for blue, cyan, green, black, magenta, and yellow color line respectively. - We used
`*`

for marker. Replace the marker`*`

by`+, o, x, ^`

, and`v`

.

For example,