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