I have a two data samples
data1=rand(96,1); %random number with 96*1
data2=zeros(96,1)+mean(data1);
I would like to plot ''stairs'' of data1 and date 2 in one figure. data 2 is mean of data 1, now i would like to plot somting like this in stairs for example if data2 is > data1 fill with green color, other wise with yellow color. here red line is data1 and data2 is mean.

 채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 9월 7일
편집: Dyuman Joshi 2023년 9월 7일

1 개 추천

data1=rand(96,1); %random number with 96*1
avg=mean(data1);
%Get the x and y coordinates corresponding to the stairs plot
[x,y] = stairs(data1);
%Visualizing the stairs plot with the mean value for reference
figure
plot(x,y)
yline(avg,'k')
%Comparing values with mean in order to get two different plots
yb=min(y,avg);
ys=max(y,avg);
figure
hold on
box on
%Plotting filled area 2D plot with mean value as the baseline
area(x,yb,avg,'FaceColor','g')
area(x,ys,avg,'FaceColor','y')
yline(avg,'k')

댓글 수: 6

Jayachandra Malavatu
Jayachandra Malavatu 2023년 9월 7일
Thanks Joshi , its working well.
Jayachandra Malavatu
Jayachandra Malavatu 2023년 9월 13일
Jayachandra Malavatu
Jayachandra Malavatu 2023년 9월 13일
what if there is two values like here 160 and 250, below 160 is with green color and above 250 is with yellow color.
Dyuman Joshi
Dyuman Joshi 2023년 9월 13일
What should be the color for values between 160 and 250?
Jayachandra Malavatu
Jayachandra Malavatu 2023년 9월 13일
its white , only graph should be there something like shown in figure above
Dyuman Joshi
Dyuman Joshi 2023년 9월 13일
편집: Dyuman Joshi 2023년 9월 13일
Here's a workaround.
I'll update you if I find a better solution.
%Random values for example
data = [randi([120 210],1,50) randi([200 220],1,15) randi([210 300],1,50)];
[x,y]=stairs(data);
valmin = 160;
valmax = 250;
ymin = min(valmin,y);
ymax = max(valmax,y);
figure
hold on
area(x,ymax,valmax,'FaceColor','b','EdgeColor','w','ShowBaseLine',0)
area(x,ymin,valmin,'FaceColor','y','EdgeColor','w','ShowBaseLine',0)
area(x,valmax*ones(size(x)),valmin,'FaceColor','w','EdgeColor','w')
plot(x,y,'k-')

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by