I want to change the backgroung color of my plots.

1 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(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