Open In App

Scatter Plot in MATLAB

Last Updated : 27 Oct, 2022
Like Article

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. 


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 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")


A simple scatter plot


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 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")



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 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")



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 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")


Scatter Plot from a table with filled circles.


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

Similar Reads

Mesh Surface Plot in MATLAB
Mesh Surface Plot is used to depict f(X, Y, Z) in a three-dimensional space. Matlab allows users to create mesh surface plots using the mesh() method. Different syntax of mesh() method are: Mesh(X, Y, Z)Mesh(Z)Mesh(___,C)Mesh(___, Name, Value)S = mesh()Mesh(ax,____)Mesh(X, Y, Z)It plots X, Y, Z on a three-dimensional surface.Matrices X, Y are plott
3 min read
How to plot a Histogram in MATLAB ?
A Histogram is a diagrammatic representation of a group of data over user-specified ranges. Basically, the histogram contains several bins. Bins are non-overlapping intervals in which the data is spread. In MATLAB we have a function named hist() which allows us to plot a bar graph. Syntax: hist(X) where X represents the data. The X is a vector. The
2 min read
2D Line Plot in MATLAB
'2D' stands for 2-dimensional and a 2D line is a line that is moved in 2-dimensions. A line in 2D means that we could move in forward and backward direction but also in any direction like left, right, up, down. In MATLAB we have a function named plot() which allows us to plot a line in 2 directions. Syntax: plot(X,Y) where X and Y represent the x a
3 min read
Plot a 3D Contour in MATLAB
Contour plots are used to show 3D surfaces by plotting z- slides on a 2D surface. A contour plot is also called a line plot. In contour, we have 3 variables x, y, z. The x, y variables are used to give the values for z, ( z=f(x, y)). The x and y variables are usually in a grid called meshgrid. There are various contour plots in MATLAB like contour,
3 min read
Plot Expression or Function in MATLAB
In this article, we will discuss how to plot expressions or functions in MATLAB. We can make use fplot() function in MATLAB to generate the plot corresponding to an expression or function. There are different variants of fplot() function fplot(f)fplot(f,xinterval)fplot(___,LineSpec)fplot(___,Name,Value) Now discussing each variant in detail. Below
3 min read
How To Plot a Function of Two Variables in MATLAB?
In order to plot a function of two variables in Matlab first you have to know some functions like plot, meshgrid() function, and very well know about how to plot one variable functions in Matlab. You have to follow some main contents or you can say procedure to plot a function. Below is the process to plot two variables function in Matlab: Algorith
2 min read
MATLAB Sine Wave Plot
A sine wave or sinusoidal wave is a periodic function that involves the trigonometric sine function along with other factors such as Amplitude, frequency, and time instant of the wave. The general form of a sine wave function is: Syntax: f(t) = A*sin(w*t + theta) Where, A = amplitudew = angular frequency of the wave, which is 2 *pi * frequencyt = t
2 min read
Automatically Plot Different Color Lines in MATLAB
MATLAB handles the colors of plots on its own as it has the functionality to do so. What this means is that when a user plots multiple lines in a plot, MATLAB gives each line a different color based on some calculations that are made by MATLAB itself. However, if one wishes to change those colors, MATLAB provides that option as well. In this articl
3 min read
How to Select Random Rows from a Matrix in MATLAB?
A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array. Syntax:matrix_name(i,j)where, i is the row number, and J is the column number which is to be indexed. Example 1: C/C++ Code % MATLAB code for select % random matrix elements mat = magic(5); mat(
2 min read
MATLAB Annotation
Annotations in MATLAB is a way of adding explanation or notes to the plots. Annotations add more information to the plots. There are different syntax formations for adding annotations to a plot: annotation(lineType,x,y)annotation(lineType)annotation(shapeType,dim)annotation(shapeType)annotation(___,Name,Value) Let's discuss all the above functions
3 min read
Article Tags :