Switch to unified view

a b/Supporting Functions/kakearney-boundedline-pkg-8179f9a/README.md
1
2
# boundedline.m: line with shaded error/confidence bounds
3
4
5
Author: Kelly Kearney
6
7
8
This repository includes the code for the `boundedline.m` Matlab function and the accompanying `outlinebounds.m` function, along with all dependent functions required to run them.
9
10
11
The `boundedline` function allows a user to easily plot and line with a shaded patch around it.  Ths sort of plot is often used to indicate uncertainty intervals or error bounds around a line.
12
13
14
15
## Contents
16
17
18
- Getting started        
19
- Syntax        
20
- Example 1: Plotting lines using various syntax options        
21
- Example 2: Filling gaps        
22
- Contributions
23
24
## Getting started
25
26
27
**Prerequisites**
28
29
30
This function requires Matlab R14 or later.
31
32
33
**Downloading and installation**
34
35
36
This code can be downloaded from [Github](https://github.com/kakearney/boundedline-pkg/) or the [MatlabCentral File Exchange](http://www.mathworks.com/matlabcentral/fileexchange/27485-boundedline-m).  The File Exchange entry is updated daily from the GitHub repository.
37
38
39
**Matlab Search Path**
40
41
42
The following folders need to be added to your Matlab Search path (via `addpath`, `pathtool`, etc.):
43
44
45
46
```matlab
47
boundedline-pkg/Inpaint_nans
48
boundedline-pkg/boundedline
49
boundedline-pkg/catuneven
50
boundedline-pkg/singlepatch
51
```
52
53
54
55
## Syntax
56
57
58
`boundedline(x, y, b)` plots a line with coordinates given by `x` and `y`, surrounded by a patch extending a certain distance `b` above/below that line.  The dimensions of the `x`, `y`, and `b` arrays can vary to allow for multiple lines to be plotted at once, and for patch bounds to be either constant or varying along the length of the line.  See function header help for full details of these variations.
59
60
61
`boundedline(..., 'alpha')` renders the bounded area patch using a partially-transparent patch the same color as the corresponding line(s). If not included, the bounded area will use a fully-opaque patch in a lighter shade of the corresponding line color.
62
63
64
`boundedline(..., 'transparency', transp)` indicates the tranparency or intensity of the bounds patch, using a scalar between 0 and 1.  Default is 0.2.
65
66
67
`boundedline(..., 'orientation', orient)` indicates the orientation of the bounds.  Orientation can be either `'vert'` for vertical (y-direction) bounds, or `'horiz'` for horizontal (x-direction) bounds.  Default is `'vert'`.
68
69
70
`boundedline(..., 'nan', nanflag)` indicates how the bounds patch should handle NaNs in the line coordinates or bounds values.  Options are `'fill'`, to smooth over the gap using neighboring values, `'gap'` to leave a blank space in the patch at those points, or `'remove'` to drop the NaN-points entirely, leading to linear interpolation of the gap in the patch.  See examples below for more details on these options.
71
72
73
`boundedline(..., 'cmap', cmap)` colors the lines (in order of plotting) acording to the colors in this n x 3 colormap array, overriding any linespec or default colors.
74
75
76
`boundedline(..., ax)` plots the bounded line to the axis indicated by handle `ax`.  If not included, the current axis is used.
77
78
79
`[hl, hp] = boundedline(...)` returns the handles the resulting line and patch object(s).
80
81
82
`hout = outlinebounds(hl, hp)` adds an outline to the bounds patch generated by `boundedline`, returning the handle of the resulting line object in `hout`.
83
84
85
Full details of all input and output variables for both functions can be accessed via the `help` function.
86
87
88
89
## Example 1: Plotting lines using various syntax options
90
91
92
This example builds the 4-panel example image used on the MatlabCentral File Exchange, which shows several different methods for supplying line coordinates, bounds coordinates, and shading options.
93
94
95
The first axis plots two lines using the LineSpec option for input, which allows yoy to set line color, line color, and marker type for each line. The bounds on the first line vary over x, while the bounds on the second line are constant for all x. An outline is added to the bounds so the overlapping region can be seen more clearly.
96
97
98
99
```matlab
100
x = linspace(0, 2*pi, 50);
101
y1 = sin(x);
102
y2 = cos(x);
103
e1 = rand(size(y1))*.5+.5;
104
e2 = [.25 .5];
105
106
ax(1) = subplot(2,2,1);
107
[l,p] = boundedline(x, y1, e1, '-b*', x, y2, e2, '--ro');
108
outlinebounds(l,p);
109
title('Opaque bounds, with outline');
110
axis tight;
111
```
112
113
114
![](./readmeExtras/README_01.png)
115
116
For our second axis, we use the same 2 lines, and this time assign x-varying bounds to both lines.  Rather than using the LineSpec syntax, this  example uses the default color order to assign the colors of the lines and patches.  I also turn on the `'alpha'` option, which renders the patch wit partial transparency.
117
118
119
120
```matlab
121
ax(2) = subplot(2,2,2);
122
boundedline(x, [y1;y2], rand(length(y1),2,2)*.5+.5, 'alpha');
123
title('Transparent bounds');
124
axis tight;
125
```
126
127
128
![](./readmeExtras/README_02.png)
129
130
The bounds can also be assigned to a horizontal orientation, for a case where the x-axis represents the dependent variable.  In this case, the scalar error bound value applies to both lines and both sides of the lines.
131
132
133
134
```matlab
135
ax(3) = subplot(2,2,3);
136
boundedline([y1;y2], x, e1(1), 'orientation', 'horiz')
137
title('Horizontal bounds');
138
axis tight;
139
```
140
141
142
![](./readmeExtras/README_03.png)
143
144
Rather than use a LineSpec or the default color order, a colormap array can be used to assign colors.  In this case, increasingly-narrower bounds are added on top of the same line.
145
146
147
148
```matlab
149
ax(4) = subplot(2,2,4);
150
boundedline(x, repmat(y1, 4,1), permute(0.5:-0.1:0.2, [3 1 2]), ...
151
'cmap', cool(4), ...
152
'transparency', 0.5);
153
title('Multiple bounds using colormap');
154
155
set(ax([1 2 4]), 'xlim', [0 2*pi]);
156
set(ax(3), 'ylim', [0 2*pi]);
157
axis tight;
158
```
159
160
161
![](./readmeExtras/README_04.png)
162
163
164
## Example 2: Filling gaps
165
166
167
If you plot a line with one or more NaNs in either the `x` or `y` vector, the NaN location is rendered as a missing marker with a gap in the line. However, the `patch` command does not handle NaNs gracefully; it simply fails to show the patch at all if any of the coordinates include NaNs.
168
169
170
Because of this, the expected behavior of the patch part of boundedline when confronted with a NaN in either the bounds array (`b`) or the x/y-coordinates of the line (which are used to calculate the patch coordinates) is ambiguous.  I offer a few options.
171
172
173
Before I demonstrate the options, I'll create a dataset that has a few different types of gaps:
174
175
176
177
```matlab
178
x = linspace(0, 2*pi, 50);
179
y = sin(x);
180
b = [ones(size(y))*0.2; rand(size(y))*.5+.5]';
181
182
y(10)   = NaN;  % NaN in the line but not bounds
183
b(20,1) = NaN;  % NaN in lower bound but not line
184
b(30,2) = NaN;  % NaN in upper bound but not line
185
b(40,:) = NaN;  % NaN in both sides of bound but not line
186
```
187
188
189
Here's what that looks like in an errorbar plot.
190
191
192
193
```matlab
194
figure;
195
he = errorbar(x,y,b(:,1), b(:,2), '-bo');
196
197
198
line([x([10 20 30 40]); x([10 20 30 40])], [ones(1,4)*-2;ones(1,4)*2], ...
199
'color', ones(1,3)*0.5, 'linestyle', ':');
200
text(x(10), sin(x(10))-0.2, {'\uparrow','Line','gap'}, 'vert', 'top', 'horiz', 'center');
201
text(x(20), sin(x(20))-0.2, {'\uparrow','Lower','bound','gap'}, 'vert', 'top', 'horiz', 'center');
202
text(x(30), sin(x(30))-0.2, {'\uparrow','Upper','bound','gap'}, 'vert', 'top', 'horiz', 'center');
203
text(x(40), sin(x(40))-0.2, {'\uparrow','Two-sided','bound','gap'}, 'vert', 'top', 'horiz', 'center');
204
205
axis tight equal;
206
```
207
208
209
![](./readmeExtras/README_05.png)
210
211
The default method for dealing with NaNs in boundedline is to leave the gap in the line, but smooth over the gap in the bounds based on the neighboring points.  This option can be nice if you only have one or two missing points, and you're not interested in emphasizing those gaps in your plot:
212
213
214
215
```matlab
216
delete(he);
217
[hl,hp] = boundedline(x,y,b,'-bo', 'nan', 'fill');
218
ho = outlinebounds(hl,hp);
219
set(ho, 'linestyle', ':', 'color', 'r', 'marker', '.');
220
```
221
222
223
![](./readmeExtras/README_06.png)
224
225
I've added bounds outlines in a contrasting color so you can see how I'm handling individual points.
226
227
228
The second option leaves a full gap in the patch for any NaN.  I considered allowing one-sided gaps, but couldn't think of a good way to distinguish a gap from a zero-valued bound.  I'm open to suggestions if you have any (email me).
229
230
231
232
```matlab
233
delete([hl hp ho]);
234
[hl,hp] = boundedline(x,y,b,'-bo', 'nan', 'gap');
235
ho = outlinebounds(hl,hp);
236
set(ho, 'linestyle', ':', 'color', 'r', 'marker', '.');
237
```
238
239
240
![](./readmeExtras/README_07.png)
241
242
The final option removes points from the patch that are NaNs.  The visual result is very similar to the fill option, but the missing points are apparent if you plot the bounds outlines.
243
244
245
246
```matlab
247
delete([hl hp ho]);
248
[hl,hp] = boundedline(x,y,b,'-bo', 'nan', 'remove');
249
ho = outlinebounds(hl,hp);
250
set(ho, 'linestyle', ':', 'color', 'r', 'marker', '.');
251
```
252
253
254
![](./readmeExtras/README_08.png)
255
256
257
## Contributions
258
259
260
Community contributions to this package are welcome!
261
262
263
To report bugs, please submit [an issue](https://github.com/kakearney/boundedline-pkg/issues) on GitHub and include:
264
265
266
267
- your operating system
268
- your version of Matlab and all relevant toolboxes (type `ver` at the Matlab command line to get this info)
269
- code/data to reproduce the error or buggy behavior, and the full text of any error messages received
270
271
Please also feel free to submit enhancement requests, or to send pull requests (via GitHub) for bug fixes or new features.
272
273
274
I do monitor the MatlabCentral FileExchange entry for any issues raised in the comments, but would prefer to track issues on GitHub.
275
276
277
278
<sub>[Published with MATLAB R2016b]("http://www.mathworks.com/products/matlab/")</sub>