How to connect the point of the plot on Matlab?
20 views (last 30 days)
Show older comments
So I have the following code, which is supposed to update the P column of matrix x by adding columns 2 and 5 together. Then the code is supposed to add tall rows of column P together. This process will continue for 10 hours by using "for loop"
Now, when I plot by using 'plot function' then I dont see any graph, but when I use scatter then I see graph of discrete time which make sense and the scatter graph is correct.
Now, how do I connect those points together so my graph will look like continuous time.
(Please, do not say you dont need for loop for that because there is a lot of code that goes inside the for loop, I am just trying to give simple idea of my code.
Would you please tell me how to solve this?
for y=1:10
P=BR(:,2) + BR(:,5)
Ptotal=sum(P())
% figure('Name','Load vs Time') %Also, how can I fix this line so that my figure has a name and all points will appear on a single figure
scatter(y,Ptotal,'LineWidth',10)
title('load vs time')
xlabel('Time(s)')
ylabel('load')
hold on
end
hold off
1 Comment
DGM
on 4 Dec 2021
Edited: DGM
on 4 Dec 2021
scatter() is for scattered plots -- markers without lines.
plot() will plot markers and/or lines as specified.
If you're plotting one single point at a time, you're not plotting a series. You're plotting single points. There isn't anything to draw a line between. Each point is its own series. This approach is typically undesired.
When using plot() like that, the reason you see nothing is because you haven't specified a marker style. Each plot object has no marker, but has a solid line between points -- but since there's only one point in each series, there are no lines either. If you specify a marker style, then you'll be in the same scenario you are with the scatter plot: a bunch of unrelated points that can't be connected.
Don't plot anything one point at a time. Collect your data and store it in a vector/array and then plot it when you're done.
Answers (1)
dpb
on 4 Dec 2021
%figure('Name','Load vs Time')
%Also, how can I fix this line so that my figure has a name and all points will appear on a single figure
What's wrong with title? A figure is just a container for the axes object; it is the axes that has a title.
hF=figure;
hAx=axes;
title('load vs time')
xlabel('Time(s)')
ylabel('load')
hold on
for y=1:10
P=BR(:,2) + BR(:,5)
Ptotal=sum(P())
scatter(y,Ptotal,'LineWidth',10)
end
hold off
Above takes all the invariant stuff outside the loop -- there's no point redrawing the same title and labels every time.
For animation, look instead into using animatedline and addpoints instead. They're designed for that purpose. One can solve the problem other ways, but with the introduction of new features, there's no point in regressing.
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots 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!