필터 지우기
필터 지우기

Plots and legend don't match

조회 수: 1 (최근 30일)
Mihir Tasgaonkar
Mihir Tasgaonkar 2022년 6월 27일
댓글: Mihir Tasgaonkar 2022년 6월 28일
Hi,
I have a FOR loop where I'm plotting two different graphs. I'm then specifying the legend entries outside the loop.
for jj=1:5
plot(app.AccTabTorqueProfile,app.Motor.(sprintf('SR%d',jj)).RPM, app.Motor.(sprintf('SR%d',jj)).Torque, app.Motor.(sprintf('SR%d',jj)).RPM, app.Motor.(sprintf('SR%d',jj)).Torque*-app.const.mot.regen_trq_fac,"Color",C{jj});
hold(app.AccTabTorqueProfile, 'on');
drawnow;
plot(app.AccTabPowerProfile,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR/1000,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR*-app.const.mot.regen_trq_fac/1000,"Color",C{ii});
hold(app.AccTabPowerProfile, 'on');
drawnow;
end
legend(app.AccTabTorqueProfile,"SR1","SR2","SR3","SR4","SR5");
legend(app.AccTabPowerProfile,"SR1","SR2","SR3","SR4","SR5");
This code gives the same colours to "SR1" and "SR2", same for "SR3" and "SR4", and a third color for "SR5". Can someone help me plot the same colors in the legend as well?
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 6월 27일
plot(app.AccTabPowerProfile,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR/1000,app.Motor.(sprintf('SR%d',ii)).RPM,app.Motor.(sprintf('SR%d',ii)).PWR*-app.const.mot.regen_trq_fac/1000,"Color",C{ii});
Is there a relationship between ii used to index here, compared to the jj that you are looping over ?

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

채택된 답변

dpb
dpb 2022년 6월 27일
편집: dpb 2022년 6월 27일
Besides Walter's note that you're using ii in the second plot instead of jj,it would be cleaner/simpler to do something like
for jj=1:5
vname="SR"+jj; % use string class overloaded plus operator
plot(app.AccTabTorqueProfile, ...
app.Motor.(vname).RPM, app.Motor.(vname).Torque, ...
app.Motor.(vname).RPM, app.Motor.(vname).Torque*-app.const.mot.regen_trq_fac,...
"Color",C{jj},'DisplayName',vname);
if jj==1,;hold(app.AccTabTorqueProfile, 'on');end
drawnow;
plot(app.AccTabPowerProfile, ...
app.Motor.(vname).RPM,app.Motor.(vname).PWR/1000, ...
app.Motor.(vname).RPM,app.Motor.(vname).PWR*-app.const.mot.regen_trq_fac/1000, ...
"Color",C{jj},'DisplayName',vname);
if jj==1,;hold(app.AccTabPowerProfile, 'on');end
drawnow;
end
where I've also assumed the ii was a typo and changed it to jj
Making that change alone would undoubtedly fix the problem by making them consistent usage of color (not to mention fixing up the data to not be the same for all for the second plot since ii isn't varying in the above code).
  댓글 수: 1
Mihir Tasgaonkar
Mihir Tasgaonkar 2022년 6월 28일
Thanks a lot for the response!
And yes, the 'ii' and 'jj' thing was a typo...

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by