-
Notifications
You must be signed in to change notification settings - Fork 54
Description
The anomaly plot function could be much more versatile by allowing the user to specify a variable baseline. This is useful for visualising differences between the annual trend of a given parameter and its climatological mean. This is an example plot:

Implementing this into the code is pretty straightforward:
- Line 81
Assert that the base is either a scalar (constant baseline) or a vector the same length as x and y (variable baseline):
assert(isscalar(base) | numel(base)==numel(y),'Input error: Base value must be a scalar or a vector of the same length as x and y.')
- Data Manipulation section (line 103)
If base is a scalar, convert it into a column vector the length of y (before "Columnate inputs to ensure...":
% If base is a scalar convert it into a column vector the length of y:
if isscalar(base)
base = repmat(base,numel(y),1);
end
Columnate base just like x and y (only relevant if base is not a scalar):
base = base(:);
Find NaNs both in y and base so filling will work (and remove them from base as well):
% If y or base contains nans, ignore them so filling will work:
ind = (isfinite(y) & isfinite(base));
...
base = base(ind);
The intersections subfunction now simplifies as follows:
[xc,yc] = intersections(x,y,x,base);
Add zero crossings to base:
% Add zero crossings to the input dataset and sort them into the proper order:
...
base = [base;yc];
...
base = base(ind);
Splitting the data into a top and bottom dataset changes as follows:
% Start thinking about this as two separate datasets which share baseline values where they meet:
ytop = y;
ytop(ytop<base) = base(ytop<base);
ybot = y;
ybot(ybot>base) = base(ybot>base);
- Plotting
Instead of using area we now need to use fill, the output remains the same:
% Plot the top half:
htop = fill([x;flipud(x)],[ytop;flipud(base)],topcolor);
% Plot the bottom half:
hbot = fill([x;flipud(x)],[ybot;flipud(base)],bottomcolor);
VOILA! This will allow the user to provide either a constant or a variable baseline and the code will do the rest:

Here is a copy of the adjusted code:
anomaly_JW.m.zip
I hope this is helpful.
Cheers,
Jake