如何在Matlab中为绘图的特定x值设置背景颜色? [英] How to set background color for specific x-values of plot in Matlab?

查看:490
本文介绍了如何在Matlab中为绘图的特定x值设置背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如见下文.

我想用红色标记另一个独立时间序列大于0的年份.

I'd like to color-code (in red) the years for which another independent timeseries is greater than 0.

推荐答案

您可以使用

You can use the patch() function to draw sqares wherever you want, and give it the color in RGBA format, using an A (alpha) of around 0.5 for example

让我们举例说明:

x=1:0.1:10;
y=sin(x*2*pi);

box1=[1 1 2 2];
box2=[4 4 5 5];
boxy=[-1 1 1 -1]*max(y)*1.2;

plot(x,y)
patch(box1,boxy,[0 1 0],'FaceAlpha',0.2)
patch(box2,boxy,[1 0 0],'FaceAlpha',0.2)

ylim(1.1*[min(y) max(y)])

这篇关于如何在Matlab中为绘图的特定x值设置背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆