1 | function h=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 | % - 'fontcolor' : default is 'k'
|
---|
10 | % - 'smallbars' : bars next to each tick (default is false)
|
---|
11 | % - 'position' : colorbar position in normalized units
|
---|
12 | % - 'orientation' : 'vertical' (default) or 'horizontal'
|
---|
13 | % - 'title' : colorbar title
|
---|
14 | % - 'xlabel' : colorbar x-label
|
---|
15 | % - 'ylabel' : colorbar y-label
|
---|
16 | % - 'tick' : specified values of tick labels
|
---|
17 | % - 'ticksep' : spacing between ticks
|
---|
18 | % - 'inverttickposition' : put ticks on the left hand side for vertical cb
|
---|
19 |
|
---|
20 | %check inputs
|
---|
21 | if nargin<3,
|
---|
22 | help manualcb
|
---|
23 | error('bad usage');
|
---|
24 | end
|
---|
25 | if zmin>zmax,
|
---|
26 | error('zmin should be smaller than zmax');
|
---|
27 | end
|
---|
28 |
|
---|
29 | %Get plot axes
|
---|
30 | mainaxes = gca;
|
---|
31 |
|
---|
32 | %process options
|
---|
33 | options = pairoptions(varargin{:});
|
---|
34 | if exist(options,'tick') & exist(options,'ticksep'),
|
---|
35 | error('only one of tick or ticksep can be specified');
|
---|
36 | end
|
---|
37 | fontsize = getfieldvalue(options,'fontsize',12);
|
---|
38 | fontcolor = getfieldvalue(options,'fontcolor','k');
|
---|
39 | smallbars = getfieldvalue(options,'smallbars',false);
|
---|
40 |
|
---|
41 | %Colorbar position
|
---|
42 | if ~exist(options,'position'),
|
---|
43 | position = plotboxpos;
|
---|
44 | xstart = position(1)+position(3)+0.01;
|
---|
45 | ystart = position(2);
|
---|
46 | width = .02;
|
---|
47 | height = position(4);
|
---|
48 | else
|
---|
49 | position = getfieldvalue(options,'position');
|
---|
50 | xstart = position(1);
|
---|
51 | ystart = position(2);
|
---|
52 | width = position(3);
|
---|
53 | height = position(4);
|
---|
54 | end
|
---|
55 | axes('Units','normalized','Position',[xstart ystart width height],'XTickLabel','','YTickLabel','','Visible','on');
|
---|
56 | xlim([0 1]);
|
---|
57 | ylim([0 1]);
|
---|
58 |
|
---|
59 | %Prepare ticks
|
---|
60 | if ~exist(options,'log'),
|
---|
61 | deltaz = getfieldvalue(options,'ticksep',dtick(zmax-zmin));
|
---|
62 | ztick = getfieldvalue(options,'tick',(deltaz*ceil(zmin/deltaz)):deltaz:zmax);
|
---|
63 | if (any(ztick>zmax) | any(ztick<zmin)),
|
---|
64 | error('one or more specified tick values falls outside of [zmin,zmax]');
|
---|
65 | end
|
---|
66 | ytick = (ztick-zmin)/(zmax-zmin);
|
---|
67 | else
|
---|
68 | %old method
|
---|
69 | ztick = getfieldvalue(options,'tick',round( logspace(log10(zmin),log10(zmax),8) ));
|
---|
70 | ytick = linspace(0,1,numel(ztick));
|
---|
71 |
|
---|
72 | %New method
|
---|
73 | test=logspace(-10,10,21);
|
---|
74 | pos=find(test>=zmin & test<=zmax);
|
---|
75 | ztick= test(pos);
|
---|
76 | ytick= (log(ztick) - log(zmin))/(log(zmax) - log(zmin));
|
---|
77 | end
|
---|
78 |
|
---|
79 | %Display colorbar
|
---|
80 | hold on
|
---|
81 | numcolors=size(cmap,1);
|
---|
82 | if 0,
|
---|
83 | %disappears somtimes
|
---|
84 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
85 | image_rgb = ind2rgb(repmat((1:numcolors)',1,10),cmap);
|
---|
86 | else
|
---|
87 | image_rgb = ind2rgb(repmat((1:numcolors),10,1),cmap);
|
---|
88 | end
|
---|
89 |
|
---|
90 | imagesc([0 1],[0 1],image_rgb);
|
---|
91 | else
|
---|
92 | %Creates triangles when exported as pdf
|
---|
93 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
94 | for i=1:numcolors,
|
---|
95 | patch([0,0,1,1],[(i-1)/numcolors,i/numcolors,i/numcolors,(i-1)/numcolors],0,'FaceColor',cmap(i,:),'Clipping','off','EdgeColor','none')
|
---|
96 | end
|
---|
97 | else
|
---|
98 | for i=1:numcolors,
|
---|
99 | patch([(i-1)/numcolors,i/numcolors,i/numcolors,(i-1)/numcolors],[0,0,1,1],0,'FaceColor',cmap(i,:),'Clipping','off','EdgeColor','none')
|
---|
100 | end
|
---|
101 | end
|
---|
102 | end
|
---|
103 | patch([0,0,1,1],[0,1,1,0],fontcolor,'FaceColor','none','Clipping','off','Edgecolor',fontcolor)
|
---|
104 |
|
---|
105 | %Add ticks
|
---|
106 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
107 | %Use FOR LOOP otherwise numbers are not correcly centered
|
---|
108 | if getfieldvalue(options,'inverttickposition',0)==1,
|
---|
109 | for i=1:length(ytick), text(-0.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','right','VerticalAlignment','middle','FontSize',fontsize,'Color',fontcolor); end
|
---|
110 | else
|
---|
111 | for i=1:length(ytick), text(1.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',fontsize,'Color',fontcolor); end
|
---|
112 | end
|
---|
113 | if smallbars,
|
---|
114 | for i=1:numel(ztick)
|
---|
115 | patch([0.8 1.0],[ytick(i) ytick(i)],fontcolor,'Edgecolor',fontcolor)
|
---|
116 | patch([0.0 0.2],[ytick(i) ytick(i)],fontcolor,'Edgecolor',fontcolor)
|
---|
117 | end
|
---|
118 | end
|
---|
119 | else
|
---|
120 | %Use FOR LOOP otherwise numbers are not correcly centered
|
---|
121 | for i=1:length(ytick), text(ytick(i),-0.5,num2str(ztick(i)),'HorizontalAlignment','center','VerticalAlignment','top','FontSize',fontsize,'Color',fontcolor); end
|
---|
122 | if smallbars,
|
---|
123 | for i=1:numel(ztick)
|
---|
124 | patch([ytick(i) ytick(i)],[0.8 1.0],[ytick(i) ytick(i)],fontcolor,'Edgecolor',fontcolor)
|
---|
125 | patch([ytick(i) ytick(i)],[0.0 0.2],[ytick(i) ytick(i)],fontcolor,'Edgecolor',fontcolor)
|
---|
126 | end
|
---|
127 | end
|
---|
128 | end
|
---|
129 |
|
---|
130 | if exist(options,'title'),
|
---|
131 | title(getfieldvalue(options,'title'),'FontSize',getfieldvalue(options,'titlefontsize',fontsize),'Color',fontcolor);
|
---|
132 | end
|
---|
133 | if exist(options,'ylabel'),
|
---|
134 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'horizontal'),
|
---|
135 | th=title(getfieldvalue(options,'title'),'FontSize',fontsize,'Color',fontcolor);
|
---|
136 | set(th,'Position',[ytick(end)+0.075,-0.3]);
|
---|
137 | elseif getfieldvalue(options,'inverttickposition',0)==1,
|
---|
138 | text(1.9,.7,getfieldvalue(options,'ylabel'),'HorizontalAlignment','right','VerticalAlignment','middle','FontSize',fontsize,'Color',fontcolor,'rotation',90);
|
---|
139 | else
|
---|
140 | ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize,'Color',fontcolor);
|
---|
141 | end
|
---|
142 | end
|
---|
143 |
|
---|
144 | %Back to original axes
|
---|
145 | h=gca;
|
---|
146 | if getfieldvalue(options,'showregion',0)==0,
|
---|
147 | %Do it this way in order to preserve the figure visibility
|
---|
148 | set(gcf,'CurrentAxes',mainaxes);
|
---|
149 | end
|
---|
150 |
|
---|
151 | function delta = dtick(range)
|
---|
152 | %Tick intervals
|
---|
153 | m = 10^floor(log10(range));
|
---|
154 | p = ceil(range/m);
|
---|
155 | if p <= 1, delta = .1*m;
|
---|
156 | elseif p == 2, delta = .2*m;
|
---|
157 | elseif p <= 5, delta = .5*m;
|
---|
158 | else delta = m;
|
---|
159 | end
|
---|