Borrar filtros
Borrar filtros

Coloring scatterplots based on frequency of point occurrence in input

27 visualizaciones (últimos 30 días)
Hello,
I have an input file (.txt) that is approximately 10,000 data points, all of the form "x y"
I know that many of the points are repeats. Is it possible to, when I load and scatterplot this data, to color points based on how frequently they occur in the input? For instance the color could be (R,G,B) = (f,f,f) where f is the amount of times the point appears in input.
My current code (that makes all points the same blue color) looks like this:
data=load('input.txt')
scatter(data(:,1),data(:,2),0.3)
I am new to Matlab (but not to programming) so the more explanation the better!
Thank you in advance.

Respuesta aceptada

Jiro Doke
Jiro Doke el 13 de Feb. de 2012
EDIT: unique instead of sort
You should be able to do it with:
unique (use the 'rows' parameter)
hist (use the n = hist(Y,x) syntax)
scatter (use the scatter(X,Y,S,C) syntax)
Okay, so here's how I would do it. I must say it was a little trickier than I had expected. Maybe someone else can come up with an easier solution.
A = fix(rand(100,1)*10);
B = fix(rand(100,1)*10);
AB =[A B];
% Find unique rows and corresponding indices
[uniqueAB, ~, n] = unique(AB, 'rows');
% Find number of occurrences
nHist = hist(n, unique(n));
mx = max(nHist);
% Create colors for each number of occurrence
colors = jet(mx);
colormap(colors);
% Construct a color matrix
cMatrix = colors(nHist, :);
% Create scatter plot
scatter(uniqueAB(:, 1), uniqueAB(:, 2), 25, cMatrix, 'filled');
colorbar('YTick', linspace(1/(2*mx), 1-1/(2*mx), mx), ...
'YTickLabel', 1:mx);
  2 comentarios
Sukuchha
Sukuchha el 16 de Feb. de 2012
thanks jiro,it worked so beautifully ! The original guy who posted this question seems to gone to sleep. If i could i would accept your question.
jason
jason el 9 de Abr. de 2013
Thank you Jiro, this worked very well. At the moment, I need to extend this further to include two sets of scatters on the same figure. Can you please advise me how can I make them share the same colorbar scale, or color setting. I think one data the max frequency is 130 where the other one is about 50. thanks

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 13 de Feb. de 2012
You can pass in the colors of each point. The trick is that you first have to call hist(data) to find the frequency of each range of your data and assign that to a particular color. I think you can do it - it's not that hard. Give it a shot. If you really can't come back for help.
  2 comentarios
Alex V
Alex V el 13 de Feb. de 2012
I don't really see how a histogram will help.
I was going to try to sort the data (it is discreet), then for every x coordinate count how many times the same y coordinate appears.
I also don't see how to pass into scatter() the colors of specific points (as opposed to making them all the same).
Image Analyst
Image Analyst el 13 de Feb. de 2012
I see. And what do you think the histogram does? Do you think that it might "for every x coordinate count how many times the same y coordinate appears"? Now you have two people telling you to use the hist.

Iniciar sesión para comentar.


Sukuchha
Sukuchha el 14 de Feb. de 2012
Hi image analyst and jiro,
I tried to follow your instruction but couldnot succeed !
This is what i did.
A = fix(rand(100,1)*10);
B = fix(rand(100,1)*10);
AB =[A B];
sort_AB = sort(AB,1);
[n,xout] = hist(sort_AB)
But, with hist command, n is nx2 matrix where each columns of sort_AB is taken independently. So n will not give me, how many points are within 10 equally divided range.
And also please explain how to make a C matrix?
A example with code will surely make things clear :)

Anita Joshi
Anita Joshi el 17 de Jun. de 2017
Hallo everyone, I would like to print the number of occurance of the 2D data in the same graph.. how do I do it?

Categorías

Más información sobre Discrete Data Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by