MATLAB Answers

Custering data by color

7 views (last 30 days)
Hey there everyone.
I have a series of data (50000 x 2 matrix) of an eye diagram and when I plot them. It has a solid blue output.
What I have in mind is to cluster this data by color in terms of their density (the more the density, the more color tends from blue to red) as in the second figure I sent (contauning blues, greens and tinges of red and yellow in the center). How can I accomplish this in matlab?
Thank you

  2 Comments

darova
darova on 3 Jul 2020
What about hist3?
Mahdi Khalili Hezarjaribi
I do not get it what you mean. You mean hist3 can be used for color-partitioning my data? I think that command is something totally different unless it is something new I heard

Sign in to comment.

Accepted Answer

darova
darova on 3 Jul 2020
here is an example. Adapt it for you needs
% generate some data
t = linspace(0,2*pi,1e4)';
x = cos(50*t) + 0.8*cos(t);
y = sin(50*t) + 0.8*sin(t);
% number of grid points
n = 30;
z1 = hist3([x y],[n n]);
% create mesh for histogram
[x1,y1] = meshgrid( linspace(-1.8,1.8,n) );
pcolor(x1,y1,z1-30) % display density
line(x,y,'col','y') % display lines
figure(2)
z = interp2(x1,y1,z1,x,y); % interpolate colors
patch(x,y,x*0,z,'edgecolor','flat',...
'facecolor','none')
50

  3 Comments

Mahdi Khalili Hezarjaribi
Thank you for your help
I did what you instructed yet I think it somehow just shades my plot rather than real color-partitioning as I sent in a figure in my question. the result is something like this:
however I attached my data (data.mat) as matfile so that it will help you analyse everything and many thanks for your help so far
darova
darova on 4 Jul 2020
made some changes. Look
n = 48 n = 24
Mahdi Khalili Hezarjaribi
Absolutely correct
thanks *_*

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 3 Jul 2020
mahdi: Is this what you're looking for:
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
s = load('data.mat')
x = s.data(:, 1);
y = s.data(:, 2);
subplot(2, 2, 1);
plot(x, y, 'b.');
title('Plot of original data (Note: it is quantized)', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
grid on;
% Make image out of it.
rows = length(unique(x));
columns = rows;
grayImage = zeros(rows, columns);
% Rescale x and y to get them in range
xs = rescale(x, 1, columns);
ys = rescale(y, 1, rows);
lineBreaks = find(isnan(x))
% Make sure we include the first point if it's not a nan
if lineBreaks(1) > 1
lineBreaks = [0; lineBreaks]; % Zero because we add one to it later.
end
for k = 2 : length(lineBreaks)
index1 = lineBreaks(k-1) + 1; % First index after a NAN.
index2 = lineBreaks(k) - 1; % Last index before the next NAN.
thisx = xs(index1:index2);
thisy = ys(index1:index2);
% Now because x is quantized, we need to interpolate
% values along x and y to get very possible value.
xq = linspace(1, length(thisx), columns);
xi = spline(1 : length(thisx), thisx, xq); % Interpolated x have a lot more values, more finely quantized than the original x.
yq = linspace(1, length(thisy), rows);
yi = spline(1 : length(thisy), thisy, yq); % Interpolated y have a lot more values, more finely quantized than the original y.
% For each one of these interpolated values, add a count to our image.
for ki = 1 : length(xi)
col = round(xi(ki));
row = round(yi(ki));
if isnan(row) || isnan(col) || row <= 0 || row > rows || col <= 0 || col > columns
continue;
end
grayImage(row, col) = grayImage(row, col) + 1;
end
end
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
subplot(2, 2, 2);
% Colorize it by creating a colormap
cmap = jet(256); % or hsv or parula or winter
cmap(1,:) = [0,0,0];
% Flip the vertical axis so that lower numbers are at the bottom and higher y are at the top.
imshow(grayImage, [], 'Colormap', cmap);
% imshow(grayImage, [], 'XData', [min(x), max(x)], 'YData', [min(y), max(y)]);
axis('on', 'xy');
title('Output Image', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
% Rename tick marks
xt = xticks();
xt2 = rescale(xt, min(x), max(x));
for k = 1 : length(xt2)
xt2t{k} = sprintf('%.2f', xt2(k));
end
xticklabels(xt2t);
yt = yticks();
yt2 = rescale(yt, min(y), max(y));
for k = 1 : length(yt2)
yt2t{k} = sprintf('%.2f', yt2(k));
end
yticklabels(yt2t);
% colormap(cmap);
colorbar;
% Get the histogram of counts, just for curiosity.
subplot(2, 2, 3:4);
counts = histcounts(grayImage);
counts(1) = 0; % Suppress all the pixels that are 0 because they never got anything assigned to them.
bar(counts);
grid on;
title('Histogram of Image', 'FontSize', fontSize);
xlabel('Counts', 'FontSize', fontSize);
ylabel('# Pixels with that count', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);

  2 Comments

Mahdi Khalili Hezarjaribi
Many thanks
Exactly What I wanted!

Sign in to comment.

Products


Release

R2019b

Translated by