Coding Ref

How to create a scatter plot in MATLAB

How to create a scatter plot in MATLAB

A scatter plot is a type of plot that shows the relationship between two variables. In a scatter plot, individual data points are represented by markers on a two-dimensional graph. The position of each marker is determined by the values of the two variables for that data point.

In MATLAB, the scatter function is used to create scatter plots. The scatter function takes two arguments, representing the x-coordinates and y-coordinates of the data points. For example, to create a scatter plot of the points (1,2), (3,4), and (5,6), you could use the following commands:

x = [1 3 5];
y = [2 4 6];
scatter(x, y)

In addition to the x- and y-coordinates, the scatter function also accepts a number of optional arguments that allow you to customize the appearance of the plot. For example, you can specify the color, size, and shape of the markers using the 'Color', 'SizeData', and 'Marker' properties.

Here is an example that creates a scatter plot wiÏth red markers of size 10 and square shape:

x = [1 3 5];
y = [2 4 6];
scatter(x, y, 'Color', 'r', 'SizeData', 10, 'Marker', 's')
You can also add labels, titles, and a legend to your scatter plot. For example, the following commands will add a title, axis labels, and a legend to the previous plot:
x = [1 3 5];
y = [2 4 6];
scatter(x, y, 'Color', 'r', 'SizeData', 10, 'Marker', 's')
title('Scatter Plot Example')
xlabel('x')
ylabel('y')
legend('data')

Four types of scatter plots

There are many different types of scatter plots that can be used to represent different types of data. Some common types of scatter plots include:

  • Scatter plot with a regression line: This type of scatter plot shows a linear or non-linear regression line that fits the data. The line can help to visualize the trend of the data and make predictions about future values.
  • Scatter plot with a smoothing line: This type of scatter plot shows a smooth line that passes through the data points. The line can help to highlight the overall pattern of the data and smooth out any noise or irregularities.
  • Scatter plot with multiple variables: This type of scatter plot shows the relationship between more than two variables. The data points are represented by markers with different colors, sizes, or shapes to represent the different variables.
  • Scatter plot matrix: This type of scatter plot shows a matrix of scatter plots, with each plot showing the relationship between two variables. The plots are arranged in a grid, with each row and column representing a different variable.

Conclusion

In MATLAB, the scatter function is used to create scatter plots. The scatter function takes two arguments, representing the x-coordinates and y-coordinates of the data points.

You'll also like

Related tutorials curated for you

    How to plot a Bode in MATLAB

    MATLAB subplot

    How to create a scatter plot in MATLAB

    How to create a 3D plot in MATLAB

    How to plot a line in MATLAB

    How to plot colors in MATLAB

    How to create a surface plot in MATLAB

    Plot markers in MATLAB

    How to plot a legend in MATLAB

    How to plot points in MATLAB

    MATLAB linspace