10 Types of MATLAB 2D Plot Explained with Examples and Code

MATLAB Programming Feature image

When I learned about Pie plot and other two dimensional plots in MATLAB (MATLAB 2D plot), first time, I was curious to know…

  • What are the other types of 2D plots we can draw in the MATLAB?
  • How to write code to draw 2D plot for all those different plot types?
  • How to make them more colorful?

I started getting more knowledge and doing work on MATLAB R2013a.

I cleared my basic concepts required for plotting different 2D plots. Now I can draw any 2D plots with the various available MATLAB functions.

Here I am sharing my experience with the help of this tutorial. You will find it very interesting plotting different 2D plots and then coloring it.

This tutorial is an extension of our previous tutorial of plotting 2D graphs in MATLAB.

Let’s dive in…

Classifications of Two Dimensional Plots in MATLAB

(MATLAB 2D plot)

MATLAB  supports more types of two-dimensional (2D) plots. We are trying to cover plots which are widely used in industry and in different projects.

Here is a list of 10 various MATLAB 2D plots.

  1. Area Plot
  2. Bar Plot
  3. Stem Plot
  4. Stairs Plot
  5. Barh Plot
  6. Pie Plot
  7. Polar Plot
  8. Compass Plot
  9. Scatter Plot
  10. Errorbar Plot

Now, I am describing each 2D plots by using the MATLAB code and its decorating functions in details.

1. Area Plot

In the Area plotting graph, you can use basic functions. It is a very easy draw.

In the MATLAB plotting, there is a function area() to plot Area.

How to draw Area plot in MATLAB?


The syntax for the Area plot…



As an example, we are plotting Area graph for trigonometric function. It is one of the types of functions in MATLAB.

% To create the area plot for the given equation Sin(t)Cos(2t). 

% Enter the value of range of variable 't'.
% Put the given equation
title('Area Plot')


  • ‘%’ is used to add the comment. The line starts with ‘%’ will get executed.
  • ‘;’ semicolon is used to suppress the entered value.

Output in MATLAB:

After the running code, you will get the Area plot as shown below.

2. Stem Plot

In Stem plot, the discrete sequence data and variables are used. This plot is created by using the stem() function.

How to create the Stem plot in MATLAB?


The syntax for the stem plot is,

stem(x, y)


Here we are creating the stem plot using the MATLAB functions for array of some random discrete numbers.

% Consisder the variable range of 'x' and 'y',
x=[3 1 6 7 10 9 11 13 15 17];
y=[14 7 23 11 8 16 9 3 23 17];
title('Stem Plot')
xlabel('X axis')
ylabel('Y axis')

In this program, I used the color function (red ‘r’) and stem plot function (stem).

To decorate your graph, you can learn more about color markers and line specifiers in MATLAB.

Output in MATLAB:

You can see the 2-dimensional stem plot in MATLAB.

3. Bar Plot

You can create different types of bar plot in MATLAB. Here, I am explaining the simple bar plot code with the help of multiple functions.

How to draw Bar plot in MATLAB?


The syntax for the bar plot is,




Let’s write a MATLAB code to plot the Bar for arrays of some random numbers.

x=[1 3 5 7 10 13 15];
y=[0 0.5 1 1.5 3 2 2];
title('Bar Plot')
xlabel('X axis')
ylabel('y axis')

Output in MATLAB:

Below is diagram as bar 2-dimensional plot.

4. Barh Plot

Barh plot is short abbreviations of Horizontal bar. Here I am using the Barh function for the horizontal plane.

How to draw a Barh Plot in MATLAB?


The same syntax uses like bar plot,



x=[1 3 5 7 10 13 15];
y=[0 0.5 1 1.5 3 2 2];
title('Barh Plot')
xlabel('X axis')
ylabel('y axis')

Output in MATLAB:

Barh plot is showing in the below diagram.

If you look at the above graph, you can see it the exact 90-degree tilt of Bar graph.

5. Errorbar Plot

Errorbar plot is again of the types of bar plot.

How to draw an Errorbar Plot in MATLAB?


The syntax for the Errorbar plot are,



x=[5 10 15 20 25 30 35 40];
y=[0.1 3 4 3.5 5 6.6 7 8];
title('Errorbar Plot')
xlabel('X axis')
ylabel('Y axis')

We are adding color function to decorate our plot.

Output in MATLAB:

See the 2-dimensional error plot.

6. Stairs Plot

This is again one of the MATLAB 2D plots that look more like stairs.

How to design the stairs plot in MATLAB?




Here, I am writing the MATLAB code of stairs plot with the black (k) color function.

x=[0 1 2 4 5 7 8];
y=[1 3 4 6 8 12 13];
title('Stairs Plot')
xlabel('X axis')
ylabel('Y axis')

Output in MATLAB:

Stairs plot is shown in the below diagram. You can design as per your MATLAB code.

It more looks like stairs. Isn’t it?

7. Pie Plot

In mathematics, the pie chart is used to indicate data in percentage (%) form.

In MATLAB, you can draw different kinds of pie plots by using simple code.

How to draw a Pie Plot in MATLAB?




To write the simple code for the pie plot.

x=[10 20 25 40 75 80 90];
title('Pie Plot')

We have 7 numbers in the array ‘x’.

When you execute the above code in MATLAB, All the values in array gets converted in the percentage.

Output in MATLAB:

If you look at the code, we have not added any color function. By default, Pie plot is colorful. So it makes plotting Pie graph even easier.

8. Polar Plot

Plotting Polar plot is a little tricky.

When you are plotting the polar plot, theta and radius must be the same size.

How to plot the Polar in MATLAB?


In Polar plot, you can write the following syntax,


Polar plot function has arguments as theta, radius, line specification.


theta=[-pi/2 pi 2*pi];
radious=[2 6 9];
title('Polar Plot')

Where ‘r’ considered as the red color.

Output in MATLAB:

The line specification and color are shown in the below diagram.  Red dashed depicts the given radius of the circle [2 6 9].

9. Compass Plot

Compass plot is the same as the polar plot. Both need equal quantities of theta and radius.

How to draw a Compass Plot in MATLAB?


The syntax of the compass plot is,



For the designing compass plot, you just need to write only ‘compass’ function.

theta=[-pi pi/2 pi];
r=[2 6 9];
title('Compass Plot')

Output in MATLAB:

10. Scatter Plot

Plotting the scatter plot in MATLAB is simple.

How to draw a Scatter Plot in MATLAB?


For the scatter plot,



x=[1 2 3 5 7 9 11 13 15];
y=[1.2 3 4 2.5 3 5.5 4 6 7];
title('Scatter Plot')
xlabel('X axis')
ylabel('Y axis')

Output in MATLAB:

After the running MATLAB code, you will see this…

There are so many 2D plots you can draw in MATLAB. I described briefly the most useful top 10 plots.

In this tutorial, I have covered different types of plots by using examples and different functions [color |marker style| line spec]. you can read the MATLAB 3D plot with their 5 different classifications.

If you have any query for MATLAB 2D plot, please comment below. I will reply to you as soon as possible.

Stay tuned and See you again…

Are you looking for job?

Job openings »


4 thoughts on “10 Types of MATLAB 2D Plot Explained with Examples and Code”

Leave a Comment