Scatter plot visualizes the same points in matlab

I have the following problem: I need to build a data scatter chart. Everything is fine, but there are some duplicate data:

x = [11, 10, 3, 8, 2, 6, 2, 3, 3, 2, 3, 2, 3, 2, 2, 2, 3, 3, 2, 2]; y = [29, 14, 28, 19, 25, 21, 27, 15, 24, 23, 23, 18, 0, 26, 11, 27, 23, 30, 30, 25]; 

You can see that there are two elements with (2, 25); (2,27); (3,24); Therefore, if to create this data with regular scatter(x,y) , I lose this information: enter image description here

The way out of this that I found is to use the undocumented 'jitter' parameter

 scatter(x,y, 'jitter','on', 'jitterAmount', 0.06); 

But I do not like the forecast: enter image description here

What I was trying to achieve is the following:

enter image description here

Where the number of duplicates is next to the point (if the number is greater than 1) or may be inside the point.

Any idea how to achieve this?

+6
source share
1 answer

You can do this quite easily, cut it into two parts:

First you will need to identify the unique 2d points and count them. This is what we have unique and accumarray for. Read the documentation if you do not immediately understand what they are doing and what results they have:

 x = [11 10 3 8 2 6 2 3 3 2 3 2 3 2 2 2 3 3 2 2]; y = [29 14 28 19 25 21 27 15 24 23 23 18 0 26 11 27 23 30 30 25]; A=[x' y']; [Auniq,~,IC] = unique(A,'rows'); cnt = accumarray(IC,1); 

Now each Auniq line contains unique 2d points, and cnt contains the number of occurrences of each of these points:

 >> [cnt Auniq] ans = 1 2 11 1 2 18 1 2 23 2 2 25 1 2 26 ...etc 

There are many possibilities to display the number of inputs. As you already mentioned, you can put numbers inside / next to scatter markers, other parameters are color coding, marker size, ... let it all, you can also combine it!

The number next to the marker

 scatter(Auniq(:,1), Auniq(:,2)); for ii=1:numel(cnt) if cnt(ii)>1 text(Auniq(ii,1)+0.2,Auniq(ii,2),num2str(cnt(ii)), ... 'HorizontalAlignment','left', ... 'VerticalAlignment','middle', ... 'FontSize', 6); end end xlim([1 11]);ylim([0 30]); 

enter image description here

Number inside marker

 scatter(Auniq(:,1), Auniq(:,2), (6+2*(cnt>1)).^2); % make the ones where we'll put a number inside a bit bigger for ii=1:numel(cnt) if cnt(ii)>1 text(Auniq(ii,1),Auniq(ii,2),num2str(cnt(ii)), ... 'HorizontalAlignment','center', ... 'VerticalAlignment','middle', ... 'FontSize', 6); end end 

as you can see, I very simply increased the size of the markers by the scatter function itself.

enter image description here

Color coding

 scatter(Auniq(:,1), Auniq(:,2), [], cnt); colormap(jet(max(cnt))); % just for the looks of it 

enter image description here

after which you can add a colorbar or legend to indicate the number of occurrences in color.

+7
source

All Articles