The ability to automatically equalize the marker size of the legend and plot markers when using the "scatter" function is not available in MATLAB 7.9 (R2009b).
You may use either of the following workarounds to create a scatter plot in which the legend markers have the same size as the plot markers:
1. Manually set marker sizes in the legend
- For MATLAB R2014a and prior releases, manually set the marker size of the patch objects in the legend. Note that the marker area input to the "scatter" function is specified in square points, whereas the 'MarkerSize' property of a patch object is given in points:
M = findobj(h,'type','patch')
set(M,'MarkerSize', sqrt(150))
- For MATLAB R2014b to MATLAB R2024b, use the following syntax. This is due to a change in the legend graphics object hierarchy. This also enables setting each marker size in the legend, if different sizes are desired to match the different sizes of points in a figure.
[h, icons] = legend('Circle', 'Plus', 'X', 'Location', 'NorthEast');
icons(4).Children.MarkerSize = sqrt(150);
icons(5).Children.MarkerSize = sqrt(150);
icons(6).Children.MarkerSize = sqrt(150);
Note: support for multiple outputs of the "legend" function is still provided for MATLAB R2024b, but it is expected to be removed in a future release of MATLAB.
2. Use the "plot" function instead of the "scatter" function:
figure; hold on
plot(1,1,'ko', 'MarkerSize', 12)
plot(1,2,'k+', 'MarkerSize', 12)
plot(2,1,'kx', 'MarkerSize', 12)
h = legend('Circle', 'Plus', 'X', 'Location', 'NorthEast');
set(h, 'FontSize', 14)
axis([0 3 0 3])
Another possible approach with the "plot" function is to use the following syntax. This also enables setting each marker size in the legend, if different sizes are desired to match the different sizes of points in a figure.
[h, icons] = legend('Circle', 'Plus', 'X', 'Location', 'NorthEast');
icons(4).MarkerSize = sqrt(150);
icons(5).MarkerSize = sqrt(150);
icons(6).MarkerSize = sqrt(150);