how add intensity/frequency to the scatter plot

Hi, I would like to scatter plot these two vectors: X=[0 0 1 2 0 1 1 0]; Y=[1 1 1 1 2 0 0 2]; and had the frequency of occurrence in the color or in the size of the points, something like the attached pic

 Accepted Answer

Consider the code below but also check the documentation for:
X=[0 0 1 2 0 1 1 0];
Y=[1 1 1 1 2 0 0 2];
% find the freqency vector
minVal = min(Y)
maxVal = max(Y)
vectorOfvalues = minVal:maxVal
howMany = Y'==vectorOfvalues
howManySum = sum(howMany);
% this line nicely show how the occurance of a value
% 1st row: value
% 2nd row: occurance
howManySum = [vectorOfvalues; howManySum]
% pre-allocation
occurence = zeros(1,length(Y));
for i=1:length(howManySum)
occurence(Y==howManySum(1,i)) = howManySum(2,i);
end
Y
occurence
% I've added the factor of 50 for the occurance
% so the diffrence between the dot sizes is visible
figure
scatter(X,Y,50*occurence)

6 Comments

Hi! thansk for your answer, but I got an error here: howMany = Y'==vectorOfvalues matrix dimensions must agree
  • IF you literally copied/pasted my code - it should work (see attached live script and the .pdf)
  • This error appears when you're trying to compare to row vectors or two column vectors - make sure one of the vector is in column orientation and the other one in the row orientation - see example below
  • Try clearing the workspace of the variables that you use within this code - make sure that whatever is created with the code, is created for the first time
>> Y
Y =
1 1 1 1 2 0 0 2
>> vectorOfvalues
vectorOfvalues =
0 1 2
>> howMany = Y ==vectorOfvalues
Matrix dimensions must agree.
>> Y = Y'
Y =
1
1
1
1
2
0
0
2
>> howMany = Y ==vectorOfvalues
howMany =
8×3 logical array
0 1 0
0 1 0
0 1 0
0 1 0
0 0 1
1 0 0
1 0 0
0 0 1
Thanks for the your help. I still have the == problem but i solved in this way:
howMany1 = Y'==vectorOfvalues(1) howMany2 = Y'==vectorOfvalues(2) howMany3 = Y'==vectorOfvalues(3) howMany = [howMany1 howMany2 howMany3]
Which version of Matlab do you use?
R2015a..indeed maybe it can be related to the version
Must be. The code:
howMany = Y'==vectorOfvalues
uses so called implicit expansion which was introduced in version R2016B.

Sign in to comment.

More Answers (0)

Categories

Find more on Configure Simulation Conditions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!