How do I specify the size of the circles in the scatter plot so that the length of their diameters is equal to length of one unit on the axes?
35 次查看(过去 30 天)
显示 更早的评论
I am doing molecular dynamics simulations and plotting circles(scatter plots) with the data points I get from the simulation.My length scale is such that diameter of circle=1 unit on x and y axes.I want to know how to specify the scatter size so that it matches with the scale. please help.
h=figure('visible','off');
xlim([-25 25]);
hold on;
ylim([-25 25]);
hold on;
scatter(data(:,1),data(:,2),50);
0 个评论
回答(1 个)
Walter Roberson
2017-6-1
That is not possible. The size for scatter is always area in "points squared" (one point is 1/72 of an inch). That is an absolute size that does not change as you zoom the plot in or out, so it is not possible to control it in data units (because data units change as you zoom in or out).
You can
ax = gca;
AR = get(gca, 'dataaspectratio');
if ~isequal(AR(1:2), [1 1])
error('Units are not equal on X and Y, cannot create marker size that is one unit on both');
end
oldunits = get(ax, 'Units');
set(ax, 'Units', 'points');
pos = get(ax, 'Position'); %[X Y width height]
set(ax, 'Units', oldunits');
XL = xlim(ax);
points_per_unit = pos(3) / (XL(2) - XL(1));
marker_size = points_per_unit .^2 * pi / 4; %pi*r^2 but remember points_per_unit is d not r
7 个评论
Walter Roberson
2017-6-9
ax = gca;
Any time you are working with "current" objects like "current axes", which object is "current" can change in between lines, because the user might click. In particular, if the user is debugging, then MATLAB processes pending graphics events every time the keyboard is given control, so if the user clicks on a window to grab it to move it aside to be able to view something (like the code) then the window can accidentally become the "current". It is therefore best practice to take a copy of the "current" axes into a variable if you are going to refer to it, so that if what gca refers to changes, you still consistently refer to the original axes.
AR = get(gca, 'dataaspectratio');
if ~isequal(AR(1:2), [1 1])
error('Units are not equal on X and Y, cannot create marker size that is one unit on both');
end
axes have a dataaspectratio property that describes how many data units in one direction equal how many data units in the other direction. The calculations of area that we need to do are only correct of 1 unit horizontally is the same as 1 unit vertically, so we check to see whether that is the case and reject the situation if it is not.
oldunits = get(ax, 'Units');
set(ax, 'Units', 'points');
pos = get(ax, 'Position'); %[X Y width height]
set(ax, 'Units', oldunits');
The Position property of an axes does not have a fixed unit of measure. The unit of measure it uses is given by the Units property. The way to find out how big something is in a particular unit of measure (such as Points) is to take a copy of the current unit of measure, change to the unit of measure we are interested in, ask the size, and then switch back to the old unit of measure.
XL = xlim(ax);
points_per_unit = pos(3) / (XL(2) - XL(1));
We already established that we are using a 1:1 data aspect ratio, so the ratio for x is the same as the ratio for y, so it does not matter whether we query x or y, so we pick one of the two.
The first element of xlim is the data units of the left side of the axes; the second element of xlim is the data units of the right side of the axes. Subtract the two to find out how many data units the horizontal axes spans. The third element of Position is the width, which we have configured to be in Points. So the ratio of the width in points to the data units tells us how many points there are for each data unit.
marker_size = points_per_unit .^2 * pi / 4; %pi*r^2 but remember points_per_unit is d not r
This is a standard area = pi*r^2 = pi*(d/2)^2 after an implicit points_per_unit * 1 data_unit
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!