plot too many lines
이전 댓글 표시
Why does MATLAB plot so many dashed lines when I am plotting just one for the 95% confidence intervals?
xd = bindex_total2_nonan(:,9); % depth
yd = bindex_total2_nonan(:,6); % pos
% poly fit
[pd,Sd] = polyfit(xd,yd,1);
[y_fitd,deltad] = polyval(pd,xd,Sd);
conf1 = y_fitd+2.*deltad;
conf2 = y_fitd-2.*deltad;
figure('Renderer', 'painters', 'Position', [10 10 900 600]);
scatter(bindex_total2(:,9),bindex_total2(:,6),'+','k');
hold on;
plot(xd,y_fitd,'color','k')
plot(xd(1:2:54),conf1(1:2:54),'r--')
plot(xd(1:2:54),conf2(1:2:54),'r--')
댓글 수: 6
Geoff Hayes
2019년 2월 4일
Elias - what are the dimensions of the inputs to your calls to plot? Are the xd and y_fitd column or row arrays, or are they matrices? If matrices, that may explain why you are seeing many dashed lines...
Walter Roberson
2019년 2월 4일
We see from the first line that xd must be a column vector, and yd as well. y_fitd is the same length as xd generated from the fitted line.
Walter Roberson
2019년 2월 4일
I notice you never turn "hold off". If you were to run this code several times, all of the runs would get plotted together.
Elias de Korte
2019년 2월 4일
I was also looking at this problem, and if it's not the dimensions of xd/yfit that causes the problem maybe its indeed like @Walter Roberson suggested. Also maybe its a good practice to close figures (close all;) and clear workspace variables (clear;) at the start of the script.
Elias de Korte
2019년 2월 4일
채택된 답변
추가 답변 (1개)
Rik
2019년 2월 4일
You should not use clear on its own. If you want to, you can use
clear variables
As to your actual problem, is your xd not sorted? That would cause the line to jump back and forth, making it look like you have a lot of different lines.
댓글 수: 3
YT
2019년 2월 4일
Can you clarify why "you should not use clear on its own"? Isnt typing every specific variable you want to clear from your workspace much more troublesome than just using clear;?
Rik
2019년 2월 4일
Maybe I worded it a bit too strongly, but when people read the advice to use clear, they will often understand it to use this
clc,close all,clear all
This means that not just the variables are cleared, but also loaded functions and clases, slowing code down. You can read the options in the doc for clear.
Literally typing
clear variables
seems to do the same as only using clear, but this way you show your intent as well preventing misunderstandings.
Also, you can use wildcards and/or regular expressions to specify groups of variables you might want to clear.
YT
2019년 2월 4일
Ah okay good to know. Thanks for the clarification.
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!