How to add serialized legends to a chart in MATLAB

I have a database with n wind speeds, then I classify those speeds in a variable called Nclassesdevento (in the example it was classified in 10 classes).
I am making a graph of the nclasses and I need the caption for those classes. Includes the Legend function in the for loop because I expected the legend to return for each class after each interaction but it didn't work.
In other words, it is necessary to include the legend for each respective class
Can someone help me?
figure(3)
subplot(2,1,1);
hold on
xaxa=0;
for kkkk=1:Nclassesdevento
plot(ventoclass(kkkk,1:nptv(kkkk)))
xaxa=xaxa+1
legenda(xaxa)=legend(['Classe ', num2str(kkkk)])
end
hold off
xlabel('Ocorrência dos ventos');
ylabel('Velocidade do Vento média (m/s)');
title(['Velocidade média do Vento SCADA categorizada em ', num2str(Nclassesdevento()),' classes'])
subplot(2,1,2);
histfit(ventoutil,50,'wbl')
xlabel('Velocidade media do vento (m/s)');
ylabel('numero de Ocorrência');
title('Histograma da velocidade media do vento e curva de Weibull');
legend({'Vento','Weibull'});

Réponses (2)

Star Strider
Star Strider le 8 Juin 2020
Modifié(e) : Star Strider le 8 Juin 2020

0 votes

See if the Line Properties name-value pair DisplayName will do what you want. Specifically see: Specify Legend Labels During Plotting Commands for an illustration.

1 commentaire

My pleasure.
I cannot run your code without the appropriate data.
Try this:
plot (ventoclass (kkkk, 1: nptv (kkkk)), 'DisplayName',sprintf('Classe %2d',kkkk))
That worked when I tried it with this test code:
figure
hold on
for kkkk = 1:3;
plot(rand(1,2), rand (1,2), 'DisplayName',sprintf('Classe %2d',kkkk))
end
hold off
legend
.

Connectez-vous pour commenter.

First of all I would like to thank you for the answer. I consulted DisplayName and managed to display the caption, but I still have a problem because I could include a text before the class number, because the class is being displayed by
plot (ventoclass (kkkk, 1: nptv (kkkk)), 'DisplayName', num2str ( kkkk))
1
....
10
which can be seen in the figure below. I would like to see:
Class 1
......
Class 10
I tried this command but it didn't work
plot (ventoclass (kkkk, 1: nptv (kkkk)), 'DisplayName','Classe ', num2str ( kkkk))
Can anybody help me?
see the complete code
figure(3)%Plota os ventos classificados
subplot1=subplot(2,1,1);
hold on
for kkkk=1:Nclassesdevento
plot(ventoclass(kkkk,1:nptv(kkkk)),'DisplayName',num2str(kkkk))
end
hold off
xlabel('Ocorrência dos ventos');
ylabel('Velocidade do Vento média (m/s)');
title(['Velocidade média do Vento SCADA categorizada em ', num2str(Nclassesdevento()),' classes'])
legend1 = legend(subplot1,'show');
set(legend1,'Location','best');
subplot(2,1,2);
........

Catégories

En savoir plus sur Printing and Saving dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by