plot() vs. scatter() functions in MATLAB

We can visualize data using multiple plots in MATLAB. We can call plot() and scatter() functions to create 2D plots because they have multiple purposes and styles for plots.

The plot() function

The plot() function is mostly used for making line plots. It connects data points with lines, allowing us to visualize trends or connections between variables. It is often used for time series and continuous data.

The syntax is as follows:

plot(X, Y)

Here, X is the vector of x-coordinates, and Y is the vector of y-coordinates.

Example

Let’s review an example of q = p^(1/2) in the code widget below.

p = 10:20;
q = p.^(1/2);
a = figure();
plot(p, q)
title('Plot Example')
xlabel('X-axis')
ylabel('Y-axis')

In the code above:

  • Line 1: It declares a variable, p, and stores points from 10 to 20.

  • Line 2: It takes the square root of p and stores it in q.

  • Line 4: It plots p and q using the plot() function.

  • Lines 5–7: These label the graph and axis.

The scatter() function

The scatter() function is used to create scatter plots. It displays individual data points as markers without connecting them with lines. It is used to visualize the distribution of data points or to identify outliers.

The syntax is as follows:

scatter(X, Y)

Here, X is the vector of x-coordinates, and Y is the vector of y-coordinates.

Example

Let’s review an example of q = p^3 in the code widget below.

p = 1:15;
q = p.^3;
a = figure();
scatter(p, q)
title('Scatter Example')
xlabel('X-axis')
ylabel('Y-axis')

In the code above:

  • Line 1: It declares a variable, p, and stores points from 1 to 15.

  • Line 2: It takes the cube of p and stores it in q.

  • Line 4: It plots p and q using the scatter() function.

  • Lines 5–7: These label the graph and axis.

plot() vs. scatter()

The key differences between these two functions are as follows:

The plot() Function

The scatter() Function

It connects data points with lines.

It displays individual data points.

It is typically used for continuous data.

It is useful for discrete data or emphasizing individual data points.

Conclusion

We can use plot() to visualize trends or relationships between variables with connected lines and scatter() to display individual data points without connecting lines.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved