I want to change the backgroung color of my plots.

조회 수: 1 (최근 30일)
Mohammad Sadegh Nasirianfar
Mohammad Sadegh Nasirianfar 2022년 5월 7일
댓글: Voss 2022년 5월 7일
I want to draw a graph in which for any rang that graph shows different backgroung it shows. for example in the range of o-1 red background, in 1-2 blu, in 2-3 yellow and so forth.
could I do that in matlab?

채택된 답변

Voss
Voss 2022년 5월 7일
Is something like this what you mean?
colors = [1 0 0; 0 0 1; 1 1 0];
thresholds = [1 2];
figure()
subplot(2,1,1)
x = 0:0.05:2*pi;
y = 1.5+1.5*sin(x);
plot(x,y,'k','LineWidth',2);
xlim([0 2*pi]);
ylim([0 3]);
set_background(x,y,colors,thresholds)
subplot(2,1,2)
x = 0:0.025:2*pi;
y = 1.5+1.5*sin(x)+0.45*randn(1,numel(x));
plot(x,y,'k','LineWidth',2);
xlim([0 2*pi]);
ylim([-2 5]);
set_background(x,y,colors,thresholds)
function set_background(x,y,colors,thresholds)
idx = ones(1,numel(x));
thresholds = [-Inf thresholds Inf];
for ii = 2:numel(thresholds)
idx(y >= thresholds(ii-1) & y < thresholds(ii)) = ii-1;
end
yl = get(gca(),'YLim').';
x = (x(1:end-1)+x(2:end))/2;
x = [2*x(1)-x(2) x 2*x(end)-x(end-1)];
p = patch( ...
'XData',x((2:end)+[0;0;-1;-1;0]), ...
'YData',repmat(yl([1 2 2 1 1]),1,numel(x)-1), ...
'FaceColor','flat', ...
'FaceVertexCData',colors(idx,:), ...
'EdgeColor','none');
ch = get(gca(),'Children');
ch(ch == p) = [];
set(gca(),'Children',[ch(:); p],'Layer','top');
end
  댓글 수: 6
Mohammad Sadegh Nasirianfar
Mohammad Sadegh Nasirianfar 2022년 5월 7일
thank you very very much for your great help.
Voss
Voss 2022년 5월 7일
You're welcome!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by