My figure wont display a line

조회 수: 5 (최근 30일)
Hasan Swain
Hasan Swain 2020년 4월 16일
댓글: Hasan Swain 2020년 4월 17일
For some reason I am unable to produce a line to connect my data points on the plot shown below. Please help. Also, I am using version 2018a.
time_array = linspace(1,34,34);
plot(time_array(1:2),2,'-ob')
hold on
plot(time_array(3:32),0,'-ok')
plot(time_array(33:34),-2,'-ob')

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 17일
Hasan - look closely at your calls to plot
plot(time_array(1:2),2,'-ob')
The first input is an array of two elements but the second input is an array of one element. So what is happening here? If you change the code to
hPlots = plot(time_array(1:2),2,'-ob')
then hPlots will be an array of graphics objects that are created when you call plot for this data. Now when you run this code and since there is no semi-colon at the end of the line, you will see something similar to the following in the command window
hPlots =
188.0033
189.0033
This tells us that two graphics objects are being created and so we have two separate plots! And so this is why there is no line connecting the data points. You need to have a 1-1 mapping of x and y points as your inputs. You can easily fix this by just replicating the second input for as many elements in the first input array. Try the following
hPlots = plot(time_array(1:2),repmat(2,1,length(time_array(1:2))),'-ob')
hold on
plot(time_array(3:32),repmat(0,1,length(time_array(3:32))),'-ok')
plot(time_array(33:34),repmat(-2,1,length(time_array(33:34))),'-ob')
Now your data points shoudl be connected. (Note also how hPlots is a single graphics object handle.)
  댓글 수: 1
Hasan Swain
Hasan Swain 2020년 4월 17일
Thanks a lot Geoff! This was very helpful.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by