Reputation: 53
I want to plot multiple points in the same coordinate, but with a small offset distance, so it's possible to see all the points.
I have an N-by-M matrix example:
[0, 12, 7, 10, 0, 12;
0, 7, 7, 10, 0, 7 ;
7, 12, -3, 0, 7, 7 ;
0, 10, 4, 0, 7, 10];
I want on the x-axis to be 1:M
in this case x=[1,2,3,4,5,6]
.
The y-axis should be the colums, so at x=1
it should plot [0,0,7,0]
but this only gives me two different visible points (at [1,0]
and [1,7]
). The other points are hidden underneath the last point plotted at [1,0]
.
How is it possible to plot each of the points with a dot, and add a small offset (between -0.1 and 0.1) to the x
and y
coordinates of each dot, to
make the different dots visible?
Upvotes: 0
Views: 606
Reputation: 30046
Shifting by some small amount to increase visibility is (probably) a bad idea...
A better option would be to use concentric circles, by varying the point size using scatter
.
% Set up matrix for plotting
m = [0, 12, 7, 10, 0, 12;
0, 7, 7, 10, 0, 7 ;
7, 12, -3, 0, 7, 7 ;
0, 10, 4, 0, 7, 10];
x = 1:size(m,2);
% Unique elements in the matrix (could use tolerancing here)
u = unique(m);
% Set up sizes of each point
s = zeros(size(m));
for ii = 1:numel(u);
c = cumsum(m == u(ii),1); % Running total of matching value in columns
s = s + c.*(m == u(ii)); % Increase point size by number of points
end
s = (s*5).^2; % Scatter takes square-points size, times 5 for scale
% Plot each row
figure; hold on;
for jj = 1:size(m,1)
scatter(x, m(jj,:), s(jj,:), 'linewidth', 1.25); % Using the sizes we just made
end
hold off; grid on; xlim([0,7]);
Output:
This method has the advantage of immediately seeing "hotspots" in your data where there are many points. Also, whilst the colours are more visible, colours aren't necessary for reading the plot. This is convenient when working with greyscale print-outs or colleagues with colour-blindness.
A similar but alternative method would be to give each row its own size, then the size/colour is consistent. You just don't get the "hotspots" and you may get a large circle with no smaller ones inside it - making it harder to see the actual data point location.
Upvotes: 3
Reputation: 1448
You want to add a little jitter? Why not do so uniformly:
mat = [0,12,7,10,0,12; 0,7,7,10,0,7; 7,12,-3,0,7,7; 0,10,4,0,7,10];
mat2 = mat' + 0.1 * repmat(1:4, [6 1]) - 0.15;
f1 = figure('Color', ones(1,3));
plot(mat2, '.', 'MarkerSize', 20);
box off;
ax1 = gca;
ax1.XTick = 1:6;
ax1.XLim = [0 7];
I prefer to only add the jitter to the y-axis so the points still line up, but if you want to add it to both the x and y axes, just do this:
xvals = repmat(1:6, [4 1])' + 0.1 * repmat(1:4, [6 1]) - 0.25;
plot(xvals, mat2, '.', 'MarkerSize', 20);
Upvotes: 0