-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcolorbarps.m
163 lines (139 loc) · 4.34 KB
/
colorbarps.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function h = colorbarps(varargin)
% colorbarps places a horizontal colorbar in the lower left corner of a
% map of Antarctica.
%
%% Syntax
%
% colorbarps
% colorbarps('PropertyName',PropertyValue,...)
% h = colorbarps(...)
%
%% Description
%
% colorbarps places a horizontal colorbar in the lower left corner of the
% current axes. Its placement is intended to avoid covering any permanent
% ice in a standard map of Antarctica.
%
% colorbarps('PropertyName',PropertyValue,...) specifies any colorbar
% properties.
%
% h = colorbarps(...) returns a handle of the colorbar.
%
%% Example
%
% figure
% antbounds % draws an outline of the continent
% colorbarps
%
%% Citing Antarctic Mapping Tools
% This function was developed for Antarctic Mapping Tools for Matlab (AMT). If AMT is useful for you,
% please cite our paper:
%
% Greene, C. A., Gwyther, D. E., & Blankenship, D. D. Antarctic Mapping Tools for Matlab.
% Computers & Geosciences. 104 (2017) pp.151-157.
% http://dx.doi.org/10.1016/j.cageo.2016.08.003
%
% @article{amt,
% title={{Antarctic Mapping Tools for \textsc{Matlab}}},
% author={Greene, Chad A and Gwyther, David E and Blankenship, Donald D},
% journal={Computers \& Geosciences},
% year={2017},
% volume={104},
% pages={151--157},
% publisher={Elsevier},
% doi={10.1016/j.cageo.2016.08.003},
% url={http://www.sciencedirect.com/science/article/pii/S0098300416302163}
% }
%
%% Author Info
% Written by Chad Greene of NASA Jet Propulsion Laboratory, October 2021.
%
% See also colorbar.
% Position of current axes:
axpos = plotboxpos(gca);
% Position of colorbar relative to current axes:
cbpos = [axpos(1)+axpos(3)/30 axpos(2)+axpos(4)/30 axpos(3)/2.5 axpos(3)/30];
% Set it real nice:
h = colorbar('south');
h.Position = cbpos;
h.AxisLocation = 'in';
if nargout==0
clear h
end
end
function pos = plotboxpos(h)
%PLOTBOXPOS Returns the position of the plotted axis region
%
% pos = plotboxpos(h)
%
% This function returns the position of the plotted region of an axis,
% which may differ from the actual axis position, depending on the axis
% limits, data aspect ratio, and plot box aspect ratio. The position is
% returned in the same units as the those used to define the axis itself.
% This function can only be used for a 2D plot.
%
% Input variables:
%
% h: axis handle of a 2D axis (if ommitted, current axis is used).
%
% Output variables:
%
% pos: four-element position vector, in same units as h
% Copyright 2010 Kelly Kearney
% Check input
if nargin < 1
h = gca;
end
if ~ishandle(h) || ~strcmp(get(h,'type'), 'axes')
error('Input must be an axis handle');
end
% Get position of axis in pixels
currunit = get(h, 'units');
set(h, 'units', 'pixels');
axisPos = get(h, 'Position');
set(h, 'Units', currunit);
% Calculate box position based axis limits and aspect ratios
darismanual = strcmpi(get(h, 'DataAspectRatioMode'), 'manual');
pbarismanual = strcmpi(get(h, 'PlotBoxAspectRatioMode'), 'manual');
if ~darismanual && ~pbarismanual
pos = axisPos;
else
dx = diff(get(h, 'XLim'));
dy = diff(get(h, 'YLim'));
dar = get(h, 'DataAspectRatio');
pbar = get(h, 'PlotBoxAspectRatio');
limDarRatio = (dx/dar(1))/(dy/dar(2));
pbarRatio = pbar(1)/pbar(2);
axisRatio = axisPos(3)/axisPos(4);
if darismanual
if limDarRatio > axisRatio
pos(1) = axisPos(1);
pos(3) = axisPos(3);
pos(4) = axisPos(3)/limDarRatio;
pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
else
pos(2) = axisPos(2);
pos(4) = axisPos(4);
pos(3) = axisPos(4) * limDarRatio;
pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
end
elseif pbarismanual
if pbarRatio > axisRatio
pos(1) = axisPos(1);
pos(3) = axisPos(3);
pos(4) = axisPos(3)/pbarRatio;
pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
else
pos(2) = axisPos(2);
pos(4) = axisPos(4);
pos(3) = axisPos(4) * pbarRatio;
pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
end
end
end
% Convert plot box position to the units used by the axis
temp = axes('Units', 'Pixels', 'Position', pos, 'Visible', 'off', 'parent', get(h, 'parent'));
set(temp, 'Units', currunit);
pos = get(temp, 'position');
delete(temp);
end