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
x = 23:75;
y = 1:53;
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
x = 23:75;
y = 1:53;
csize = linspace(1,50,53);
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
x = linspace(-pi,pi,100);
y = cos(x).^2;
col = linspace(1,7,length(x));
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
x = linspace(-3,3,73);
y = x.^3;
tbe = table(x,y);
scatter(tbe, "x" , "y" , 'filled' ),xlabel( "X Column" ),
ylabel( "Y Column" ),title( "Scatter From Table" )
|
Output:
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
Share your thoughts in the comments
Please Login to comment...