MATLAB: How to specify the size of the markers created by the SCATTER plot in units proportional to the data being plotted in MATLAB 7.6 (R2008a)

markersizeMATLAB

I am using the SCATTER function to create a scatter plot of my data using the following syntax,
scatter(X,Y,S)
I would like to specify the marker width as 5 units on the X axis.

Best Answer

The SCATTER function expects its 'S' parameter to contain the marker area in points squared. This area corresponds to the area of a square bounding box around the marker. To make the size of the marker relative to the units of 'X' and 'Y' you will need to convert the marker width in 'normalized' units into 'point' units.
To achieve this, you can first create the scatter plot using the default marker size. You can then determine the ratio of conversion between normalized units and points given the size of the newly created axes, and finally change the "SizeData" property of the scatter plot to the appropriate value.
x = 1:50;
y = rand(1,50);
s = 5; %Marker width in units of X
h = scatter(x,y); % Create a scatter plot and return a handle to the 'hggroup' object
%Obtain the axes size (in axpos) in Points
currentunits = get(gca,'Units');
set(gca, 'Units', 'Points');
axpos = get(gca,'Position');
set(gca, 'Units', currentunits);
markerWidth = s/diff(xlim)*axpos(3); % Calculate Marker width in points
set(h, 'SizeData', markerWidth^2)
The above code uses the "Position" property of the axis to determine the size of the axis in points and the "XLim" and "YLim" properties to determine the size of the axis in normalized units. For more information on the Position, Units and other axes properties, please consult the documentation for AXES properties.