How can i add legend to the figure in matlab?

3 次查看(过去 30 天)
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 个评论
Paul Hoffrichter
Paul Hoffrichter 2021-1-22
If you post some code to illustrate the problem, that may be useful to others.
Sravani Kurma
Sravani Kurma 2021-1-22
编辑:Sravani Kurma 2021-1-22
%% 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 ?

请先登录,再进行评论。

回答(1 个)

Bjorn Gustavsson
Bjorn Gustavsson 2021-1-22
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 个评论
Sravani Kurma
Sravani Kurma 2021-1-22
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 2021-1-22
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

请先登录,再进行评论。

类别

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