How to use loop for plotting multiple plots and legend?

조회 수: 11 (최근 30일)
Arshey Dhangekar
Arshey Dhangekar 2022년 8월 25일
댓글: Dyuman Joshi 2022년 9월 12일
I want to plots mulplits plots on and I have single x time axis and temp1 upto temp20 similalry so how to use loop here for labeling the legend and plots instead of writing for 20 times.
t=readtable ()
plot (t.time,t.temp1, .t.time,temp20)
legend ("Temp1",....,"Temp2")

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 8월 25일
What you want to do is possible but quite more tedious than what you did.
y=readtable('test file.xlsx');
z=cell(1,20);
for i=1:20
z{i}=sprintf('temp%d', i); %collecting legend labels in a cell array
plot(table2array(y(:,'Time')),table2array(y(:,z{i})))
hold on
end
hold off
legend(z,'Location', 'best','FontSize',6 , 'NumColumns',4)

추가 답변 (1개)

Arshey Dhangekar
Arshey Dhangekar 2022년 9월 1일

How to vary colors because it's difficult to identify the parameters. Any suggestions

  댓글 수: 3
Arshey Dhangekar
Arshey Dhangekar 2022년 9월 11일
Temp 1 and temp 15 have same plot colors and similarly other plots also so it's hard differentiate the plots. So how to vary colors so that every temp1 to temp 20 will have different color or shade
Dyuman Joshi
Dyuman Joshi 2022년 9월 12일
You can use rand() (which I guess is the default option as well) to obtain different colors. Though there's a chance that 2 or more colors seem similar, for eg -
x=0:0.01:10;
for i=1:20
plot(x,sin(x).^i,'Color',rand(1,3))
z{i}=sprintf('sin^{%d} x', i);
hold on
end
legend(z,'Location', 'best','FontSize',6 , 'NumColumns',4)
%11-13, 16-18 look similar
Or you can define particular array for it, to distinguish better in between colors.
A random example -
y = (dec2base(0:19,3)-'0')/2;
figure
for i=1:20
plot(x,cos(x).^i,'Color',y(i,:))
z{i}=sprintf('cos^{%d} x', i);
hold on
end
legend(z,'Location', 'best','FontSize',6 , 'NumColumns',4)

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

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by