MATLAB is a powerful programming language, that can be used to draw various plots used in machine learning, deep learning, computer vision, and big data programming. Let us start with coding for plots in MATLAB. Today I'd like to welcome two guest bloggers. David Garrison is a MATLAB Product Manager here at MathWorks. Andrei Ursache is a member of the Advanced Support Group with expertise in image acquisition and instrument control.
When I learned about Pie plot and other two dimensional plots in MATLAB (MATLAB 2D plot), first time, I was curious to know…
Plot Vector Matlab Introduction to Plot Vector Matlab MATLAB provides us with plenty of functionalities, useful in various computational problems. In this article we will learn how to create vector plots in MATLAB. MATLAB Plot Gallery Select a Web Site Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
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…
(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.
Now, I am describing each 2D plots by using the MATLAB code and its decorating functions in details.
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?
Syntax:
The syntax for the Area plot…
MATLAB Code:
As an example, we are plotting Area graph for trigonometric function. It is one of the types of functions in MATLAB.
Note:
Output in MATLAB:
After the running code, you will get the Area plot as shown below.
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?
Syntax:
The syntax for the stem plot is,
MATLAB Code:
Here we are creating the stem plot using the MATLAB functions for array of some random discrete numbers.
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.
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?
Syntax:
The syntax for the bar plot is,
MATLAB Code:
Let’s write a MATLAB code to plot the Bar for arrays of some random numbers.
Output in MATLAB:
Below is diagram as bar 2-dimensional 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?
Syntax:
The same syntax uses like bar plot,
MATLAB Code:
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.
Errorbar plot is again of the types of bar plot.
How to draw an Errorbar Plot in MATLAB?
Syntax:
The syntax for the Errorbar plot are,
MATLAB Code:
We are adding color function to decorate our plot.
Output in MATLAB:
See the 2-dimensional error plot.
This is again one of the MATLAB 2D plots that look more like stairs.
How to design the stairs plot in MATLAB?
Syntax:
MATLAB Code:
Here, I am writing the MATLAB code of stairs plot with the black (k) color function.
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?
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?
Syntax:
MATLAB Code:
To write the simple code for the 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.
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?
Syntax:
In Polar plot, you can write the following syntax,
Polar plot function has arguments as theta, radius, line specification.
MATLAB Code:
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].
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?
Syntax:
The syntax of the compass plot is,
MATLAB Code:
For the designing compass plot, you just need to write only ‘compass’ function.
Output in MATLAB:
Plotting the scatter plot in MATLAB is simple.
How to draw a Scatter Plot in MATLAB?
Syntax:
For the scatter plot,
MATLAB Code:
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…
I have completed master in Electrical Power System. I work and write technical tutorials on the PLC, MATLAB programming, and Electrical on DipsLab.com portal.
Sharing my knowledge on this blog makes me happy. And sometimes I delve in Python programming.
This example shows how to combine plots in the same axes using the hold
function, and how to create multiple axes in a figure using the tiledlayout
function. The tiledlayout
function is available starting in R2019b. If you are using an earlier release, use the subplot
function instead.
By default, new plots clear existing plots and reset axes properties, such as the title. However, you can use the hold on
command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off.
When the hold state is on, new plots do not clear existing plots or reset axes properties, such as the title or axis labels. The plots cycle through colors and line styles based on the ColorOrder
and LineStyleOrder
properties of the axes. The axes limits and tick values might adjust to accommodate new data.
You can display multiple axes in a single figure by using the tiledlayout
function. This function creates a tiled chart layout containing an invisible grid of tiles over the entire figure. Each tile can contain an axes for displaying a plot. After creating a layout, call the nexttile
function to place an axes object into the layout. Then call a plotting function to plot into the axes. For example, create two plots in a 2-by-1 layout. Add a title to each plot.
Note: This code uses the tiledlayout
function, which is available starting in R2019b. If you are using an earlier release, use the subplot
function instead.
To create a plot that spans multiple rows or columns, specify the span
argument when you call nexttile
. For example, create a 2-by-2 layout. Plot into the first two tiles. Then create a plot that spans one row and two columns.
Modify the axes appearance by setting properties on each of the axes objects. You can get the axes object by calling the nexttile
function with an output argument. You also can specify the axes object as the first input argument to a graphics function to ensure that the function targets the correct axes.
For example, create two plots and assign the axes objects to the variables ax1
and ax2
. Change the axes font size and x-axis color for the first plot. Add grid lines to the second plot.
You can control the spacing around the tiles in a layout by specifying the Padding
and TileSpacing
properties. For example, display four plots in a 2-by-2 layout.
Minimize the spacing around the perimeter of the layout and around each tile by setting the Padding
and TileSpacing
properties to 'none'
.
You can display a shared title and shared axis labels in a layout. Create a 2-by-1 layout t
. Then display a line plot and a stem plot. Synchronize the x-axis limits by calling the linkaxes
function.
Add a shared title and shared axis labels by passing t
to the title
, xlabel
, and ylabel
functions. Move the plots closer together by removing the x-axis tick labels from the top plot and setting the TileSpacing
property of t
to 'compact'
.
hold
nexttile
tiledlayout
title