how to do scatter plot and color code the values?

47 次查看(过去 30 天)
I have 6 variables: x, y, I, theta, time, and LED (see attached the mat file). The LED is a sinusoidal signal. I want to scatter plot x in the x axis, y in the y axis, and I as a vector for each x and y. The vector I can easily be calculated as
Ivec = [cos(theta) sin(theta)]*I;
Then I want to color this Ivec according to the position of the LED signal. For each cycle of the LED signal, a shade of color will be defined. The same color shade will be repeated for the rest of the cycles present in the file.
May I know how to do this?
Thanks!
  3 个评论
Adam Danz
Adam Danz 2022-2-11
> " ...and I as a vector for each x and y"
This part is where things become unclear. The line of code provided in the question throws an error so the intentions are not clear.
> "Then I want to color this Ivec..."
This also needs some clarity. What does it mean to color a vector?
It sounds like you're looking for one of the following.
Anu
Anu 2022-2-11
编辑:Anu 2022-2-11
Thanks, @Adam Danz. And sorry for the confusion. I have attached a representative image, where the black arrows are the I vector, x and y values are sown as the red scattered points. I want to color code these I vectors such that these map with the positions of the LED signal. For example, the red color I vector represents the maximum of the LED signal, the orange color I vector represents the position at the lower LED value... the light pink I vector represents the position at the lowest value of the LED value, and so on.
Ofcourse, this image does not take into account the color codes and the cycles of the sinusoidal signal. Therefore, my plot will look different than what it shows, and will be a bit messy.
I think I need to use pcolor() to shade these values. But, I don't know how I should do everything together.
Hope it helps!

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-2-11
Here's an attempt (basically the same as what @Adam Danz said, if I understand correctly):
S = load('matlab.mat');
Ivec = [cos(S.theta); sin(S.theta)].*S.I;
figure();
hold on
colors = flip(autumn(16),1); % specify your favorite set of colors here
N_colors = size(colors,1);
LED_thresh = linspace(min(S.LED),max(S.LED),N_colors+1);
LED_thresh([1 end]) = [-Inf Inf];
for ii = 1:size(colors,1)
idx = S.LED >= LED_thresh(ii) & S.LED < LED_thresh(ii+1);
quiver(S.x(idx),S.y(idx),Ivec(1,idx),Ivec(2,idx),0,'Color',colors(ii,:));
end
colorbar();
set(gca(), ...
'Colormap',colors, ...
'CLim',[min(S.LED) max(S.LED)], ...
'XLim',[min(S.x+Ivec(1,:)) max(S.x+Ivec(1,:))], ...
'YLim',[min(S.y+Ivec(2,:)) max(S.y+Ivec(2,:))]);
  2 个评论
Adam Danz
Adam Danz 2022-2-11
+1, looks good.
I'd set the quiver(___,scale) factor to something like 4 to decrease vector length but even still, the density prevents seeing underlying patterns.
@Anu, perhaps you could do some vector averaging to reduce the density if that's an issue.
Adam Danz
Adam Danz 2022-2-11
My idea was to convert the LED values into index values 1:n where n is the number of colors in the colormap. This version is much slower but sets the color of each vector individually.
S = load('matlab.mat');
Ivec = [cos(S.theta); sin(S.theta)].*S.I;
figure();
hold on
nColors = 256; % integer; larger is better
colors = flip(autumn(nColors),1);
% convert LED values to color indices
LEDnorm= (S.LED - min(S.LED)) / (max(S.LED)-min(S.LED)); % (0:1)
LEDidx = round(LEDnorm * (nColors-1)) + 1; % (1:nColors)
for ii = 1:numel(S.x)
quiver(S.x(ii),S.y(ii),Ivec(1,ii),Ivec(2,ii),'off','Color',colors(LEDidx(ii),:));
end
cb = colorbar();
ylabel(cb,'LED')
set(gca(), ...
'Colormap',colors, ...
'CLim',[min(S.LED) max(S.LED)]);

请先登录,再进行评论。

更多回答(2 个)

Adam Danz
Adam Danz 2022-2-11
Sounds like Les Beckham was correct. quiver() is the way to go. However, the quiver function only lets you set 1 color for all quiver arrows so you'll need to plot each one individually.
That is demonstrated here and here.
I suggest choosing (or creating) a colormap, scaling the LED values to become indices of the colormap, and then applying the colors to quiver arrows created within a loop.
If my interpretation of the task is correct, let me know where you'd like additional help, otherwise, please let us know where my interpretation goes awry.

William Rose
William Rose 2022-2-11
Please do not use l (lower case L) or I (upper case i) as a variable name, because they appear identical in some fonts. Also, upper case i is likely to be confused with the identity matrix. Use L or i or something else. I am renaming your variable from upper case i to L, to avoid confusion.
load('anuData');
L=I;
You said "The vector I can easily be calculated as Ivec = [cos(theta) sin(theta)]*I;"
but this does not calculate l, since l is on the right hand side. I think you meant to say that the vector lvec can easily be calculated. Also, the statement you wrote produces an error. I think you intended do element-by-element mutiplication, with the dot-multiply operator, on the right hand side. Also, since theta and L are row vectors, you need to transpose them or use a semicolon to make two rows in lvec().
You said "I want to color this Ivec according to the position of the LED signal. For each cycle of the LED signal, a shade of color will be defined. The same color shade will be repeated for the rest of the cycles present in the file." This statement is not clear to me. If you define a color for each cycle, and you repeat the same color shade for the rest of the cycles, then all points will be the same color. I see from plotting LED versus time that LED is periodic with period T=0.5 seconds. Therefore there are 20 cycles in this recording. Perhaps you want to use a different color for each cycle. The code below uses a different color for each cycle. I also plot LED versus time, with the relevant colors, to indicate which colors correspond to which times.
lvec = [cos(theta); sin(theta)].*L; %use the dot-multiply operator
%Create a 20x3 matrix. Each row is a colorspec.
colors=[1,0,0; 1,.3,0; 1,.5,0; 1,.8,0;...
1,1,0; .8,1,0; .5,1,0; .3,1,0;...
0,1,0; 0,1,.3; 0,1,.5; 0,1,.8;...
0,1,1; 0,.8,1; 0,.5,1; 0,.3,1;...
0,0,1; .3,0,1; .5,0,1; .8,0,1];
T=0.5; %cycle duration
figure;
subplot(311), hold on
for i=1:20
plot(x(time>(i-1)*T & time<=i*T),y(time>(i-1)*T & time<=i*T),...
'Marker','*','MarkerEdgeColor',colors(i,:),'LineStyle','none');
end
xlabel('X'); ylabel('Y'); grid on
subplot(312), hold on
for i=1:20
plot(lvec(1,time>(i-1)*T & time<=i*T),lvec(2,time>(i-1)*T & time<=i*T),...
'Marker','*','MarkerEdgeColor',colors(i,:),'LineStyle','none');
end
xlabel('lvec(1)'); ylabel('lvec(2)'); grid on
subplot(313), hold on
for i=1:20
plot(time(time>(i-1)*T & time<=i*T),LED(time>(i-1)*T & time<=i*T),...
'Marker','.','MarkerEdgeColor',colors(i,:),'LineStyle','none');
end
xlabel('Time'); ylabel('LED')
Try it. Good luck!
  1 个评论
Anu
Anu 2022-2-11
Ideally, I want to do what @_ has shown, and @Adam Danz @Les Beckham suggested
However, it's hard to interpret after looking at the data and the plot. I expected it to be messy but thought I could do something. It seems that I can't.
A special thanks to @William Rose for making it more logical, intuitive, and interpretable. And, thanks for your suggestions. I will keep those in mind.
This community is teaching me many things. I learned so much over the last couple of months. Thanks to all! :-) :-)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by