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

Last change on this file since 15211 was 15211, checked in by cborstad, 12 years ago

don't return to main axes if showregion is used, it covers up the inset with the main axes

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1function 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% - 'smallbars' : bars next to each tick (default is false)
10% - 'position' : colorbar position in normalized units
11% - 'orientation' : 'vertical' (default) or 'horizontal'
12% - 'title' : colorbar title
13% - 'tick' : specified values of tick labels
14% - 'ticksep' : spacing between ticks
15
16%check inputs
17if nargin<3,
18 help manualcb
19 error('bad usage');
20end
21if zmin>zmax,
22 error('zmin should be smaller than zmax');
23end
24
25%Get plot axes
26mainaxes = gca;
27
28%process options
29options = pairoptions(varargin{:});
30if exist(options,'tick') & exist(options,'ticksep'),
31 error('only one of tick or ticksep can be specified');
32end
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
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
60ytick = (ztick-zmin)/(zmax-zmin);
61
62%Display colorbar
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'),
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
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
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
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 if strcmpi(getfieldvalue(options,'orientation','vertical'),'horizontal'),
95 th=title(getfieldvalue(options,'title'),'FontSize',fontsize);
96 set(th,'Position',[ytick(end)+0.075,-0.3]);
97 else
98 title(getfieldvalue(options,'title'),'FontSize',fontsize);
99 end
100end
101if exist(options,'ylabel'),
102 ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize);
103end
104
105%Back to original axes
106if strcmpi(getfieldvalue(options,'showregion',0),0)
107 axes(mainaxes);
108end
109
110function delta = dtick(range)
111%Tick intervals
112m = 10^floor(log10(range));
113p = ceil(range/m);
114if p <= 1, delta = .1*m;
115elseif p == 2, delta = .2*m;
116elseif p <= 5, delta = .5*m;
117else delta = m;
118end
Note: See TracBrowser for help on using the repository browser.