Plots and legend don't match
2 次查看(过去 30 天)
显示 更早的评论
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
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
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).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!