Why the legend shows error ?
5 views (last 30 days)
Show older comments
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]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1268695/image.png)
How can wee solve it?
2 Comments
Answers (2)
dpb
on 19 Jan 2023
Moved: dpb
on 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.
Walter Roberson
on 20 Jan 2023
Moved: Walter Roberson
on 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.
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!