How do I specify the size of the markers created by the SCATTER plot in units proportional to the data being plotted in MATLAB 7.6 (R2008a)?
86 views (last 30 days)
MathWorks Support Team on 18 Oct 2013
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.
More Answers (2)
Matthias Wurm on 17 Feb 2016
I think there's still a minor bug in the answer offered by the support team. In my case I set the axis style to 'equal'. To get the desired result, I suggest an improved scaling factor:
This gives reasonable results if the plotting area was limited either of the horizontal or vertical boundaries of the figure. Additional advice: I use a similar code in a callback function, which runs when I change the figure size (triggered by 'SizeChangedFcn'). Here I accidently found out that I sometimes get a strange/wrong resize behaviour if I do not ask for the 'Position' property before changing the 'Units'. In other words: if I skip line 2 ('ax.Position', which has obviously no function), then resizing the figure from small to large, does not work. This seems to be a bug.