| 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 | 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
|
|---|
| 85 | end
|
|---|
| 86 |
|
|---|
| 87 | %Display colorbar
|
|---|
| 88 | hold on
|
|---|
| 89 | numcolors=size(cmap,1);
|
|---|
| 90 | if 0,
|
|---|
| 91 | %disappears somtimes
|
|---|
| 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);
|
|---|
| 99 | else
|
|---|
| 100 | %Creates triangles when exported as pdf
|
|---|
| 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
|
|---|
| 109 | end
|
|---|
| 110 | end
|
|---|
| 111 | patch([0,0,1,1],[0,1,1,0],fontcolor,'FaceColor','none','Clipping','off','Edgecolor',fontcolor)
|
|---|
| 112 |
|
|---|
| 113 | %Add ticks
|
|---|
| 114 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
|---|
| 115 | %Use FOR LOOP otherwise numbers are not correcly centered
|
|---|
| 116 | if getfieldvalue(options,'inverttickposition',0)==1,
|
|---|
| 117 | for i=1:length(ytick), text(-0.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','right','VerticalAlignment','middle','FontSize',fontsize,'Color',fontcolor); end
|
|---|
| 118 | else
|
|---|
| 119 | for i=1:length(ytick), text(1.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',fontsize,'Color',fontcolor); end
|
|---|
| 120 | end
|
|---|
| 121 | if smallbars,
|
|---|
| 122 | for i=1:numel(ztick)
|
|---|
| 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)
|
|---|
| 125 | end
|
|---|
| 126 | end
|
|---|
| 127 | else
|
|---|
| 128 | %Use FOR LOOP otherwise numbers are not correcly centered
|
|---|
| 129 | for i=1:length(ytick), text(ytick(i),-0.5,num2str(ztick(i)),'HorizontalAlignment','center','VerticalAlignment','top','FontSize',fontsize,'Color',fontcolor); end
|
|---|
| 130 | if smallbars,
|
|---|
| 131 | for i=1:numel(ztick)
|
|---|
| 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)
|
|---|
| 134 | end
|
|---|
| 135 | end
|
|---|
| 136 | end
|
|---|
| 137 |
|
|---|
| 138 | if exist(options,'title'),
|
|---|
| 139 | title(getfieldvalue(options,'title'),'FontSize',getfieldvalue(options,'titlefontsize',fontsize),'Color',fontcolor);
|
|---|
| 140 | end
|
|---|
| 141 | if exist(options,'ylabel'),
|
|---|
| 142 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'horizontal'),
|
|---|
| 143 | th=title(getfieldvalue(options,'title'),'FontSize',fontsize,'Color',fontcolor);
|
|---|
| 144 | set(th,'Position',[ytick(end)+0.075,-0.3]);
|
|---|
| 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);
|
|---|
| 147 | else
|
|---|
| 148 | ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize,'Color',fontcolor);
|
|---|
| 149 | end
|
|---|
| 150 | end
|
|---|
| 151 |
|
|---|
| 152 | %Back to original axes
|
|---|
| 153 | h=gca;
|
|---|
| 154 | if getfieldvalue(options,'showregion',0)==0,
|
|---|
| 155 | %Do it this way in order to preserve the figure visibility
|
|---|
| 156 | set(gcf,'CurrentAxes',mainaxes);
|
|---|
| 157 | end
|
|---|
| 158 |
|
|---|
| 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
|
|---|