필터 지우기
필터 지우기

Adding variable values into legend

조회 수: 710 (최근 30일)
tyler tuokko
tyler tuokko 2019년 3월 17일
답변: Shubhankar 2023년 4월 25일
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

채택된 답변

Star Strider
Star Strider 2019년 3월 17일
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
tyler tuokko
tyler tuokko 2019년 3월 17일
thanks but i still want to keep the other legend and it wont let me make 2 of them. but i think i found an okay way of doing it by adding a blank subplot, turning the axis off and then adding a text box into the middle of the blank plot.
figure(1)
subplot(1,1,1)
title('Line Plot of Intensity for all Attenuated Rays')
xlabel('Y Position of Ray Plot Diagram (Detectors)')
ylabel('Relative Intensity')
plot(Y,I,'o-','linewidth',1,'markersize',0.1,'markerfacecolor','g');
hold on
legend(sprintfc('pol angle %d degrees',polAngSet),'Location','northwestoutside')
title(legend,'Changing Polerization Angle')
figure(1)
p1 = subplot(1,3,1);
axis off
ht = text(0, 0.5, { "PARAMETERS"," ", "Number of Rays = "+r, "Lazer Distance = " + dlaser,"Rad Face = " + radFaceSet(1),"h = "+h, "k = "+k}, 'EdgeColor', 'k');
Star Strider
Star Strider 2019년 3월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Da Bu
Da Bu 2021년 4월 22일
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
TANMOY BAIRAGI
TANMOY BAIRAGI 2022년 1월 10일
That's a great technique, thanks a lot.
Michael O'Brien
Michael O'Brien 2023년 3월 13일
you beautiful human being, you x

댓글을 달려면 로그인하십시오.


Shubhankar
Shubhankar 2023년 4월 25일
Hi Da Bu,
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?

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by