Adding variable values into legend
1 026 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
tyler tuokko
le 17 Mar 2019
Réponse apportée : Shubhankar
le 25 Avr 2023
Hello, i am looping over different valuesthat effect the plot and i can get a legend that shows how these values relate to the lines shown in the plot. However i would also like to add another legend or something similar that will show some parameters that are manually set in the program.
ie: If gravity would effect the shape of my plot and i set gravity to 10 i would like to be able to have it show on the firgure that "10" was the value of gravity used for run of the plot
thanks
0 commentaires
Réponse acceptée
Star Strider
le 17 Mar 2019
There are several ways to do that.
One is:
figure
plot((1:100), rand(1, 100).*sin(1:100))
gravity = 10;
gravstr = sprintf('Gravitational Acceleration is %.1f today',gravity);
legend(gravstr)
Experiment to get the result you want.
2 commentaires
Star Strider
le 17 Mar 2019
As always, my pleasure.
I don’t entirely understand what you’re doing.
The legend call wants one plotted object (line, marker, etc.) for each item in it, and will complain if there are more legend items than plotted objects. In that context, the text call is likely your best option. I’ve used an ‘empty’ subplot position for a legend location from time to time, using the 'Position' property to place it.
Plus de réponses (2)
Da Bu
le 22 Avr 2021
Maybe this code can help you:
x = 1:.1:3;
colors = jet(5);
for n = 1:5
plot(x,sin(x)*n,'color',colors(n,:),'DisplayName',strcat('gravity=',num2str(n)))
hold on;
end
legend('show')
2 commentaires
Shubhankar
le 25 Avr 2023
your codes works perfect, but could you be kind enough to give the explanation of legend('show')?
and what exactly you are doing in the plot command?
0 commentaires
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!