MATLAB Answers

How can i add legend to the figure in matlab?

22 views (last 30 days)
Sravani Kurma
Sravani Kurma on 22 Jan 2021
Commented: Bjorn Gustavsson on 22 Jan 2021
I have plotted a figure with 9 plots in it and in legend i have given 9 names to display. All good upto now.
But after some editing on the actual figure like in terms of axis setting and figure settings, i could find that 2 out of 9 of legend information is missing in the legend box on the figure, i tried setproperty and going to string but not getting what to do after that. It is asking to enter expressions. so what should i enter to get my legend information back ?

  2 Comments

Paul Hoffrichter
Paul Hoffrichter on 22 Jan 2021
If you post some code to illustrate the problem, that may be useful to others.
Sravani Kurma
Sravani Kurma on 22 Jan 2021
%% Plotting
j = 0:2:40;
grid on;
semilogy(j,OP1_e1,'g-o','linewidth',0.5);
hold on
semilogy(j,OP2_e1,'g-*','linewidth',0.5);
hold on
semilogy(j,OP3_e1,'g-d','linewidth',0.5);
hold on
semilogy(j,OP1_e2,'r-o','linewidth',0.5);
hold on
semilogy(j,OP2_e2,'r-*','linewidth',0.5);
hold on
semilogy(j,OP3_e2,'r-d','linewidth',0.5);
hold on
semilogy(j,OP1_e3,'b-o','linewidth',0.5);
hold on
semilogy(j,OP2_e3,'b-*','linewidth',0.5);
hold on
semilogy(j,OP3_e3,'b-d','linewidth',0.5);
hold on
title('OP Vs Avg SNR with r0=2 bits/sec/hz');
ylabel('OP');
xlabel('Es/No');
hold on;
legend('simulated with N = 1 for \eta = 0.15','simulated with N= 2 \eta = 0.15','simulated with N = 5 \eta = 0.15','simulated with N = 1 for \eta = 0.45','simulated with N= 2 \eta = 0.45','simulated with N = 5 \eta = 0.45','simulated with N = 1 for \eta = 0.85','simulated with N= 2 \eta = 0.85','simulated with N = 5 \eta = 0.85');
hold on;
problem here is i have lost blue diamond line and green diamond line info in box. can someone say me how to add it ?

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 22 Jan 2021
It is often preferable to add the legend after the other adjustments. I often find that legends and colorbars doesn't perfectly adjust to changes in axes size and position, and the only solution I've found is to add them last. Maybe that is enough to fix your problem.
HTH

  3 Comments

Bjorn Gustavsson
Bjorn Gustavsson on 22 Jan 2021
Try to store the line-handles that your plotting-functions return:
ph(1) = semilogy(j,OP1_e1,'g-o','linewidth',0.5); % etc...
Then you have more control over the legend:
legend(ph(1:9),'your','legend','texts')
Sravani Kurma
Sravani Kurma on 22 Jan 2021
Thank you so much for your response.
Here, i wanted to edit the figure by clicking the box----> properties --->string----> setting the inputs
Actually, i have mistakenly removed by using this method. so, there is an option in it to enter the expression soon after we open string , my doubt is can we enter something over there to get that legend info back for the two missing lines?
Bjorn Gustavsson
Bjorn Gustavsson on 22 Jan 2021
This works perfectly fine in matlab2020a:
OP = exp(randn(9,numel(j)));
ph = semilogy(j,OP);
clf
for i1 = 1:size(OP,1),
ph(i1) = semilogy(j,OP(i1,:));
hold on
end
set(ph(1:3),'color','b')
set(ph(4:6),'color','g')
set(ph(7:9),'color','r')
set(ph(1:3:end),'Marker','*')
set(ph(2:3:end),'Marker','o')
set(ph(3:3:end),'Marker','d')
xlabel('Es/No')
ylabel('OP');
title('OP Vs Avg SNR with r0=2 bits/sec/hz');
legend(ph,...
'simulated with N = 1 for \eta = 0.15',...
'simulated with N= 2 \eta = 0.15',...
'simulated with N = 5 \eta = 0.15',...
'simulated with N = 1 for \eta = 0.45',...
'simulated with N= 2 \eta = 0.45',...
'simulated with N = 5 \eta = 0.45',...
'simulated with N = 1 for \eta = 0.85',...
'simulated with N= 2 \eta = 0.85',...
'simulated with N = 5 \eta = 0.85');
If you've removed some parts of the legend by interactively editing the figure, then I don't know what's possible. In that case I figure it is far less fuss to simply replot the figure?
HTH

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!