-
Notifications
You must be signed in to change notification settings - Fork 6
/
jicolorbar.m
309 lines (268 loc) · 10.4 KB
/
jicolorbar.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
function handle=jicolorbar(loc,tit)
% JICOLORBAR Same as COLORBAR, but does not resize current axis. (JRI)
% JICOLORBAR('vert') appends a vertical color scale to the current
% axis. JICOLORBAR('horiz') appends a horizontal color scale.
%
% This is a modified version of an older Matlab COLORBAR function.
% modified by JRI John Iversen ([email protected])
%
% My main problem with the original is aesthetic and layout: adding a color
% bar to an axis changes the size of the axis.
%
% The key improvement is that this does NOT rescale the axis when a colorbar is
% added, which is preferable e.g. when you like your plot layout already,
% or when mixing plots with and without colorbars (and wanting
% them all to be the same size).
%
% The size of colorbars is also a bit different: I think they look much better
% narrower. They are also placed flush with the edge of the axis.
% JICOLORBAR('wide') is the same width as the default colorbar.
%
% In addition, you can show half-height versions of the colorbar using:
% JICOLORBAR('vshort') adds a half-height vertical color scale.
% JICOLORBAR('hshort') adds a half-width horizontal color scale.
%
% JICOLORBAR(H) places the colorbar in the axes H. The colorbar will
% be horizontal if the axes H width > height (in pixels).
%
% JICOLORBAR without arguments either adds a new vertical color scale
% or updates an existing colorbar.
%
% H = JICOLORBAR(...) returns a handle to the colorbar axis.
%
% Note, jicolorbar can be used together with colorbar and freezeColors to provide two
% colorbars for a single axis. E.g.
% scatter(abs(randn(100,1)),randn(100,1),rand(100,1)*100,rand(100,1),'filled');colormap hot
% freezeColors; freezeColors(jicolorbar)
% hold on
% scatter(-abs(randn(100,1)),randn(100,1),rand(100,1)*100,rand(100,1),'filled');colormap cool
% freezeColors; freezeColors(colorbar)
%
% JICOLORBAR('...freeze') 'fix' added as suffix to other location strings causes the colorscale
% to be fixed and not changed by future changes to caxis or colormap. Useful with freezeColors.
% I don't think it's needed, is it? We can freeze and unfreeze the colorbar
% directly since it's based on an image.
% Clay M. Thompson 10-9-92
% Copyright (c) 1984-98 by The MathWorks, Inc.
% $Revision: 5.31 $ $Date: 1998/07/24 18:08:03 $
% If called with COLORBAR(H) or for an existing colorbar, don't change
% the NextPlot property.
% JRI Modified matlab function colorbar
% Catch colorbar('delete') special case -- must be called by the deleteFcn.
if nargin==1 && strcmp(loc,'delete')
ax = gcbo;
if strcmp(get(ax,'tag'),'JI_COLORBAR'), ax=get(ax,'parent'); end
ud = get(ax,'userdata');
if isfield(ud,'PlotHandle') && ishandle(ud.PlotHandle) && ...
isfield(ud,'origPos') && ~isempty(ud.origPos)
units = get(ud.PlotHandle,'units');
set(ud.PlotHandle,'units','normalized');
set(ud.PlotHandle,'position',ud.origPos);
set(ud.PlotHandle,'units',units);
end
if isfield(ud,'DeleteProxy') && ishandle(ud.DeleteProxy)
delete(ud.DeleteProxy)
end
% hl = legend;
% if hl.Visible
% legend % Update legend
% end
return
end
changeNextPlot = 1;
fs = get(gca,'fontsize')-1; %fontsize for colorbar axis
if nargin<1, loc = 'vert'; end
if ~strcmp(loc,'delete') && isempty(findstr(loc,'vert')), loc = ['vert' loc]; end
if nargin<2, tit = ''; 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(gcda,'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');
elseif strcmp(typ, 'scatter')
cdatamapping = 'scaled';
end
end
if ( strcmp(cdatamapping, 'scaled') )
% Treat images and surfaces alike if cdatamapping == 'scaled'
t = caxis;
d = (t(2) - t(1))/size(colormap,1);
t = [t(1)+d/2 t(2)-d/2];
else
if hasimage
t = [1, size(colormap,1)];
else
t = [1.5 size(colormap,1)+.5];
end
end
h = gcda;
if nargin==0
% Search for xisting colorbar
ch = get(findobj(gcf,'type','image','tag','JI_COLORBAR'),{'parent'}); ax = [];
for i=1:length(ch)
ud = get(ch{i},'userdata');
d = ud.PlotHandle;
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;
% Make sure image deletefcn doesn't trigger a colorbar('delete')
% for colorbar update
set(get(ax,'children'),'deletefcn','')
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(findstr(loc,'short'))
scale = 0.5; %height relative to axis height
else
scale = 1;
end
if isempty(ax)
units = get(h,'units'); set(h,'units','normalized')
pos = get(h,'Position');
[az,el] = view;
%JRI MODIFIED to not modify original axis size, and give a narrower bar
stripe = 0.04; edge = 0.01;
if ~isempty(findstr(loc,'wide'))
stripe = 0.07;
end
if all([az,el]==[0 90]), space = 0; else space = .1; end
%set(h,'Position',[pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)])
% original code:
%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)])
%rect = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
rect = [pos(1)+pos(3) pos(2)+(0.5*(1-scale)*pos(4)) stripe*pos(3) scale*pos(4)];
ud.origPos = pos;
% Create axes for stripe and
% create DeleteProxy object (an invisible text object in
% the target axes) so that the colorbar will be deleted
% properly.
ud.DeleteProxy = text('parent',h,'visible','off',...
'tag','ColorbarDeleteProxy',...
'handlevisibility','off',...
'deletefcn','eval(''delete(get(gcbo,''''userdata''''))'','''')');
ax = axes('Position', rect);
%JRI ADDITION--smaller font
set(ax,'fontsize',fs);
setappdata(ax,'NonDataObject',[]); % For DATACHILDREN.M
set(ud.DeleteProxy,'userdata',ax)
set(h,'units',units)
else
axes(ax);
ud = get(ax,'userdata');
end
% Create color stripe
n = size(colormap,1);
%%% JRI 10/14/03
%%% enable fixed colors, so when change colormap, colorbar doesn't
%%% change
if ~isempty(findstr(loc,'freeze'))
cm = colormap;
cc(:,1,1) = cm(:,1);
cc(:,1,2) = cm(:,2);
cc(:,1,3) = cm(:,3);
image([0 1],t,cc,'Tag','JI_COLORBAR','deletefcn','jicolorbar(''delete'')'); set(ax,'Ydir','normal')
else
image([0 1],t,(1:n)','Tag','JI_COLORBAR','deletefcn','jicolorbar(''delete'')'); set(ax,'Ydir','normal')
end
set(ax,'YAxisLocation','right')
set(ax,'xtick',[])
% set up axes deletefcn
set(ax,'tag','JI_Colorbar','deletefcn','jicolorbar(''delete'')')
title(tit)
elseif loc(1)=='h' % Append horizontal scale to top of current plot
if ~isempty(findstr(loc,'short'))
scale = 0.5; %height relative to axis height
else
scale = 1;
end
if isempty(ax)
%%JRI *** don't change axis position,
%% add option to scale length of colorbar
units = get(h,'units'); set(h,'units','normalized')
pos = get(h,'Position');
stripe = 0.04; space = 0.05;
% stripe = 0.075; space = 0.1;
% set(h,'Position',...
% [pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)])
% rect = [pos(1) pos(2) pos(3) stripe*pos(4)];
rect = [pos(1)+(0.5*(1-scale)*pos(3)) pos(2)-(stripe+space)*pos(4) scale*pos(3) stripe*pos(4)];
ud.origPos = pos;
% Create axes for stripe and
% create DeleteProxy object (an invisible text object in
% the target axes) so that the colorbar will be deleted
% properly.
ud.DeleteProxy = text('parent',h,'visible','off',...
'tag','ColorbarDeleteProxy',...
'handlevisibility','off',...
'deletefcn','eval(''delete(get(gcbo,''''userdata''''))'','''')');
ax = axes('Position', rect);
setappdata(ax,'NonDataObject',[]); % For DATACHILDREN.M
set(ud.DeleteProxy,'userdata',ax)
set(h,'units',units)
else
axes(ax);
ud = get(ax,'userdata');
end
% Create color stripe
n = size(colormap,1);
image(t,[0 1],(1:n),'Tag','JI_COLORBAR','deletefcn','jicolorbar(''delete'')'); set(ax,'Ydir','normal')
set(ax,'ytick',[])
% set up axes deletefcn
set(ax,'tag','JI_Colorbar','deletefcn','jicolorbar(''delete'')')
else
error('COLORBAR expects a handle, ''vert'', or ''horiz'' as input.')
end
if ~isfield(ud,'DeleteProxy'), ud.DeleteProxy = []; end
if ~isfield(ud,'origPos'), ud.origPos = []; end
ud.PlotHandle = h;
set(ax,'userdata',ud)
axes(h)
set(gcf,'NextPlot',origNextPlot)
% hl = legend;
% if hl.Visible
% legend % Update legend
% end
if nargout>0, handle = ax; end
%--------------------------------
function h = gcda
%GCDA Get current data axes
h = datachildren(gcf);
if isempty(h) || any(h == gca)
h = gca;
else
h = h(1);
end