Why the legend shows error ?

4 vues (au cours des 30 derniers jours)
Wiqas Ahmad
Wiqas Ahmad le 19 Jan 2023
I'm getting the following error with the code:
figure;
hold on
[ax,h1,h2]=plotyy(distance,G.*(distance.^-2),distance,G);
set(ax,{'ycolor'},{'k';'k'},'Fontsize',16);
% set(ax(2),'ytick',[0:0.2:1.4],'fontsize',16);
plot(distance,(distance.^-2),'k--','LineWidth',2);
h1.Color = 'b';
h2.Color = 'r';
h1.LineWidth=2;
h2.LineWidth=2;
xlabel('\fontname{Arial}Range(m)');
set(gca,'xlim',[0 200],'xtick',[0:50:200]);
ylabel('\fontname{Arial}Backscattering signal intensity(unit)');
set(gca,'ylim',[0.*10.^-3 1.5.*10.^-3],'ytick',[0:.5:1.5].*10.^-3);
set(gca,'color','w','Fontsize',16,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','boxstyle','full','Fontname','Arial','Fontsmoothing','on');
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
yyaxis right
ylabel('Overlap function value');
set(gca,'ylim',[0 1.4],'ytick',[0:0.2:1.4]);
set(gca,'Ycolor','k');
legend('Signal intensity (unit)','1/z^2','Overlap function','fontsize',12,'location','Northeast','orientation','vertical','numcolumns',1,...
'box','on');
leg.ItemTokenSize = [15,18];
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0.3 0.3 .3 .45],'innerposition',[.11 .15 .15 .75]);
How can wee solve it?
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 19 Jan 2023
I think you need to assign the legend to the variable 'leg'
Wiqas Ahmad
Wiqas Ahmad le 20 Jan 2023
I have tried it but of no use.

Connectez-vous pour commenter.

Réponses (2)

dpb
dpb le 19 Jan 2023
Déplacé(e) : dpb le 19 Jan 2023
We don't have the data to try to reproduce, but I expect the problem is you're trying to mix the old plotyy and the new yyaxis syntax and they don't play well together.
Stick with one or the other and it'll probably work....if use all yyaxis calls, you'll get a legend object created by default; if you were then to assign the 'DisplayName' property to the line when plotted, they would show up automagically; then you could retrieve the legend handle (it's a child of the figure, not the axes) and make whatever global changes wanted there.
  1 commentaire
Wiqas Ahmad
Wiqas Ahmad le 20 Jan 2023
I have removed the yyaxis syntax but still the problem persist. I also used handles to each line but none of them works. The code now is:
figure;
hold on
[ax,h1,h2]=plotyy(distance,G.*(distance.^-2),distance,G);
set(ax,{'ycolor'},{'k';'k'},'Fontsize',16);
plot(distance,(distance.^-2),'k--','LineWidth',2);
h1.Color = 'b';
h2.Color = 'r';
h1.LineWidth=2;
h2.LineWidth=2;
xlabel('\fontname{Arial}Range(m)');
set(gca,'xlim',[0 200],'xtick',[0:50:200]);
ylabel(ax(1),'\fontname{Arial}Backscattering signal intensity(unit)');
set(ax(1),'ylim',[0.*10.^-3 1.5.*10.^-3],'ytick',[0:.5:1.5].*10.^-3);
ylabel(ax(2),'\fontname{Arial}Overlap function value');
set(ax(2),'ylim',[0 1.4],'ytick',[0:0.2:1.4]);
set(ax(2),'Ycolor','k');
set(gca,'color','w','Fontsize',16,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','boxstyle','full','Fontname','Arial','Fontsmoothing','on');
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
leg=legend('Signal intensity (unit)','1/z^2','Overlap function','fontsize',12);
leg.ItemTokenSize = [15,18];
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0.3 0.3 .3 .45],'innerposition',[.11 .15 .15 .75]);

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 20 Jan 2023
Déplacé(e) : Walter Roberson le 20 Jan 2023
legend({'Signal intensity (unit)','1/z^2','Overlap function'}, 'fontsize', 12, 'location', 'Northeast', 'orientation', 'vertical', 'numcolumns', 1,...
'box', 'on');
but I wonde which MATLAB version you are using? You might possibly be using a version too early to support multiple columns.
  1 commentaire
Wiqas Ahmad
Wiqas Ahmad le 20 Jan 2023
Déplacé(e) : Walter Roberson le 20 Jan 2023
Thank you, Sir. You are right. I removed the "column" from the syntax and it works perfectly. By the way I'm using version R2017a and I think it doesn't support the multiple columns.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by