Why the legend shows error ?

조회 수: 4 (최근 30일)
Wiqas Ahmad
Wiqas Ahmad 2023년 1월 19일
이동: Walter Roberson 2023년 1월 20일
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
Dyuman Joshi
Dyuman Joshi 2023년 1월 19일
I think you need to assign the legend to the variable 'leg'
Wiqas Ahmad
Wiqas Ahmad 2023년 1월 20일
I have tried it but of no use.

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

답변 (2개)

dpb
dpb 2023년 1월 19일
이동: dpb 2023년 1월 19일
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
Wiqas Ahmad
Wiqas Ahmad 2023년 1월 20일
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]);

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


Walter Roberson
Walter Roberson 2023년 1월 20일
이동: Walter Roberson 2023년 1월 20일
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
Wiqas Ahmad
Wiqas Ahmad 2023년 1월 20일
이동: Walter Roberson 2023년 1월 20일
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.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by