1
votes

Very irritating there is enough size on the graph to widen the legend but it does not resize automatically to the content. Anyone a suggestion on how to fix this? (latex interpreter set to legend)

Error marked in red where legend data falls out of the legend box

enter image description here

enter image description here

Fully minimal working example of my problem:

window=figure('Visible','on');
axes1 = axes('Parent',window);
grid(axes1,'on');
hold(axes1,'all');
set(window, 'Position', [0 0 1000 1000]);

x=rand(1000,1);
y=rand(1000,1);
companyMap='companyname of datapoint'

%draw scatter in window
scatter(x,y,'fill','MarkerFaceColor',[0,0,1],'MarkerEdgeColor',[0,0,1],'Marker','.','SizeData',30,'Parent',axes1,'DisplayName',sprintf('%s Datapoint',strrep(companyMap,'&','\&')));
linFit = fitlm(x,y);
dispName = sprintf('Linear fit: $y=%.3f x+ %.3f$, $R^{2}=%.3f$',linFit.Coefficients.Estimate(2),linFit.Coefficients.Estimate(1),linFit.Rsquared.Ordinary);
if linFit.Coefficients.Estimate(1) < 0
    dispName = strrep(dispName,'+','');
end
%lsline('linewidth',2,'Parent',axes1,'DisplayName',dispName);
line(x,linFit.Fitted,'Parent',axes1,'Tag','lsline','Color',[0 0 0],'DisplayName',dispName,'linewidth',2);
legend1 = legend(axes1,'show');
set(legend1,'Interpreter','latex');
%set(legend1, 'FontSize',7);
set(legend1,'Location','Best');

% Create title
title( { 'tit' }, 'FontSize',20);
% Create xlabel
xlabel( { 'x' } , 'FontSize',16, 'Interpreter','LaTex');
% Create ylabel
ylabel( { 'y' } , 'FontSize',16, 'Interpreter','LaTex');
1
As you haven't provided any code to help us guessing what is wrong, I assume you actually used pen an paper. I'd recommend to use a rubber, remove the overflown line by rubbing, and draw a new one. - Robert Seifert
Can you post just the relevant part of the code? And the code should be runnable. See stackoverflow.com/help/mcve - Luis Mendo
For me your code works as it is supposed to do. - Robert Seifert
Break the line with '\n' in sprintf. - Yvon
[hleg1, hobj1] = legend(axes1,'show'); textobj = findobj(hobj1, 'type', 'text'); set(textobj, 'Interpreter', 'latex'); set(hleg1,'position',[.2,.2,.8,.8])mathworks.com/matlabcentral/answers/… - Yvon

1 Answers

0
votes

Working example with solution set(legend1,'position',[0.5 0.8 0.4 0.1])

window=figure('Visible','on');
axes1 = axes('Parent',window);
grid(axes1,'on');
hold(axes1,'all');
set(window, 'Position', [0 0 1000 1000]);

x=rand(1000,1);
y=rand(1000,1);
companyMap='companyname of datapoint'

%draw scatter in window
scatter(x,y,'fill','MarkerFaceColor',[0,0,1],'MarkerEdgeColor',[0,0,1],'Marker','.','SizeData',30,'Parent',axes1,'DisplayName',sprintf('%s Datapoint',strrep(companyMap,'&','\&')));
linFit = fitlm(x,y);
dispName = sprintf('Linear fit: $y=%.3f x+ %.3f$, $R^{2}=%.3f$',linFit.Coefficients.Estimate(2),linFit.Coefficients.Estimate(1),linFit.Rsquared.Ordinary);
if linFit.Coefficients.Estimate(1) < 0
    dispName = strrep(dispName,'+','');
end
%lsline('linewidth',2,'Parent',axes1,'DisplayName',dispName);
line(x,linFit.Fitted,'Parent',axes1,'Tag','lsline','Color',[0 0 0],'DisplayName',dispName,'linewidth',2);
legend1 = legend(axes1,'show');
set(legend1,'Interpreter','latex');
%set(legend1, 'FontSize',7);
set(legend1,'position',[0.5 0.8 0.4 0.1])

% Create title
title( { 'tit' }, 'FontSize',20);
% Create xlabel
xlabel( { 'x' } , 'FontSize',16, 'Interpreter','LaTex');
% Create ylabel
ylabel( { 'y' } , 'FontSize',16, 'Interpreter','LaTex');