source: issm/trunk-jpl/src/m/plot/manualcb.m@ 20463

Last change on this file since 20463 was 20463, checked in by Mathieu Morlighem, 9 years ago

CHG: found a workaround for the incomplete colorbar

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