Why the legend shows error ?

5 次查看(过去 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 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by