Azzera filtri
Azzera filtri

legend for multiple plots

2 visualizzazioni (ultimi 30 giorni)
nadia naji
nadia naji il 31 Mag 2022
Commentato: Voss il 31 Mag 2022
Hello,
I have 4 matrix with 6 rows and 15 columns. I want to plot all these matrix in on plot. for this aim I used the followin code:
x=1:15
plot(x,res540,'-r')
hold on
plot(x,res720,'--b');
plot(x,res900,'-.g');
plot(x,res1080,':k','LineWidth',0.2);
now I need to put a legend for above code in one plot. but each method I used for it does not work or all legend has same color and style. what should I do for this? Thanks

Risposta accettata

Voss
Voss il 31 Mag 2022
% some matrices:
res540 = rand(6,15);
res720 = 1+rand(6,15);
res900 = 2+rand(6,15);
res1080 = 3+rand(6,15);
% store the line handles returned from plot;
% each plot call returns 6 lines
h = zeros(6,4);
x=1:15;
h(:,1) = plot(x,res540,'-r');
hold on
h(:,2) = plot(x,res720,'--b');
h(:,3) = plot(x,res900,'-.g');
h(:,4) = plot(x,res1080,':k','LineWidth',0.2);
% make a legend for the 1st line returned from
% each plot call (i.e., first row of h)
legend(h(1,:),{'540' '720' '900' '1080'})
  2 Commenti
nadia naji
nadia naji il 31 Mag 2022
Great. Thank you.
Voss
Voss il 31 Mag 2022
You're welcome!

Accedi per commentare.

Più risposte (1)

Bjorn Gustavsson
Bjorn Gustavsson il 31 Mag 2022
Use the outputs from plot to explicitly control what is show in the legend:
x=1:15
ph1 = plot(x,res540,'-r');
hold on
ph2 = plot(x,res720,'--b');
ph3 = plot(x,res900,'-.g');
ph4 = plot(x,res1080,':k','LineWidth',0.2);
legend([ph1(1),ph2(1),ph3(1),ph4(1)],'t1','t2','t3','t4')
HTH

Prodotti


Release

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by