필터 지우기
필터 지우기

Plotting with a for loop

조회 수: 1 (최근 30일)
EYKL
EYKL 2021년 9월 24일
댓글: Cris LaPierre 2021년 9월 25일
Hello all,
I was wondering if it was possible to use a for loop to plot certain lines on my figures. For example, my 1st plot will contain all 4 lines while my 2nd plot will omit the very 1st line in my 1st plot and only plot the remaining 3 lines. My 3rd plot will then omit the first 2 lines and only plot the remaining 2 lines. Instead of adding the code manually, is there a simpler approach?
% % x,y,z,a,b = My datasets
tiledlayout(3,1);
figure;
nexttile; % 1st plot with all 4 lines
plot(x,y,'r-');
hold on;
plot(x,z,'b-');
hold on;
plot(x,a,'k-');
hold on;
plot(x,b,'k-');
nexttile; % 2nd plot with 3 lines
plot(x,z,'b-');
hold on;
plot(x,a,'k-');
hold on;
plot(x,b,'m-');
nexttile; % 3rd plot with 2 lines
plot(x,a,'k-');
hold on;
plot(x,b,'m-');

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 9월 24일
If your variables y,z,a & b are column vectors (and if they are not, you could easily make them column vectors), you could do the following
x = 1:10;
data = rand(10,4); % data = [y,z,a,b];
cspec = {'r','b','k','m'};
tiledlayout(3,1);
ax1 = nexttile; % 1st plot with all 4 lines
plot(x,data(:,1:4));
colororder(ax1,cspec(:));
ax2 = nexttile; % 2nd plot with 3 lines
plot(x,data(:,2:4))
colororder(ax2,cspec(2:end));
ax3 = nexttile; % 3rd plot with 2 lines
plot(x,data(:,3:4))
colororder(ax3,cspec(3:end));
  댓글 수: 2
EYKL
EYKL 2021년 9월 25일
Hi @Cris LaPierre, I noticed you used colororder to fix the colours of the plots. Is there a way I can do this with point markers and linetype? For example:
%1st line = '-rx';
%2nd line = '-bo';
%3rd line = '-g+';
Also, I didn't know colororder existed. I learned something new today. Thanks.
Cris LaPierre
Cris LaPierre 2021년 9월 25일
There is, but there is not a helper function for that purpose. You must manually set the properties of the axes. The property you want is LineStyleOrder, which allows you specify both line and marker style. However, take note of this comment on how LineStyleOrder works:
"MATLAB assigns styles to lines according to their order of creation. It changes to the next line style only after cycling through all the colors in the ColorOrder property with the current line style."

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by