# Learn the Art of Dual-Plot Visualization in MATLAB with Detailed Examples

In this extensive guide, we will delve into the realm of data visualization using MATLAB. We will specifically focus on the creation of two separate graphs that can help engineers and data scientists …

Updated November 9, 2023

*Hey! If you love Computer Vision and OpenCV as much as I do let's connect on Twitter or LinkedIn. I talk about this stuff all the time and build cool projects.*In this extensive guide, we will delve into the realm of data visualization using MATLAB. We will specifically focus on the creation of two separate graphs that can help engineers and data scientists analyze data more efficiently and gain a better understanding of trends and patterns within their datasets. This article also covers the intricacies involved in crafting these plots, as well as the various code snippets required to achieve the desired results.

## Introduction

MATLAB is an essential tool for engineers and data scientists when it comes to handling large datasets and solving complex problems. It provides a vast range of functionalities, including various plotting tools that are useful for visualizing information in different forms such as graphs, diagrams, and charts. This article will concentrate on the process of producing two separate graphs using MATLAB, with detailed explanations and code snippets to ensure comprehension.

## Plot Types in MATLAB

Before we dive into plotting multiple graphs, let’s quickly review some essential plot types that can be produced using MATLAB:

- Line Plots (also called Line Graphs or Curves) - These are the most commonly used graphs for displaying trends over time or a relationship between variables. They are generally made up of one or multiple lines, connecting data points to visualize change over time.
- Bar Charts - They are useful for representing categorical data in a graphical form by comparing different categories (such as groups, genres, brands) with the use of rectangular bars or columns.
- Scatter Plots - These graphs are used when analyzing relationships between two variables (usually plotted against each other on x and y axes), where the data points represent individual values that contribute to the relationship’s visualization.
- Histograms - A type of graphical representation that shows the distribution of a set of values into continuous or discrete intervals, typically used in statistical analysis and for understanding how frequently certain values occur within a dataset.
- Pie Charts - These are commonly utilized when comparing parts to a whole, displaying numerical proportions by dividing a circle into slices whose sizes represent the respective values.

## Creating Two Separate Graphs

Now that we have covered some of MATLAB’s essential plot types, let’s proceed with building two different graphs in MATLAB. There are mainly three ways to achieve this: using subplots, figure windows, and separate figures. We will illustrate each method below with specific code snippets for reference.

Subplot Method

The subplot method allows you to create multiple plots on a single axis system within one window. It is an efficient way to compare different aspects of your data in a compact layout while maintaining the same scale for all plots.

Example: Plotting two separate graphs using the subplot method

First, let’s generate some sample data to use as input:

`x = linspace(0, 2 * pi, 100); y1 = sin(x); y2 = cos(x); z = rand(100,3);`

Next, create two plots with the subplot method. The `subplot`

command takes three arguments: row, column, and total number of subplots. We want to have one graph per axis, so in this case, each subplot will occupy its own position within a 1x2 grid.

```
figure(1);
subplot(1,2,1)
plot(x,y1,'r', 'LineWidth',3), hold on; % Plot sin(x) using red line with width 3
scatter(x,z(:,1),'b'); % Scatter plot of z[:,1] using blue color
title('First Subplot')
subplot(1,2,2)
plot(x,y2,'g', 'LineWidth',5), hold on; % Plot cos(x) using green line with width 5
scatter(x,z(:,2),'m'); % Scatter plot of z[:,2] using magenta color
title('Second Subplot')
```

In this example, we have two separate plots in the same figure window: the first subplot displays a line graph (sin(x) with red line and width 3), along with a scatter plot for `z[:,1]`

. The second subplot shows a line graph of cos(x) with green color and a linewidth of 5, coupled with a scatter plot for `z[:,2]`

using magenta.

Figure Windows Method

Another approach to achieving two separate graphs in MATLAB is by using multiple figure windows. This method allows you to showcase each graph within an independent window while still maintaining the flexibility to adjust their positions and sizes separately.

Example: Plotting two separate graphs using separate figure windows

Let’s generate some sample data to use as input, just like we did in our previous example:

`x = linspace(0, 2 * pi, 100); y1 = sin(x); y2 = cos(x); z = rand(100,3);`

To create two separate graphs using figure windows, start by generating the figure for each plot:

```
figure(1); % First Figure Window - Plot sin(x) and z[:,1]
plot(x,y1,'r', 'LineWidth',3), hold on; % Plot sin(x) using red line with width 3
scatter(x,z(:,1),'b'); % Scatter plot of z[:,1] using blue color
title('First Figure Window - Plot sin(x) and z[:,1]')
figure(2); % Second Figure Window - Plot cos(x) and z[:,2]
plot(x,y2,'g', 'LineWidth',5), hold on; % Plot cos(x) using green line with width 5
scatter(x,z(:,2),'m'); % Scatter plot of z[:,2] using magenta color
title('Second Figure Window - Plot cos(x) and z[:,2]')
```

In this case, we have created two separate figure windows – each with a graph displaying one line plot and a scatter plot for the respective data points from `z`

. These plots can be arranged side by side or in other layouts based on user preferences.

## Separate Figure Method

A third approach to creating multiple graphs in MATLAB is by using separate figure windows, where each graph resides within its own independent figure window. This allows for more flexibility when handling various plot types and data representations simultaneously.

Example: Plotting two separate graphs using separate figures

Let’s generate some sample data once again to use as input:

`x = linspace(0, 2 * pi, 100); y1 = sin(x); y2 = cos(x); z = rand(100,3);`

To create two separate graphs using separate figures, begin by generating the figure for each plot:

```
figure(1); % First Separate Figure Window - Plot sin(x) and z[:,1]
plot(x,y1,'r', 'LineWidth',3), hold on; % Plot sin(x) using red line with width 3
scatter(x,z(:,1),'b'); % Scatter plot of z[:,1] using blue color
title('First Separate Figure Window - Plot sin(x) and z[:,1]')
figure(2); % Second Separate Figure Window - Plot cos(x) and z[:,2]
plot(x,y2,'g', 'LineWidth',5), hold on; % Plot cos(x) using green line with width 5
scatter(x,z(:,2),'m'); % Scatter plot of z[:,2] using magenta color
title('Second Separate Figure Window - Plot cos(x) and z[:,2]')
```

In this example, we have created two separate figure windows with individual graphs that display a line plot for sin(x) and cos(x), as well as a scatter plot showing the data points from `z`

. These plots can be arranged side by side or in other layouts based on user preferences.

## Conclusion

Plotting two separate graphs using MATLAB provides engineers and data scientists with an effective visualization tool for comparing datasets, understanding relationships between variables, and identifying trends within their data. By employing the subplot method, figure windows approach, or utilizing separate figures, you can choose the most suitable technique based on your specific requirements and preferences. These techniques will enhance your MATLAB skills and enable better analysis of complex problems involving multiple datasets.