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