Plot function not working correctly - Non-continuous graphing

Hello,
I am trying to graph the following data but the plot function is not connecting the points in a continuous manner.
Find below two graphs. The first is when I try to plot the two arrays and the second is when I scatter them. As you can see from the second graph, I wish to connect these points with a continuous line. However as you can see from the first graph, when I use plot it creates a mess.
Does anyone have any suggestions?
Code:
xta = [2.1 0.3 0.8 1.7 1.6 2 2.4 2.2 0.4 3 1.2 0.7 0.2 0 2.5 0.9 0.5 2.6 2.8];
h6_ta = [5.97853697427416 -2.80975652924937 -4.80049651251653 3.64650224735648 3.10258743393927 5.16551230747825 10.8859845706138 7.13859238208390 -4.01961018236456 30.0721681625590 -0.578609482801659 -5.26354831338203 -1.45940663189310 0.154573905982674 13.5744223560721 -3.99608140671335 -4.88444411593052 16.7730149487159 24.0494440428712];
subplot(2,1,1);
plot(xta,h6_ta)
title 'Figure 1'
subplot(2,1,2);
scatter(xta,h6_ta,'o')
title 'Figure 2'
Figure:
Thank you

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 10월 17일
The array xta is not sorted. Try the following code
xta = [2.1 0.3 0.8 1.7 1.6 2 2.4 2.2 0.4 3 1.2 0.7 0.2 0 2.5 0.9 0.5 2.6 2.8];
h6_ta = [5.97853697427416 -2.80975652924937 -4.80049651251653 3.64650224735648 3.10258743393927 5.16551230747825 10.8859845706138 7.13859238208390 -4.01961018236456 30.0721681625590 -0.578609482801659 -5.26354831338203 -1.45940663189310 0.154573905982674 13.5744223560721 -3.99608140671335 -4.88444411593052 16.7730149487159 24.0494440428712];
subplot(2,1,1);
[xta_sorted, idx] = sort(xta);
plot(xta_sorted,h6_ta(idx))
title 'Figure 1'
subplot(2,1,2);
scatter(xta,h6_ta,'o')
title 'Figure 2'

댓글 수: 2

Thank you very much. That works perfectly!
I am glad to be of help!!!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2020년 10월 17일

댓글:

2020년 10월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by