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

Last change on this file since 14462 was 14462, checked in by Mathieu Morlighem, 12 years ago

BUG: fixed same bug for vertical colorbars

  • Property svn:executable set to *
File size: 3.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
[14402]15
16%check inputs
17if nargin<3,
[14404]18 help manualcb
[14402]19 error('bad usage');
20end
21if zmin>zmax,
22 error('zmin should be smaller than zmax');
23end
24
[14461]25%Get plot axes
26mainaxes = gca;
27
[14402]28%process options
29options = pairoptions(varargin{:});
[14417]30if exist(options,'tick') & exist(options,'ticksep'),
31 error('only one of tick or ticksep can be specified');
32end
[14402]33fontsize = getfieldvalue(options,'fontsize',12);
34smallbars = getfieldvalue(options,'smallbars',false);
35
36%Colorbar position
37if ~exist(options,'position'),
38 position = plotboxpos;
39 xstart = position(1)+position(3)+0.01;
40 ystart = position(2);
41 width = .02;
42 height = position(4);
43else
44 position = getfieldvalue(options,'position');
45 xstart = position(1);
46 ystart = position(2);
47 width = position(3);
48 height = position(4);
49end
50axes('Units','normalized','Position',[xstart ystart width height],'XTickLabel','','YTickLabel','','Visible','on');
51xlim([0 1]);
52ylim([0 1]);
53
[14417]54%Prepare ticks
55deltaz = getfieldvalue(options,'ticksep',dtick(zmax-zmin));
56ztick = getfieldvalue(options,'tick',(deltaz*ceil(zmin/deltaz)):deltaz:zmax);
57if (any(ztick>zmax) | any(ztick<zmin)),
58 error('one or more specified tick values falls outside of [zmin,zmax]');
59end
[14402]60ytick = (ztick-zmin)/(zmax-zmin);
61
[14417]62%Display colorbar
[14402]63hold on
64if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
65 image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2),cmap);
66else
67 image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2)',cmap);
68end
69imagesc([0 1],[0 1],image_rgb);
70patch([0,0,1,1],[0,1,1,0],'k','FaceColor','none','Clipping','off')
71
72%Add ticks
73if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
[14462]74 %Use FOR LOOP otherwise numbers are not correcly centered
75 for i=1:length(ytick), text(1.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',fontsize); end
[14402]76 if smallbars,
77 for i=1:numel(ztick)
78 patch([0.8 1.0],[ytick(i) ytick(i)],'k')
79 patch([0.0 0.2],[ytick(i) ytick(i)],'k')
80 end
81 end
82else
[14461]83 %Use FOR LOOP otherwise numbers are not correcly centered
84 for i=1:length(ytick), text(ytick(i),-0.5,num2str(ztick(i)),'HorizontalAlignment','center','VerticalAlignment','top','FontSize',fontsize); end
[14402]85 if smallbars,
86 for i=1:numel(ztick)
87 patch([ytick(i) ytick(i)],[0.8 1.0],[ytick(i) ytick(i)],'k')
88 patch([ytick(i) ytick(i)],[0.0 0.2],[ytick(i) ytick(i)],'k')
89 end
90 end
91end
92
93if exist(options,'title'),
94 title(getfieldvalue(options,'title'),'FontSize',fontsize);
95end
[14461]96if exist(options,'ylabel'),
97 ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize);
98end
[14402]99
[14461]100%Back to original axes
101axes(mainaxes);
102
[14402]103function delta = dtick(range)
104%Tick intervals
105m = 10^floor(log10(range));
106p = ceil(range/m);
107if p <= 1, delta = .1*m;
108elseif p == 2, delta = .2*m;
109elseif p <= 5, delta = .5*m;
110else delta = m;
111end
Note: See TracBrowser for help on using the repository browser.