-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
error_and_rangebars.jl
292 lines (240 loc) · 9.35 KB
/
error_and_rangebars.jl
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
errorbars(x, y, error_both; kwargs...)
errorbars(x, y, error_low, error_high; kwargs...)
errorbars(x, y, error_low_high; kwargs...)
errorbars(xy, error_both; kwargs...)
errorbars(xy, error_low, error_high; kwargs...)
errorbars(xy, error_low_high; kwargs...)
errorbars(xy_error_both; kwargs...)
errorbars(xy_error_low_high; kwargs...)
Plots errorbars at xy positions, extending by errors in the given `direction`.
If you want to plot intervals from low to high values instead of relative errors, use `rangebars`.
## Attributes
$(ATTRIBUTES)
"""
@recipe(Errorbars) do scene
Theme(
whiskerwidth = 0,
color = theme(scene, :linecolor),
linewidth = theme(scene, :linewidth),
direction = :y,
visible = theme(scene, :visible),
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
inspectable = theme(scene, :inspectable),
transparency = false
)
end
"""
rangebars(val, low, high; kwargs...)
rangebars(val, low_high; kwargs...)
rangebars(val_low_high; kwargs...)
Plots rangebars at `val` in one dimension, extending from `low` to `high` in the other dimension
given the chosen `direction`.
If you want to plot errors relative to a reference value, use `errorbars`.
## Attributes
$(ATTRIBUTES)
"""
@recipe(Rangebars) do scene
Theme(
whiskerwidth = 0,
color = theme(scene, :linecolor),
linewidth = theme(scene, :linewidth),
direction = :y,
visible = theme(scene, :visible),
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
inspectable = theme(scene, :inspectable),
transparency = false
)
end
### conversions for errorbars
function Makie.convert_arguments(::Type{<:Errorbars}, x, y, error_both)
xyerr = broadcast(x, y, error_both) do x, y, e
Vec4f(x, y, e, e)
end
(xyerr,)
end
function Makie.convert_arguments(::Type{<:Errorbars}, x, y, error_low, error_high)
xyerr = broadcast(Vec4f, x, y, error_low, error_high)
(xyerr,)
end
function Makie.convert_arguments(::Type{<:Errorbars}, x, y, error_low_high::AbstractVector{<:VecTypes{2}})
xyerr = broadcast(x, y, error_low_high) do x, y, (el, eh)
Vec4f(x, y, el, eh)
end
(xyerr,)
end
function Makie.convert_arguments(::Type{<:Errorbars}, xy::AbstractVector{<:VecTypes{2}}, error_both)
xyerr = broadcast(xy, error_both) do (x, y), e
Vec4f(x, y, e, e)
end
(xyerr,)
end
function Makie.convert_arguments(::Type{<:Errorbars}, xy::AbstractVector{<:VecTypes{2}}, error_low, error_high)
xyerr = broadcast(xy, error_low, error_high) do (x, y), el, eh
Vec4f(x, y, el, eh)
end
(xyerr,)
end
function Makie.convert_arguments(::Type{<:Errorbars}, xy::AbstractVector{<:VecTypes{2}}, error_low_high::AbstractVector{<:VecTypes{2}})
xyerr = broadcast(xy, error_low_high) do (x, y), (el, eh)
Vec4f(x, y, el, eh)
end
(xyerr,)
end
function Makie.convert_arguments(::Type{<:Errorbars}, xy_error_both::AbstractVector{<:VecTypes{3}})
xyerr = broadcast(xy_error_both) do (x, y, e)
Vec4f(x, y, e, e)
end
(xyerr,)
end
### conversions for rangebars
function Makie.convert_arguments(::Type{<:Rangebars}, val, low, high)
val_low_high = broadcast(Vec3f, val, low, high)
(val_low_high,)
end
function Makie.convert_arguments(::Type{<:Rangebars}, val, low_high)
val_low_high = broadcast(val, low_high) do val, (low, high)
Vec3f(val, low, high)
end
(val_low_high,)
end
### the two plotting functions create linesegpairs in two different ways
### and then hit the same underlying implementation in `_plot_bars!`
function Makie.plot!(plot::Errorbars{T}) where T <: Tuple{AbstractVector{<:VecTypes{4}}}
x_y_low_high = plot[1]
is_in_y_direction = lift(plot, plot.direction) do dir
if dir === :y
true
elseif dir === :x
false
else
error("Invalid direction $dir. Options are :x and :y.")
end
end
linesegpairs = lift(plot, x_y_low_high, is_in_y_direction) do x_y_low_high, in_y
output = sizehint!(Point2f[], 2length(x_y_low_high))
for (x, y, l, h) in x_y_low_high
if in_y
push!(output, Point2f(x, y - l), Point2f(x, y + h))
else
push!(output, Point2f(x - l, y), Point2f(x + h, y))
end
end
return output
end
_plot_bars!(plot, linesegpairs, is_in_y_direction)
end
function Makie.plot!(plot::Rangebars{T}) where T <: Tuple{AbstractVector{<:VecTypes{3}}}
val_low_high = plot[1]
is_in_y_direction = lift(plot, plot.direction) do dir
if dir === :y
return true
elseif dir === :x
return false
else
error("Invalid direction $dir. Options are :x and :y.")
end
end
linesegpairs = lift(plot, val_low_high, is_in_y_direction) do vlh, in_y
output = sizehint!(Point2f[], 2length(vlh))
for (v, l, h) in vlh
if in_y
push!(output, Point2f(v, l), Point2f(v, h))
else
push!(output, Point2f(l, v), Point2f(h, v))
end
end
return output
end
_plot_bars!(plot, linesegpairs, is_in_y_direction)
end
function _plot_bars!(plot, linesegpairs, is_in_y_direction)
f_if(condition, f, arg) = condition ? f(arg) : arg
@extract plot (whiskerwidth, color, linewidth, visible, colormap, colorscale, colorrange, inspectable, transparency)
scene = parent_scene(plot)
whiskers = lift(plot, linesegpairs, scene.camera.projectionview, plot.model,
scene.px_area, transform_func(plot), whiskerwidth) do endpoints, _, _, _, _, whiskerwidth
screenendpoints = plot_to_screen(plot, endpoints)
screenendpoints_shifted_pairs = map(screenendpoints) do sep
(sep .+ f_if(is_in_y_direction[], reverse, Point(0, -whiskerwidth/2)),
sep .+ f_if(is_in_y_direction[], reverse, Point(0, whiskerwidth/2)))
end
return [p for pair in screenendpoints_shifted_pairs for p in pair]
end
whiskercolors = Observable{RGBColors}()
map!(plot, whiskercolors, color) do color
# we have twice as many linesegments for whiskers as we have errorbars, so we
# need to duplicate colors if a vector of colors is given
if color isa AbstractVector
return repeat(to_color(color), inner = 2)::RGBColors
else
return to_color(color)::RGBAf
end
end
whiskerlinewidths = Observable{Union{Float32, Vector{Float32}}}()
map!(plot, whiskerlinewidths, linewidth) do linewidth
# same for linewidth
if linewidth isa AbstractVector
return repeat(convert(Vector{Float32}, linewidth), inner = 2)::Vector{Float32}
else
return convert(Float32, linewidth)
end
end
linesegments!(
plot, linesegpairs, color = color, linewidth = linewidth, visible = visible,
colormap = colormap, colorscale = colorscale, colorrange = colorrange, inspectable = inspectable,
transparency = transparency
)
linesegments!(
plot, whiskers, color = whiskercolors, linewidth = whiskerlinewidths,
visible = visible, colormap = colormap, colorscale = colorscale, colorrange = colorrange,
inspectable = inspectable, transparency = transparency, space = :pixel,
model = Mat4f(I) # overwrite scale!() / translate!() / rotate!()
)
plot
end
function plot_to_screen(plot, points::AbstractVector)
cam = parent_scene(plot).camera
space = to_value(get(plot, :space, :data))
spvm = clip_to_space(cam, :pixel) * space_to_clip(cam, space) * transformationmatrix(plot)[]
return map(points) do p
transformed = apply_transform(transform_func(plot), p, space)
p4d = spvm * to_ndim(Point4f, to_ndim(Point3f, transformed, 0), 1)
return Point2f(p4d) / p4d[4]
end
end
function plot_to_screen(plot, p::VecTypes)
cam = parent_scene(plot).camera
space = to_value(get(plot, :space, :data))
spvm = clip_to_space(cam, :pixel) * space_to_clip(cam, space) * transformationmatrix(plot)[]
transformed = apply_transform(transform_func(plot), p, space)
p4d = spvm * to_ndim(Point4f, to_ndim(Point3f, transformed, 0), 1)
return Point2f(p4d) / p4d[4]
end
function screen_to_plot(plot, points::AbstractVector)
cam = parent_scene(plot).camera
space = to_value(get(plot, :space, :data))
mvps = inv(transformationmatrix(plot)[]) * clip_to_space(cam, space) * space_to_clip(cam, :pixel)
itf = inverse_transform(transform_func(plot))
return map(points) do p
pre_transform = mvps * to_ndim(Vec4f, to_ndim(Vec3f, p, 0.0), 1.0)
p3 = Point3f(pre_transform) / pre_transform[4]
return apply_transform(itf, p3, space)
end
end
function screen_to_plot(plot, p::VecTypes)
cam = parent_scene(plot).camera
space = to_value(get(plot, :space, :data))
mvps = inv(transformationmatrix(plot)[]) * clip_to_space(cam, space) * space_to_clip(cam, :pixel)
pre_transform = mvps * to_ndim(Vec4f, to_ndim(Vec3f, p, 0.0), 1.0)
p3 = Point3f(pre_transform) / pre_transform[4]
return apply_transform(itf, p3, space)
end
# ignore whiskers when determining data limits
function point_iterator(bars::Union{Errorbars, Rangebars})
point_iterator(bars.plots[1])
end