## 2 Plots Matlab

Hi I have 3 scripts which all produce a figure. They are all graphs. How would i put them all on one script and have all plots displayed as separate figures. This will let you have two plots on the same figure so you can see them both at the same time and don't have to keep switching between figures. Subplot(1, 2, 1); plot(1:30). This chapter describes how MATLAB can be used to create and format many types of two-dimensional plots. Three-dimensional plots are addressed separately in Chapter 9. An example of a simple two-dimensional plot that was created with MATLAB is shown in Figure 5-1. The figure contains two curves that show the variation of light intensity with.

Did you ever wonder seeing amazing 3D graphs in MATLAB? How to draw multiple 3D plot graphs in MATLAB?

Plot Multiple Lines. By default, MATLAB clears the figure before each plotting command. Use the figure command to open a new figure window. You can plot multiple lines using the hold on command. Until you use hold off or close the window, all plots appear in the current figure window. Introduction to 2D Plots in Matlab. 2D Plots in Matlab is a feature that enables a user to create the two-dimensional graph for any dependent variable as a function of a depending variable. A plot can present the data in continuous, discrete, surface or volume form. The default standard function for 2D graph plotting is plot function.

This is an in-depth tutorial for you. I will explain the different MATLAB 3D plot examples and how to draw them.

This tutorial is an extension of a previous tutorial two-dimensional [2D] MATLAB plot.

When I share the 2D plot graph tutorial, some of the readers asked me about the 3D plot. And I decided to write about it.

This tutorial provides you the plot’s functions, syntax, and code, for example for the five main different types of 3D plots. At the end of this post, you will be able to draw your own 3D plot graph in MATLAB.

It’s amazing. Right?

Let’s start.

### 3D MATLAB Plot Introduction

In general, the three-dimensional plots consist of the three vectors (x,y,z) in the same graph.

In MATLAB, the plot3() function is used to draw the 3D plot graph. You can also use a specified line style, marker, and color for drawing 3D plots.

The general syntax to display the 3D plot is,

Let’s start drawing different types of the 3D plot graph…

### Classifications of Three-Dimensional Plots MATLAB 3D plot Examples

Here, we are considering, the five main different types of three-dimensional (3D) plots. These graphs are mostly used in the industry.

The following list of different 3D plots as,

- Mesh Plot
- Surface Plot
- Ribbon PLot
- Contour Plot
- Slice Plot

As a part of this tutorial about MATLAB 3D plot examples, I am describing the topmost five 3D plots one-by-one.

#### 1. Mesh 3D Plot in MATLAB

The mesh plotting function is used to display the mesh plot. It produces a wireframe surface where the lines connecting the defining points are colored.

**How to create the Mesh plot in MATLAB?**

For the mesh plotting in MATLAB, you need to pass the array values to the mesh function.

**Syntax:**

Mesh function transforms the domain specified by vectors (X, Y, Z) into arrays (x,y,z).

The syntax for the Mesh Plot is,

**MATLAB Code:**

As an example, we are plotting the mesh 3D plot for square root mathematical function.

**Output in MATLAB:**

See here, you get a colorful and smooth connecting surface line of three-dimensional [3D] Mesh plot.

You can also plot the graph for various Mathematical Expressions in MATLAB.

#### 2. Surface 3D Plot in MATLAB

A surface plot is somewhat similar to a mesh plot. The main difference between them is, in the surface plot, the connecting lines and the faces both will be displayed in the dark color.

**How to create the Surf plot in MATLAB?**

**Syntax:**

In the surface plot, ‘surf’ function is used. So, you can write a simple format like ‘function name(array)’.

**MATLAB Code:**

Let’s write a MATLAB code for the three-dimensional surface plot for an exponential function exp().

**Output in MATLAB:**

After the getting output of surface plot, you will see the connecting lines and the faces are both displayed in the same shade.

#### 3. Ribbon 3D Plot in MATLAB

As the name ribbon, this 3D plot graph will be having different color ribbons.

**How to create the ribbon plot in MATLAB?**

Here, we are using ribbon() function for plotting ribbon 3D MATLAB plot.

**Syntax:**

The general syntax for writing code,

**MATLAB Code:**

To create a ribbon plot using peak function for mathematical function ((x²)-(y²))

**Output in MATLAB:**

You can see each and every colorful shade ribbons.

#### 4. Contour 3D Plot in MATLAB

**How to create the three dimensional [3D] contour plot?**

To create the three dimensional [3D] contour plot, we are using the ‘contour3’ function.

**Note:** You can plot the Contour 2D plot by using the only ‘contour’ function.

**Syntax:**

The syntax for the three-dimensional contour plot,

**MATLAB Code:**

We are plotting the contour plot for the exponential mathematical equation is (exp( x²-y²)).

**Output in MATLAB:**

Below is a diagram for three dimensional [3D] contour plot.

#### 5. Slice 3D Plot in MATLAB

For plotting slice graph, you must know volumetric data(v), specification of three-dimensional coordinate (x,y,z), and ‘xslice, yslice, zslice’.

**Syntax:**

Slice plot’s syntax is

Where,

- xslice- ‘x’ coordinate data for slice plot
- yslice- ‘y’ coordinate data for slice plot
- zslice- ‘z’ coordinate data for slice plot

**MATLAB Code:**

Slice plot is little different from other 3D plots types. When you are writing MATLAB code for Slice plot, you need to specify each coordinator value.

Let’s draw the slite plot graph for an exponential mathematical equation.

**Output in MATLAB:**

The output looks like the below picture.

These are the topmost three dimensional [3D] used in the industry projects.

This is all about different MATLAB 3D plot examples. I have explained the different classification of MATLAB 3D plots with simple code and syntax.

If you have doubt, write in the comment. I will reply to you as soon as possible.

**Other MATLAB Tutorials:**

**Thanks for Reading!**

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.

## Multiple plots

The plot command can plot several sets of vectors.

Create a vector x of 401 equally spaced points on [0, 1].

Create a vector y1 of function values.

Create a vector y2 of function values.

Plot both sets points in the same figure.

Add a grid.

Add a title. What is still missing?

In general you can use

plot(x1, y1, s1, x2, y2, s2, x3, y3, s3)

where x1 and y1 are vectors of the same length and s1 is an optional string.

## Legends

When there are multiple plots in the same figure it is a good idea to add a legend, using, for example,

legend(string1, string2, string3)

Here string1 is a string describing the first set of values plotted, string2 is a string describing the second set of values plotted, and string3 is a string describing the third set of values plotted.

You can use the mouse to reposition the legend box within the plot, or you can specify the location of the legend box. See

help legendfor more information.

## The hold command

If you have already created a plot and later wish to add another plot, then the hold command is useful.

Create the first plot assuming x, y1 and y2 are defined as above.

Turn the hold on.

Add the second plot.

Turn the hold off.

Casio graph 25 e leclerc. Note that when using a single plot command, MATLAB adjusts the colours for successive plots. When using the hold command you must explicitly set the colours, for example using plot(x, y2, 'g').

## Subplots

Sometimes you want a single figure containing several individual subplots. The MATLAB command

subplot(m, n, k)

creates an m by n array of plots and positions you at plot number k, where the plots are numbered counting across rows.The most common examples are

- a 2 by 1 grid of subplots for two plots one on top of each other;
- a 1 by 2 grid for two plots side by side.

Here is a 2 by 2 grid of subplots to make it clear how the numbering of subplots works.

Create vector of plot points.

2 by 2 grid of subplots, at subplot 1.

Add plot on current subplot.

Add grid and title on current subplot.

2 by 2 grid of subplots, at subplot 2.

Add plot on current subplot.

Add grid and title on current subplot.

2 by 2 grid of subplots, at subplot 3.

Add plot on current subplot.

Add grid and title on current subplot.

2 by 2 grid of subplots, at subplot 4.

Add plot on current subplot.

Add grid and title on current subplot.

## Create 2 Plots Matlab

## 2 Plots Matlab Lines

## Axis limits

If you are really observant you will have noticed that the limits of the y-axis on the first subplot is from 0 to 1, while the other three plots all have y ranging from -1 to 1. MATLAB tries to choose good axis limits based on the data that is being plotted. However sometimes you want to change the axis limits.

The simplest way to do this is to use

xlim([xmin xmax])

to make the x-axis run from xmin to xmax. To adjust the limits of the y-axis use

ylim([ymin ymax])

to make the y-axis run from ymin to ymax. Note that the argument to xlim and ylim is a two element vector giving the lower and upper limits for the axis.

Another way to control the limits and scaling of the axes is to use the axis command, for example

axis([xmin xmax ymin ymax])

By default the x-scale is slightly larger than the y-scale, so you get a rectangular plot on the screen. To make the axis scaling equal use

axis equal

which is required to make a circle look like a circle!

As always, more information can be obtained by typing

help axis

in the MATLAB command window.

Create vector t of parameter values.

Store the values of cos(t) and sin(t).

Plot the two curves.

Make the axis scaling equal.

Adjust the axis limits.

Add a grid.

## Figures

MATLAB draws a plot in the current figure window. Figure windows are labelled 1, 2, 3, .. The command

figure

## Combine 2 Plots Matlab

creates a new figure window. Alternatively the commandfigure(k)

where k is a positive integer, opens figure window k if it already exists, or creates figure window k if it does not exist.

## Self-test Exercise

Plot the functions sin(x), x, and 2x/pi over the interval [0, pi/2], including the title 'Bounds on sin(x)', a grid, a legend and making sure the x-axis corresponds to the plot interval.Answer:

x = linspace(0, pi/2, 401);

plot(x, sin(x), x, x, x, 2*x/pi);

title('Bounds on sin(x)')

grid on

legend('sin(x)', 'x', '2x/pi')

xlim([0, pi/2])

Use the mouse to select the text between the word 'Answer' and here to see the answer.

## Summary

The plot command can plot several sets of data on the one set of axes. In this case a legend should be added.

## 2 Plots Matlab Plot

Several different plots within the one figure can be created using the subplot command.

## 2 Plots Matlab Lines

Axis limits and scaling can be modified with the xlim, ylim and axis commands.