How can I program a section of my plot to have a different color background?

5 views (last 30 days)
I plot lines along my data and I would like to make certain section in between them a different color. I have tried inserting a rectangle between them, but couldn't get it to be transparent (so that the data lines could still be seen). Is there a quick way to make the axis background a different color in between two x-axis values?
I have attached a sample plot to refer to.
  1 Comment
Ced
Ced on 10 Mar 2016
I don't know how to set the background, but you can get your partially transparent rectangle with "patch", and then change the "FaceAlpha" value.
Example:
x = -1:0.1:1;
y = sin(x)
% define patch using vertices and color (normalized RGB)
h = patch([ 0 1 1 0 ],[0 0 1 1],[0.5 0.5 0 ]);
h.FaceAlpha = 0.5; % partially transparent
h.EdgeAlpha = 0; % we don't want the edge
hold on;
plot(x,y); % plot whatever you like
Note that if you create the patch before plotting, the curve is plotted "in front" of the patch, so you actually don't even need to set the transparency (except maybe for aesthetic reasons).

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!