Matlab legend wont match graph

4 visualizzazioni (ultimi 30 giorni)
Rhiannon Elliott
Rhiannon Elliott il 2 Dic 2020
Commentato: Star Strider il 2 Dic 2020
Hi, I have read some of the previous solutions on this problem but they so far haven't worked for me. I cannot get the legend to replicate the colours of the graph itself. A solution I saw on another thread said I should make sure the whole matrix is being plotted, by incorporating (1,:) afterwards. This didn't work for me as it meant some lines wouldn't plot at all.
Any suggestions would be greatly appreciated!
clear
close all
clc
M = [30,10000,6,230]; %Array containing system masses (kg)
D = [5, 25, 0.5, 2]; %Array containing mission durations (years)
powerP = [200, 4000, 250, 400]; %Array containg peak power values (W)
M_ps = 0.15.*M; %Power system mass available (kg)
powerSpec = powerP./M_ps; %Specific power of each mission (W/kg)
t=linspace(0,25,1000);
powerSpec_SC1 = zeros(length(t));
powerSpec_SC1 = zeros(length(t));
powerSpec_RTG1 = zeros(length(t));
powerSpec_RTG2 = zeros(length(t));
for n=1:length(t);
T=t(n);
powerSpec_SC1(n)=37*(1-0.15).^T;
powerSpec_SC2(n)=49*(1-0.1).^T;
powerSpec_RTG1(n)=98000*exp((-log(2)*T)/0.411);
powerSpec_RTG2(n)=240*exp((-log(2)*T)/28.3);
end
figure
h1=semilogy(t,powerSpec_SC1,'r');
hold on
h2=semilogy(t,powerSpec_SC2,'b');
hold on
h3=semilogy(t,powerSpec_RTG1,'k');
hold on
h4=semilogy(t,powerSpec_RTG2,'k');
hold on
h5=plot(5,200,'ro','LineWidth',2);
hold on
h6=plot(25,4000,'go','LineWidth',2);
hold on
h7=plot(0.5,250,'bo','LineWidth',2);
hold on
h8=plot(2,400,'ko','LineWidth',2);
xlabel('Time (years)');
ylabel('Specific Power (W/kg)');
title('Specifc Power over time');
set(gca,'ylim',[1 1000000])
legend([h1, h2, h3, h4, h5, h6, h7, h8],'Solar Array 1','Solar Array 2','RTG1','RTG2','Mission 1','Mission 2','Mission 3','Mission 4')
hold off

Risposta accettata

Star Strider
Star Strider il 2 Dic 2020
See if this slight modification does what you want:
legend([h1(1), h2(1), h3(1), h4(1), h5(1), h6(1), h7(1), h8(1)],'Solar Array 1','Solar Array 2','RTG1','RTG2','Mission 1','Mission 2','Mission 3','Mission 4')
.
  2 Commenti
Rhiannon Elliott
Rhiannon Elliott il 2 Dic 2020
Thank you so much that worked perfectly!
Star Strider
Star Strider il 2 Dic 2020
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by