MATLAB Answers

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)?

187 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
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.
  2 Comments

Sign in to comment.

More Answers (2)

Matthias
Matthias on 17 Feb 2016
Hello,
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:
currentunits=ax.Units;
ax.Position;
ax.Units='Points';
axpos=ax.Position;
pba=ax.PlotBoxAspectRatio;
ax.Units=currentunits;
scale=min([axpos(3),pba(1)/pba(2)*axpos(4)])/diff(xlim);
a=markerwidth.*scale;
set(h,'SizeData',a.^2);
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.

Tags

Products


Release

R2008a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!