matthiasm@8: function [ha hb hc] = shadedplot(x, y1, y2, varargin) matthiasm@8: matthiasm@8: % SHADEDPLOT draws two lines on a plot and shades the area between those matthiasm@8: % lines. matthiasm@8: % matthiasm@8: % SHADEDPLOT(x, y1, y2) matthiasm@8: % All of the arguments are vectors of the same length, and each y-vector is matthiasm@8: % horizontal (i.e. size(y1) = [1 N]). Vector x contains the x-axis values, matthiasm@8: % and y1:y2 contain the y-axis values. matthiasm@8: % matthiasm@8: % Plot y1 and y2 vs x, then shade the area between those two matthiasm@8: % lines. Highlight the edges of that band with lines. matthiasm@8: % matthiasm@8: % SHADEDPLOT(x, y1, y2, areacolor, linecolor) matthiasm@8: % The arguments areacolor and linecolor allow the user to set the color matthiasm@8: % of the shaded area and the boundary lines. These arguments must be matthiasm@8: % either text values (see the help for the PLOT function) or a matthiasm@8: % 3-element vector with the color values in RGB (see the help for matthiasm@8: % COLORMAP). matthiasm@8: % matthiasm@8: % [HA HB HC = SHADEDPLOT(x, y1, y2) returns three handles to the calling matthiasm@8: % function. HA is a vector of handles to areaseries objects (HA(2) is the matthiasm@8: % shaded area), HB is the handle to the first line (x vs y1), and HC is matthiasm@8: % the handle to the second line (x vs y2). matthiasm@8: % matthiasm@8: % Example: matthiasm@8: % matthiasm@8: % x1 = [1 2 3 4 5 6]; matthiasm@8: % y1 = x1; matthiasm@8: % y2 = x1+1; matthiasm@8: % x3 = [1.5 2 2.5 3 3.5 4]; matthiasm@8: % y3 = 2*x3; matthiasm@8: % y4 = 4*ones(size(x3)); matthiasm@8: % ha = shadedplot(x1, y1, y2, [1 0.7 0.7], 'r'); %first area is red matthiasm@8: % hold on matthiasm@8: % hb = shadedplot(x3, y3, y4, [0.7 0.7 1]); %second area is blue matthiasm@8: % hold off matthiasm@8: matthiasm@8: % plot the shaded area matthiasm@8: y = [y1; (y2-y1)]'; matthiasm@8: ha = area(x, y); matthiasm@8: set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible matthiasm@8: set(ha, 'LineStyle', 'none') matthiasm@8: matthiasm@8: % plot the line edges matthiasm@8: hold on matthiasm@8: hb = plot(x, y1, 'LineWidth', 1); matthiasm@8: hc = plot(x, y2, 'LineWidth', 1); matthiasm@8: hold off matthiasm@8: matthiasm@8: % set the line and area colors if they are specified matthiasm@8: switch length(varargin) matthiasm@8: case 0 matthiasm@8: case 1 matthiasm@8: set(ha(2), 'FaceColor', varargin{1}) matthiasm@8: case 2 matthiasm@8: set(ha(2), 'FaceColor', varargin{1}) matthiasm@8: set(hb, 'Color', varargin{2}) matthiasm@8: set(hc, 'Color', varargin{2}) matthiasm@8: otherwise matthiasm@8: end matthiasm@8: matthiasm@8: % put the grid on top of the colored area matthiasm@8: set(gca, 'Layer', 'top') matthiasm@8: grid on