필터 지우기
필터 지우기

how to color part of a rasterplot using a color triplets array

조회 수: 1 (최근 30일)
Leonardo Mascelli
Leonardo Mascelli 2022년 11월 21일
답변: DGM 2023년 8월 31일
Hi,
i'm trying to color with two different colors a rasterplot using a binary array that represents the presence/absence of a stimulus.
the rasterplot is made this way:
% TIME_SERIES is a cell array with the times of each event in each cell,
% representing different electrodes
% STIM_TIMES is the stimulation binary signal which is shared between series
% both are passed to the algorithm via the arguments of a function
for i = 1:numel(time_series)
xdata = repmat(time_series{i}', 3, 1);
ydata = nan(size(xdata));
ydata(1, :) = i-1;
ydata(2, :) = i;
colors = [stim_times zeros(size(stim_times)) zeros(size(stim_times))];
plot(hndl, xdata, ydata, "color", colors)
end
Unrecognized function or variable 'time_series'.
the issue is (in my opinion) the size of the stim_times array but the error reported is
Error using plot
Invalid RGB triplet. Specify a three-element vector of values between 0 and 1.
  댓글 수: 2
DGM
DGM 2022년 11월 21일
If colors is intentionally supposed to be a (binarized?) color table, then no, plot() can't accept that. You might try using scatter() instead.
Robert Daly
Robert Daly 2023년 6월 13일
By "binary" do you mean "boolean" i.e. true or false?
If you want to make a raster style plot I suggest you should try
imagesc
or
pcolor
something like
figure
pcolor(xdata, ydata, colors)
shading flat

댓글을 달려면 로그인하십시오.

답변 (2개)

ag
ag 2023년 8월 30일
Hi,
I understand that you are getting an error while using the "plot" function in your script.
The error is in the following line of code:
colors = [stim_times zeros(size(stim_times)) zeros(size(stim_times))];
The error is caused due to the values being passed to the "color" input parameter of the "plot" function. To resolve this issue, make sure that the values assigned to the "colors" variable are triplets with each value ranging from 0 to 1.
For example, you can plot a figure with blue lines by using the following code:
plot(hndl, xdata, ydata, "color", [0 0 1]);
For more detailed information, please refer to the following documentation page:
Hope this helps!

DGM
DGM 2023년 8월 31일
There were multiple ways to solve this:
% this emulates the inputs as described
N = 10;
time_series = linspace(0,10,N);
time_series = num2cell(time_series);
stim_times = rand(1,N)>0.5;
% either do it in a loop, generating one tuple at a time
for i = 1:numel(time_series)
xdata = repmat(time_series{i}', 3, 1);
ydata = nan(size(xdata));
ydata(1, :) = i-1;
ydata(2, :) = i;
colors = [stim_times(i) 0 0]; % a color tuple, not a table
plot(xdata, ydata, 'color', colors,'linewidth',2); hold on
end
... or instead of generating each tuple one at a time, you can generate the color table as was attempted originally:
figure
% or generate a color table and then use it in a loop
CT = [stim_times(:) zeros(numel(stim_times),2)]; % a color table
for i = 1:numel(time_series)
xdata = repmat(time_series{i}', 3, 1);
ydata = nan(size(xdata));
ydata(1, :) = i-1;
ydata(2, :) = i;
plot(xdata, ydata, 'color', CT(i,:),'linewidth',2); hold on
end
... but the loop isn't necessary. the whole thing can be simplified:
figure
% assemble x,y data
xdata = repmat(cell2mat(time_series),[3 1]);
ydata = (1:numel(time_series)) - [1; 0; NaN];
% set colororder, plot
CT = [stim_times(:) zeros(numel(stim_times),2)]; % a color table
colororder(CT)
plot(xdata, ydata,'linewidth',2)
The question remains whether this barely-readable staircase of tiny line segments was actually the appropriate output. This could probably be done simpler yet with a scatter plot or something else.
figure
% assemble x,y data
xdata = cell2mat(time_series);
ydata = 1:numel(time_series);
% plot the data using a short color table
scatter(xdata,ydata,100,stim_times,'filled');
CT = [0 0 0; 1 0 0]; % one color for each class
colormap(CT)
... but OP never clarified, so we can guess all we want now.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by