Matlab 绘图的颜色背景通过矩形

莉娜

我想在 Matlab 中绘制一个图,其中零以下的背景区域以浅红色着色(最终也是:零以上的区域以浅绿色着色)。我下面的代码有什么问题,所以图中没有显示红色矩形?如果比矩形命令更方便,也欢迎您向我展示另一种为背景着色的方法。谢谢你。

Y = [];
for year = 2008:2016
    Y = vertcat(Y,[year;year]);
end
M = repmat([01;07],9,1);
D = [01];
vector = datetime(Y,M,D);

figure;
rectangle('Position',[0,-2e4,length(vector),2e4],'FaceColor',[1 0 0],'EdgeColor',[1 0 0]);
hold on;
plot( vector, [-2e4, -1e3, -5, -100, 5, 20, 100, 40, -20, -200, -600, -2, 30, 80, 200, 800, 1500, 2500], 'LineWidth',1.2 ), grid on;
dateaxis('x', 12);
沙玛利亚

您必须将向量转换为数字。我通常用来fill给背景上色。

figure;hold on
fill([2008 2016 2016 2008 ],...
[-2e4 -2e4 0 0],'r');
fill([2008 2016 2016 2008 ],...
[0 0 3e3 3e3],'g');
plot( str2num(datestr(vector,'yyyy')), ... %%%convert vector format
[-2e4, -1e3, -5, -100, 5, 20, 100, 40, -20, -200, -600, -2, 30, 80, 200, 800, 1500, 2500], ...
'LineWidth',1.2 ), grid on;
axis([2007 2019 -2e4 3e3])

输出不是很好。也许你想给一些透明度来填充

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章