Tak
Tak

Reputation: 3616

Spacing between markers in scatter plot Matlab

I have 6 scatter plots in one figure as shown below.

A=rand(10,2); 
B=rand(10,2); 
C=rand(10,2);
figure();
hold on;
scatter( 1:10, A(:,1), 'r*');
scatter( 1:10, A(:,2), 'ro'); 
scatter( 1:10, B(:,1), 'b*'); 
scatter( 1:10, B(:,2), 'bo'); 
scatter( 1:10, C(:,1), 'g*'); 
scatter( 1:10, C(:,2), 'go'); 

I wonder if I can make some spacing between the points so that no two points overlay each other. So for example, on the value 1 of x-axis there will be 6 different points (one representing each scatter plot), so I'm wondering how I can make each one has its one vertical lane?

So if I used stem instead of using scatter you'll see that the stem lines overlay and it makes it harder to view the plot as shown below in the screenshot. So for every xtick it has 6 stems, and I'm wondering if there is anyway I can shift 5 of the 6 stems a little bit so that they all appear.

So this is a screenshot of my current stems overlaying:

enter image description here

Upvotes: 0

Views: 900

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112669

Apply a small displacement to the x values?

x = 1:10;
y1 = rand(1,10);
y2 = rand(1,10);
y3 = rand(1,10); %// example data
delta = .004; %// displacement step, relative to x range
x_range = max(x)-min(x);
Delta = range*delta;
hold all
stem(x-Delta, y1, 'o');
stem(x,       y2, '*');
stem(x+Delta, y3, 's');

Example:

enter image description here

Upvotes: 1

phyrox
phyrox

Reputation: 2449

My first answer would be that it is not possible. If you have 2 dimensional data and some data overlaps other data, you cannot modify data to improve the visualization!

If you need to improve it, you can use a third dimension:

figure();
hold on;
scatter3( 1:10, A(:,1), 1*ones(1,10), 'r*');
scatter3( 1:10, A(:,2), 2*ones(1,10), 'ro'); 
scatter3( 1:10, B(:,1), 3*ones(1,10), 'b*'); 
scatter3( 1:10, B(:,2), 4*ones(1,10), 'bo'); 
scatter3( 1:10, C(:,1), 5*ones(1,10), 'g*'); 
scatter3( 1:10, C(:,2), 6*ones(1,10), 'go'); 

In the Z axis, you have data separated by "lanes" as you asked. So now is up to you and your users to select the best visualization planes.

A second straighfordward solution to split it lines is to divide all the scatter plots in different subplots:

figure();
subplot(6,1,1)
scatter( 1:10, A(:,1), 'r*');
subplot(6,1,2)
scatter( 1:10, A(:,2), 'ro'); 
subplot(6,1,3)
scatter( 1:10, B(:,1), 'b*'); 
subplot(6,1,4)
scatter( 1:10, B(:,2), 'bo'); 
subplot(6,1,5)
scatter( 1:10, C(:,1), 'g*'); 
subplot(6,1,6)
scatter( 1:10, C(:,2), 'go'); 

Upvotes: 0

Related Questions