From: Corey Marriott on
Hey all,

I would like to plot a dataset and color certain vertical regions of the background a different color (namely gray). The gray regions will represent an economic recession. Does anyone know how to color vertical regions of the background?

If it helps I am trying to produce something like http://research.stlouisfed.org/fred2/data/USINTDMRKTOTH_Max_630_378.png with the "gray bars" running up certain regions of the x axis.

C.M.
From: ImageAnalyst on
Corey Marriott:
One way to do it is to plot gray bars behind your main ones. Run this
demo:

clc;
close all;
clear all;
workspace; % Display workspace panel.

% Generate some sample data
data = 2.5 * rand(1, 20) - 1
% Plot the data so we can get the y limits
% so we know where to put gray backgrounds.
hBars = bar(data);
set(hBars, 'BarWidth', 1);
yl = ylim
x = get(hBars, 'XData');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Define the recession where we'll have a gray background.
recession = data < 0.4
hold on;
grayColor = [1 1 1] * 0.8; % Adust to change lightness of the gray.
% Plot gray bars going down.
hBarsDown = bar(yl(1) * recession, ...
'FaceColor', grayColor, 'BarWidth', 1, 'lineStyle', 'none');
% Plot gray bars going Up.
hBarsUp = bar(yl(2) * recession, ...
'FaceColor', grayColor, 'BarWidth', 1, 'lineStyle', 'none');
% Plot main bars.
hBars = bar(data, 'FaceColor', 'b', 'BarWidth', 1);