-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddgradient.m
More file actions
72 lines (62 loc) · 2.01 KB
/
addgradient.m
File metadata and controls
72 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function varargout = addgradient(ax,topcolor,bottomcolor)
% ADDGRADIENT Add a nice looking gradient background to a plot
%
% ADDGRADIENT adds a subtle gray gradient background to the current plot
%
% ADDGRADIENT(AX) adds the gradient to the plot in axes with handle AX.
%
% ADDGRADIENT(AX,TOPCOLOR,BOTTOMCOLOR) adds a gradient ranging from the
% specified colors, with TOPCOLOR at the top of the plot and BOTTOMCOLOR at
% the bottom of the plot. TOPCOLOR and BOTTOMCOLOR must be specified as
% RGB triplets, e.g. [R G B], where R, G, and B range from 0 to 1 to
% indicate the intensity of Red, Green, and Blue in the specified color
%
% P = ADDGRADIENT(...) returns handle to the patch used to draw the
% gradient. This is useful if you want to further modify the gradient,
% such as by setting the transparency
%
% It is recommended that you add the gradient after setting the final axis
% limits, since the gradient is not redrawn when axis limits change.
%
% Ex
% clf
% plot(10*rand(1,100));
% addgradient
%
%
% clf
% ax1 = subplot(211)
% plot(magic(3))
% ax2 = subplot(212)
% plot(10*rand(1,100));
% topcolor = [1 0 0]; % red
% bottomcolor = [0 1 0]; % green
% addgradient(ax1) % Default gray gradient to top plot
% p = addgradient(ax2,topcolor,bottomcolor); % Red-green gradient to bottom plot
% set(p,'FaceAlpha',.3) % Make transparent
% Michelle Hirsch
% mhirsch@mathworks.com
% Copyright 2010-2014 The MathWorks, Inc
if nargin==0 || isempty(ax)
ax = gca;
end
if nargin<2
topcolor = [.95 .95 .95];
bottomcolor = [.75 .75 .75];
end
lim = axis(ax);
xdata = [lim(1) lim(2) lim(2) lim(1)];
ydata = [lim(3) lim(3) lim(4) lim(4)];
cdata(1,2,:) = bottomcolor;
cdata(1,3,:) = bottomcolor;
cdata(1,1,:) = topcolor;
cdata(1,4,:) = topcolor;
p = patch(xdata,ydata,'k','Parent',ax);
set(p,'CData',cdata, ...
'FaceColor','interp', ...
'EdgeColor','none');
uistack(p,'bottom') % Put gradient underneath everything else
if nargout
varargout{1} = p;
end
set(gca,'Layer','top')