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