Matlab 2015a: how can I adjust the markers size in the legend?
2 次查看(过去 30 天)
显示 更早的评论
Hi, I am using scatter function in matlab 2015a and whenever I create a legend for my figures, the marker size is always smaller than on the plot itself. I have found no clear way of fixing this while searching around here.
Anyone can help?
Thanks
0 个评论
回答(2 个)
Walter Roberson
2015-8-21
We do not seem to have found a way to do that as yet. See http://uk.mathworks.com/matlabcentral/answers/97779-why-is-the-marker-size-in-the-legend-of-a-scatter-plot-not-equal-to-the-marker-size-in-the-plot
David Rosenbaum
2017-9-15
编辑:Walter Roberson
2017-9-16
You can solve this problem by plotting your points as you would like them to appear in the legend, then call for the legend, then replot the points in the way you'd like them to appear, having set legend 'Autoupdate' to 'off'. Finally, if the finally plotted point sizes are smaller than the originally plotted point sizes, you should first "erase" the points by showing them in a color that matches the background (typically white), and then replot them. The code in which I use this has some added material that might be useful.
close all
clear all
commandwindow
clc
figure
hold on
xlim_offset=0;
ms=14; % markersize
x=[1:101];
alpha=[-.5 -1];
max_salary=10;
fs=12;
for ai=1:size(alpha,2)
y=max_salary*x.^alpha(ai);
if ai==1
plot(x,y,'b.-','markersize', ms, 'displayname','Company A');
else
plot(x,y,'r.-','markersize', ms, 'displayname','Company B');
end
end
legend('show','location','northeast')
get(legend)
set(legend,'FontSize',10.5,'color','w')
set(legend,'AutoUpdate','off')
xlabel('Income Rank')
ylabel('Salary (thousands of dollars)')
set(gca,'xtick',[1:10:101])
set(gca,'xticklabel',[1:10:101])
xlim([min(x)-xlim_offset max(x)+xlim_offset])
% Now re-plot with new marker size
new_ms=ms-6;
for ai=1:size(alpha,2)
y=max_salary*x.^alpha(ai);
if ai==1
if new_ms<ms
plot(x,y,'w.-','markersize', ms);
end
plot(x,y,'b.-','markersize', new_ms);
else
if new_ms<ms
plot(x,y,'w.-','markersize', ms);
end
plot(x,y,'r.-','markersize', new_ms);
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!