-
Notifications
You must be signed in to change notification settings - Fork 31
/
colorbarf.m
185 lines (159 loc) · 5.12 KB
/
colorbarf.m
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function handle=colorbarf(varargin)
% colorbarf.m
% Displays colorbar axis.
% Takes four input arguments: LOCATION ('hor', 'ver')
% FONTSIZE
% FONTNAME ('Times, 'Helvetica', and so on)
% (optional) POSITION ([Left Bottom Width Height])
% If using POSITION, must also specify FONTSIZE, FONTNAME
% From the original colorbar.m, modified by FJS, June 27, 1997.
% AFTER THIS MAY WANT TO DO set(gcf,'NextPlot','add')
%COLORBAR Display color bar (color scale).
% COLORBAR('vert') appends a vertical color scale to the current
% axis. COLORBAR('horiz') appends a horizontal color scale.
%
% COLORBAR(H) places the colorbar in the axes H. The colorbar will
% be horizontal if the axes H width > height (in pixels).
%
% COLORBAR without arguments either adds a new vertical color scale
% or updates an existing colorbar.
%
% H = COLORBAR(...) returns a handle to the colorbar axis.
% Clay M. Thompson 10-9-92
% Copyright (c) 1984-96 by The MathWorks, Inc.
% $Revision: 5.18 $ $Date: 1996/10/22 15:10:46 $
% If called with COLORBAR(H) or for an existing colorbar, don't change
% the NextPlot property.
changeNextPlot = 1;
loc=varargin{1};
ftsize=varargin{2};
ftname=varargin{3};
if nargin==4
pospos=varargin{4};
end
if nargin<1, loc = 'vert'; end
ax = [];
if nargin==1,
if ishandle(loc)
ax = loc;
if ~strcmp(get(ax,'type'),'axes'),
error('Requires axes handle.');
end
units = get(ax,'units'); set(ax,'units','pixels');
rect = get(ax,'position'); set(ax,'units',units)
if rect(3) > rect(4), loc = 'horiz'; else loc = 'vert'; end
changeNextPlot = 0;
end
end
% Determine color limits by context. If any axes child is an image
% use scale based on size of colormap, otherwise use current CAXIS.
ch = get(gca,'children');
hasimage = 0; t = [];
cdatamapping = 'direct';
for i=1:length(ch),
typ = get(ch(i),'type');
if strcmp(typ,'image'),
hasimage = 1;
cdatamapping = get(ch(i), 'CDataMapping');
elseif strcmp(typ,'surface') & ...
strcmp(get(ch(i),'FaceColor'),'texturemap') % Texturemapped surf
hasimage = 2;
cdatamapping = get(ch(i), 'CDataMapping');
elseif strcmp(typ,'patch') | strcmp(typ,'surface')
cdatamapping = get(ch(i), 'CDataMapping');
end
end
if ( strcmp(cdatamapping, 'scaled') )
if hasimage,
if isempty(t);
t = caxis;
end
else
t = caxis;
d = (t(2) - t(1))/size(colormap,1);
t = [t(1)+d/2 t(2)-d/2];
end
else
if hasimage,
t = [1, size(colormap,1)];
else
t = [1.5 size(colormap,1)+.5];
end
end
h = gca;
if nargin==0,
% Search for existing colorbar
ch = get(gcf,'children'); ax = [];
for i=1:length(ch),
d = get(ch(i),'userdata');
if prod(size(d))==1 & isequal(d,h),
ax = ch(i);
pos = get(ch(i),'Position');
if pos(3)<pos(4), loc = 'vert'; else loc = 'horiz'; end
changeNextPlot = 0;
break;
end
end
end
origNextPlot = get(gcf,'NextPlot');
if strcmp(origNextPlot,'replacechildren') | strcmp(origNextPlot,'replace'),
set(gcf,'NextPlot','add')
end
if loc(1)=='v', % Append vertical scale to right of current plot
if isempty(ax),
units = get(h,'units'); set(h,'units','normalized')
pos = get(h,'Position');
[az,el] = view;
stripe = 0.075; edge = 0.02;
if all([az,el]==[0 90]), space = 0.05; else space = .1; end
set(h,'Position',[pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)])
if exist('pospos')==1
set(h,'Position',pos)
pos=pospos;
end
rect = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
% Create axes for stripe
ax = axes('Position', rect);
set(h,'units',units)
else
axes(ax);
end
% Create color stripe
n = size(colormap,1);
image([0 1],t,(1:n)','Tag','TMW_COLORBAR'); set(ax,'Ydir','normal')
set(ax,'YAxisLocation','right')
set(ax,'xtick',[])
else, % Append horizontal scale to top of current plot
if isempty(ax),
units = get(h,'units'); set(h,'units','normalized')
pos = get(h,'Position');
stripe = 0.075; space = 0.1;
set(h,'Position',...
[pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)])
if exist('pospos')==1
set(h,'Position',pos)
pos=pospos;
end
rect = [pos(1) pos(2) pos(3) stripe*pos(4)];
% Create axes for stripe
ax = axes('Position', rect);
set(h,'units',units)
else
axes(ax);
end
% Create color stripe
n = size(colormap,1);
image(t,[0 1],(1:n),'Tag','TMW_COLORBAR'); set(ax,'Ydir','normal')
set(ax,'ytick',[])
end
set(ax,'userdata',h)
set(gcf,'CurrentAxes',h)
set(ax,'FontSize',[ftsize]')
set(ax,'FontName',ftname)
if changeNextPlot
set(gcf,'Nextplot','ReplaceChildren')
end
if nargout>0, handle = ax; end
if exist('pospos')==1
set(ax,'Position',pospos)
end