%% Change Color and Transparency of Recession Bands
% Overlay recession bands on time series plot, then return the handles of the recession bands to change the color and opacity of the bands.
% Load the Data_CreditDefaults.mat data set, which contains a credit default rate series and several predictor series measured annually from 1984 through 2004.

load Data_CreditDefaults

% The variables Data and dates, among others, appear in the workspace. For more details on the data, enter Description.
% Data is a 21-by-5 numeric matrix containing the series. Extract the predictor series, which comprise the first four columns.

X = Data(:,1:4);

% dates is a numeric vector containing the 21 sampling years. Convert dates to a datetime vector of years. Assume the series are measured at the end of the year.

T = numel(dates);
dates = [dates [12 31].*ones(T,2)];
dates = datetime(dates);

% Plot the predictor series. Overlay recession bands and return the handles to the bands. Change the band color to red and reduce  the opacity to 0.1.

figure;
plot(dates,X,'LineWidth',2);
xlabel("Year");
ylabel("Level");
hBands = recessionplot;
set(hBands,'FaceColor',"r",'FaceAlpha',0.1);

% Copyright 2021 The MathWorks, Inc.