1 | function manualcb(zmin,zmax,cmap,varargin)
|
---|
2 | %MANUALCB - custom colorbar
|
---|
3 | %
|
---|
4 | % Usage:
|
---|
5 | % manualcb(min,max,colormap,options)
|
---|
6 | %
|
---|
7 | % Available options:
|
---|
8 | % - 'fontsize' : default is 12
|
---|
9 | % - 'smallbars' : bars next to each tick (default is false)
|
---|
10 | % - 'position' : colorbar position in normalized units
|
---|
11 | % - 'orientation' : 'vertical' (default) or 'horizontal'
|
---|
12 | % - 'title' : colorbar title
|
---|
13 | % - 'tick' : specified values of tick labels
|
---|
14 | % - 'ticksep' : spacing between ticks
|
---|
15 | % - 'inverttickposition' : put ticks on the left hand side for vertical cb
|
---|
16 |
|
---|
17 | %check inputs
|
---|
18 | if nargin<3,
|
---|
19 | help manualcb
|
---|
20 | error('bad usage');
|
---|
21 | end
|
---|
22 | if zmin>zmax,
|
---|
23 | error('zmin should be smaller than zmax');
|
---|
24 | end
|
---|
25 |
|
---|
26 | %Get plot axes
|
---|
27 | mainaxes = gca;
|
---|
28 |
|
---|
29 | %process options
|
---|
30 | options = pairoptions(varargin{:});
|
---|
31 | if exist(options,'tick') & exist(options,'ticksep'),
|
---|
32 | error('only one of tick or ticksep can be specified');
|
---|
33 | end
|
---|
34 | fontsize = getfieldvalue(options,'fontsize',12);
|
---|
35 | smallbars = getfieldvalue(options,'smallbars',false);
|
---|
36 |
|
---|
37 | %Colorbar position
|
---|
38 | if ~exist(options,'position'),
|
---|
39 | position = plotboxpos;
|
---|
40 | xstart = position(1)+position(3)+0.01;
|
---|
41 | ystart = position(2);
|
---|
42 | width = .02;
|
---|
43 | height = position(4);
|
---|
44 | else
|
---|
45 | position = getfieldvalue(options,'position');
|
---|
46 | xstart = position(1);
|
---|
47 | ystart = position(2);
|
---|
48 | width = position(3);
|
---|
49 | height = position(4);
|
---|
50 | end
|
---|
51 | axes('Units','normalized','Position',[xstart ystart width height],'XTickLabel','','YTickLabel','','Visible','on');
|
---|
52 | xlim([0 1]);
|
---|
53 | ylim([0 1]);
|
---|
54 |
|
---|
55 | %Prepare ticks
|
---|
56 | if ~exist(options,'log'),
|
---|
57 | deltaz = getfieldvalue(options,'ticksep',dtick(zmax-zmin));
|
---|
58 | ztick = getfieldvalue(options,'tick',(deltaz*ceil(zmin/deltaz)):deltaz:zmax);
|
---|
59 | if (any(ztick>zmax) | any(ztick<zmin)),
|
---|
60 | error('one or more specified tick values falls outside of [zmin,zmax]');
|
---|
61 | end
|
---|
62 | ytick = (ztick-zmin)/(zmax-zmin);
|
---|
63 | else
|
---|
64 | ztick = getfieldvalue(options,'tick',round(logspace(log(zmin)/log(10),log(zmax)/log(10),8)));
|
---|
65 | ytick = linspace(0,1,numel(ztick));
|
---|
66 | end
|
---|
67 |
|
---|
68 | %Display colorbar
|
---|
69 | hold on
|
---|
70 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
71 | image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2),cmap);
|
---|
72 | else
|
---|
73 | image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2)',cmap);
|
---|
74 | end
|
---|
75 | imagesc([0 1],[0 1],image_rgb);
|
---|
76 | patch([0,0,1,1],[0,1,1,0],'k','FaceColor','none','Clipping','off')
|
---|
77 |
|
---|
78 | %Add ticks
|
---|
79 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
80 | %Use FOR LOOP otherwise numbers are not correcly centered
|
---|
81 | if getfieldvalue(options,'inverttickposition',0)==1,
|
---|
82 | for i=1:length(ytick), text(-0.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','right','VerticalAlignment','middle','FontSize',fontsize); end
|
---|
83 | else
|
---|
84 | for i=1:length(ytick), text(1.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',fontsize); end
|
---|
85 | end
|
---|
86 | if smallbars,
|
---|
87 | for i=1:numel(ztick)
|
---|
88 | patch([0.8 1.0],[ytick(i) ytick(i)],'k')
|
---|
89 | patch([0.0 0.2],[ytick(i) ytick(i)],'k')
|
---|
90 | end
|
---|
91 | end
|
---|
92 | else
|
---|
93 | %Use FOR LOOP otherwise numbers are not correcly centered
|
---|
94 | for i=1:length(ytick), text(ytick(i),-0.5,num2str(ztick(i)),'HorizontalAlignment','center','VerticalAlignment','top','FontSize',fontsize); end
|
---|
95 | if smallbars,
|
---|
96 | for i=1:numel(ztick)
|
---|
97 | patch([ytick(i) ytick(i)],[0.8 1.0],[ytick(i) ytick(i)],'k')
|
---|
98 | patch([ytick(i) ytick(i)],[0.0 0.2],[ytick(i) ytick(i)],'k')
|
---|
99 | end
|
---|
100 | end
|
---|
101 | end
|
---|
102 |
|
---|
103 | if exist(options,'title'),
|
---|
104 | title(getfieldvalue(options,'title'),'FontSize',getfieldvalue(options,'titlefontsize',fontsize));
|
---|
105 | end
|
---|
106 | if exist(options,'ylabel'),
|
---|
107 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'horizontal'),
|
---|
108 | th=title(getfieldvalue(options,'title'),'FontSize',fontsize);
|
---|
109 | set(th,'Position',[ytick(end)+0.075,-0.3]);
|
---|
110 | else
|
---|
111 | ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize);
|
---|
112 | end
|
---|
113 | end
|
---|
114 |
|
---|
115 | %Back to original axes
|
---|
116 | if getfieldvalue(options,'showregion',0)==0,
|
---|
117 | axes(mainaxes);
|
---|
118 | end
|
---|
119 |
|
---|
120 | function delta = dtick(range)
|
---|
121 | %Tick intervals
|
---|
122 | m = 10^floor(log10(range));
|
---|
123 | p = ceil(range/m);
|
---|
124 | if p <= 1, delta = .1*m;
|
---|
125 | elseif p == 2, delta = .2*m;
|
---|
126 | elseif p <= 5, delta = .5*m;
|
---|
127 | else delta = m;
|
---|
128 | end
|
---|