How to plot different parts of one vector with different colors?

조회 수: 46 (최근 30일)
miki90
miki90 2018년 1월 30일
댓글: Walter Roberson 2019년 10월 2일
Hello. I have a time vector and ecg vector on y axis which need to be plotted in the same figure with 3 different colors. There are three different zones (time before), suspicious zone in the middle (which I want to be yellow) and time after. How can it be plotted? I tried to plot like in the code below, but it can't be plotted like that.
if true
time1colored(1) = time1(1:523);
time1colored(2) = time1(524:704);
time1colored(3) = time1(705:end);
rr3colored(1) = rr3(1:523);
rr3colored(2) = rr3(524:704);
rr3colored(3) = rr3(705:end);
colors='gyr';
figure; cla;
for i=1:3
plot(time1colored(1),rr3colored(1), 'Color', colors(i))
end
plot(time1,rr3)
xlabel('time [s]'),ylabel('interval [ms]')
end

채택된 답변

James Tursa
James Tursa 2018년 1월 30일
편집: James Tursa 2018년 1월 30일
Something like this if you go with the segmented approach:
ix = { 1:523, 524:704, 704:numel(time1) };
colors = 'gyr';
figure; cla;
for i=1:3
plot(time1(ix{i}),rr3(ix{i}),'Color',colors(i)); hold on
end
  댓글 수: 3
Vidz
Vidz 2019년 10월 2일
편집: Vidz 2019년 10월 2일
gives error as runs out of colors
Index exceeds the number of array elements (3).
So, I did
plot(time1(ix{i}),rr3(ix{i}),'Color',[rand rand rand]); hold on
Walter Roberson
Walter Roberson 2019년 10월 2일
The color list is hardcoded by way of
colors = 'gyr';
You can expand that to include any of the 8 color codes bcgkmrwy
If you need more than that, then I suggest using one of the color tables, such as
ctab = copper(23);
[...]
plot(time1(ix{i}), rr3(ix{i}), 'Color', ctab(i,:)); hold on

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2018년 1월 30일
This is not possible in MATLAB using plot() or related calls. plot() and related calls use line() objects, and line() objects are restricted to a single color.
You can break the vector into segments and draw the segments separately.
If you look in the File Exchange you will find a couple of different contributions for drawing colored lines.

miki90
miki90 2018년 2월 2일
Sorry for posting on an answered post, but, applying this code to plotting with markers, I haven't got only markers but the points connected with lines too. Can anyone tell me why?
if true
ip = { 1:523, 524:824, 824:numel(rr4) };
colors = ['g*','y*','r*'];
figure(); cla;
for i=1:3
plot(rr4(ip{i}),rr5(ip{i}),colors(i)); hold on
end
end
  댓글 수: 4
James Tursa
James Tursa 2018년 2월 2일
You could have used your char array as is, but you would have to change that colors(i) to colors(i,:). I think this makes the code a bit harder to read, and also your construction of colors depends on all of your row strings being the same length. The cell array approach looks neater IMO and also allows you to easily use different length char strings for each color if you wanted to.
Walter Roberson
Walter Roberson 2018년 2월 2일
Also as well as using colors(i,:) you would have had to use colors = ['g*';'y*';'r*'] and all of the specifications would have had to have been the same length. Your existing colors = ['g*','y*','r*']; is building a single character vector with contents 'g*y*r*' . Using cell arrays is better in this case.
Alternately from R2017a onwards you could stay with your existing code but use colors = ["g*","y*","r*"]; which would not require any change to the indexing you are using.

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

카테고리

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