Open In App

Scatter Plot in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Scatter Plot is a popular type of graph plot that plots pairs of coordinate points discretely rather than continuously. These plots are extensively used in various industries such as the data science industry because of their statistical importance in visualizing vast datasets. 

Scatter Plots in MATLAB:

MATLAB provides a power scatter() function to plot to scatter plots with many additional options. 

Syntax:

scatter(x_data, y_data, <optional arguments>)

The x_data and y_data represent vectors of equal length which are used as x-coordinates and y-coordinates respectively. There are various optional arguments that we will see later on in this article. Now, let us understand the scatter function with the help of examples.

Plotting a simple vector x with y.

Example 1:

Matlab




% MATLAB code for scatter plot
% x_data
x = 23:75; 
  
% y_data
y = 1:53;   
  
% Plotting the scatter plot
scatter(x,y),xlabel("X"),ylabel("Y"),title("Scatter 1")


Output:

A simple scatter plot

Here,

xlabel() gives the label to the horizontal axis.
ylabel() gives the label to the vertical axis.
title() gives the title to the graph

Now we will plot a scatter plot with variable sizes of circles.

Example 2:

Matlab




% MATLAB code for scatter plot
x = 23:75;  %x_data
y = 1:53;   %y_data
  
% Size vector of same size as x and y
csize = linspace(1,50,53);
  
% Plotting the scatter plot
scatter(x,y,csize),xlabel("X"),ylabel("Y"),title("Scatter 1")


Output:

 

Here, the csize vector specifies the size for each circle in the same order as x and y. You can also use a scalar size for each circle or a vector as in the above example for variable size.

We can also have different colors for the circles. See the below implementation.

Example 3:

Matlab




% MATLAB code 
x = linspace(-pi,pi,100);  %x_data
y = cos(x).^2;   %y_data
  
% Color vector of same size as x and y
col = linspace(1,7,length(x));  
  
% Plotting the scatter plot
scatter(x,y,5,col),xlabel("X"),ylabel("Y"),title("Scatter 1")


Output:

 

Here, we are plotting the cos2(x) function in [-pi, pi] domain. The “col” vector specifies the vector which stores the RGB value for each circle and we have used the fixed size 5, for each circle.

Note: It is necessary to specify the circle size before the color argument due to the syntax of the scatter command. In case you want it to be the default, put an empty vector [] instead of 5 in the above example.

We can also plot two columns of a table at a time. 

Example 4:

Matlab




% MATLAB code scatter plot 
x = linspace(-3,3,73);
y = x.^3;
  
% Creating table from x and y vectors
tbe = table(x,y);
  
% Plotting the scatter plot from table tbe
scatter(tbe,"x","y",'filled'),xlabel("X Column"),
ylabel("Y Column"),title("Scatter From Table")


Output:

Scatter Plot from a table with filled circles.

 

In this example, we scatter plot the two columns of the table tbe with filled circles.



Last Updated : 27 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads