Categories

# 3d scatter plot

3 D scatter plots are used to show the relationship between 3 variables on the cartesian coordinates. 3 D scatter plots are used to interpret the spread of data and identify any outliers. Scatter plots are very useful in data science, where relationships in the test data are used to create algorithms to predict the output. In MATLAB, we use the scatter3() function with 3 arguments to create 3D plots. In this topic, we are going to learn about Matlab 3d scatter plot.

## Syntax of the scatter3 function:

1. scatter3 (a, b, c)
2. scatter3 (a, b, c, Z)
3. scatter3 (a, b, c, Z, Y)

## Description of the scatter3 function:

1. scatter3 (a, b, c) is used to create a scatter plot for the input vectors a, b, c. By default, MATLAB scatter plots the data points as circles
2. scatter3 (a, b, c, Z) is used to create a scatter plot for the input vectors a, b, c with the circles of the size defined by the argument ‘Z.’
3. scatter3 (a, b, c, Z, Y) is used to create a scatter plot for the input vectors a, b, c with the circles of the size defined by the argument ‘Z’ and color specified by the argument ’Y.’

## Examples of Matlab 3d scatter plot.

Let us now understand the code to use the scatter3 function in MATLAB.

## Example 1:

In this example, we will use the scatter3 function in its most basic form and plot a sine wave and a co-wave with random noise. Below are the steps to be followed:

1. Define the random noise
2. Initialize the sin wave
3. Initialize the cos wave

## Code:

``````c = linspace(0, 2 * pi, 100);
[Initializing the random noise] a = sin(c) + rand(1, 100);
[Initializing the sine wave] b = cos(c) + rand(1, 100);
[Initializing the cos wave] scatter3(a, b, c)
[Using the scatter3 function to plot the input waves]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained a 3D scatter plot for our input waves using the scatter3 function.

## Example 2:

Next, we will learn how to control the size of the circles in the scatter plot generated using the scatter3 function. For this, we will pass the required size as the 4th argument to the scatter3 function.

## Code:

``````c = linspace(0, 2 * pi, 100);
[Initializing the random noise] a = sin(c) + rand(1, 100);
[Initializing the sine wave] b = cos(c) + rand(1, 100);
[Initializing the cos wave] scatter3(a, b, c, 50)
[Using the scatter3 function to plot the input waves. Please note that we have passed ‘50’ as the 4th argument which defines the size of the circles in the plot]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained a 3D scatter plot for our input waves using the scatter3 function. Also, the size of the circles in the plot is now larger in comparison to the plot in Example 1.

## Example 3:

In the above two examples, the color of the circles obtained is blue. Let us see how to change this color as per our requirements. In this example, we will plot a logarithmic function and an exponential function with random noise. Below are the steps to be followed:

1. Define the random noise
2. Initialize the logarithmic function
3. Initialize the exponential function

## Code:

``````c = linspace(0, 2 * pi, 100);
[Initializing the random noise] a = log(c) + rand(1, 100);
[Initializing the log function] b = exp(c) + rand(1, 100);
[Initializing the exponential function] scatter3(a, b, c, 50, 'green')
[Using the scatter3 function to plot the input waves. Please note that the argument ‘green’ is used to get the circles of the scatter plot in green color]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained a 3D scatter plot for our input waves using the scatter3 function. Also, the color of the circles is ‘green’, as expected by us.

We can also get the circles of solid nature rather than transparent. For this, we will pass another argument ‘filled’.

## Code:

``````c = linspace(0, 2 * pi, 100);
[Initializing the random noise] a = log(c) + rand(1, 100);
[Initializing the log function] b = exp(c) + rand(1, 100);
[Initializing the exponential function] scatter3(a, b, c, 50, 'green', 'filled')
[Using the scatter3 function to plot the input waves. Please note that the argument ‘filled’ is used to get the circles of the solid nature]``````

This is how our input and output will look like in MATLAB:

## Output:

As we can see in the output, we have obtained a 3D scatter plot for our input waves using the scatter3 function. Also, the circles in the plot are of solid nature, as expected by us.

If we want to get the plot in some shape other than the circles, we again pass the required shape as an argument. For example, we can pass ‘star’ as an argument to create the plots using the ‘star’ shape.