Matlab plot is drawing an individual line to each data point
Show older comments
So im writing a matlab script that creates a 3 by 100 matrix x and a 1 by 100 matrix n. When I try to plot it, matlab draws an individual line segment from the origin to each data point, rather than just connecting the data points. From what I found online, it's due to the plot function being inside a for loop, but after I moved it outside the loop it kept doing the same thing. Can somebody help.

Here's what the plot looks like
Accepted Answer
More Answers (1)
Animesh
on 24 Jun 2024
0 votes
Hi Alexandr,
It sounds like you might be using the plot function incorrectly in MATLAB. To plot a continuous line connecting your data points, you need to provide the x and y coordinates of your data points as vectors. Here’s a basic example of how to do it correctly:
% Sample data points
x = [1, 2, 3, 4, 5];
y = [2, 4, 6, 8, 10];
% Plotting the data points with a continuous line
figure;
plot(x, y, '-o'); % '-o' creates a line with circle markers at data points
xlabel('X-axis label');
ylabel('Y-axis label');
title('Plot of Data Points');
grid on;
In this example:
- x and y are vectors containing the coordinates of the data points.
- The '-o' argument specifies that MATLAB should draw a line connecting the data points and place a circle marker at each data point.
If you want to plot multiple sets of data on the same graph, you can do so by including additional pairs of x and y vectors in the plot function call:
% Additional data points
x2 = [1, 2, 3, 4, 5];
y2 = [3, 6, 9, 12, 15];
% Plotting multiple sets of data points
figure;
plot(x, y, '-o', x2, y2, '-x'); % '-x' creates a line with x markers at data points
xlabel('X-axis label');
ylabel('Y-axis label');
title('Plot of Multiple Data Sets');
legend('Data Set 1', 'Data Set 2');
grid on;
Make sure that your x and y vectors are of the same length, as MATLAB requires this to plot the points correctly.
For more information on plot function, please consider going through the documentation: https://in.mathworks.com/help/matlab/ref/plot.html
1 Comment
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
