Skip to content

Commit 3f4dbfd

Browse files
yhaBeastyBlacksmith
andcommitted
Fix bar plot vector attribute with missing values (#4736)
* Fix bar plot vector attribute with missing values * format --------- Co-authored-by: Simon Christ <[email protected]>
1 parent cd152a1 commit 3f4dbfd

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/examples.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ const _examples = PlotExample[
198198
),
199199
PlotExample( # 14
200200
"Bar",
201-
"`x` is the midpoint of the bar. (todo: allow passing of edges instead of midpoints)",
202-
:(bar(randn(99))),
201+
"`bar(x, y)` plots bars with heights `y` and centers at `x`.
202+
`x` defaults to `eachindex(y)`.",
203+
:(plot(bar(randn(10)), bar([0, 3, 5], [1, 2, 6]), legend = false)),
203204
),
204205
PlotExample( # 15
205206
"Histogram",
@@ -1235,6 +1236,16 @@ const _examples = PlotExample[
12351236
end
12361237
end,
12371238
),
1239+
PlotExample( # 66
1240+
"Bars: specifying edges and missing values",
1241+
"In `bar(x, y)`, `x` may be the same length as `y` to specify bar centers,
1242+
or one longer to specify bar edges.",
1243+
:(plot(
1244+
bar(-5:5, randn(10)), # bar edges at -5:5
1245+
bar(-2:2, [2, -2, NaN, -1, 1], color = 1:5), # bar centers at -2:2, one missing value
1246+
legend = false,
1247+
)),
1248+
),
12381249
]
12391250

12401251
# Some constants for PlotDocs and PlotReferenceImages
@@ -1256,6 +1267,7 @@ _backend_skips = Dict(
12561267
63, # twin axes unsupported
12571268
64, # legend pos unsupported
12581269
65, # legend pos unsupported
1270+
66, # bar: vector-valued `color` unsupported
12591271
],
12601272
:pgfplotsx => [
12611273
6, # images

src/recipes.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,10 @@ end
442442
end
443443

444444
xseg, yseg = map(_ -> Segments(), 1:2)
445+
valid_i = isfinite.(procx) .& isfinite.(procy)
445446
for i in 1:ny
446-
(yi = procy[i]) |> isnan && continue
447+
valid_i[i] || continue
448+
yi = procy[i]
447449
center = procx[i]
448450
hwi = _cycle(hw, i)
449451
fi = _cycle(fillto, i)
@@ -477,8 +479,9 @@ end
477479
@warn "Indices $(eachindex(v)) of attribute `$k` do not match data indices $(eachindex(y))."
478480
end
479481
# Each segment is 6 elements long, including the NaN separator.
482+
# One segment is created for each non-NaN element of `procy`.
480483
# There is no trailing NaN, so the last repetition is dropped.
481-
plotattributes[k] = @view repeat(v; inner = 6)[1:(end - 1)]
484+
plotattributes[k] = @views repeat(v[valid_i]; inner = 6)[1:(end - 1)]
482485
end
483486
end
484487
()
@@ -490,8 +493,8 @@ end
490493
markersize := 0
491494
markeralpha := 0
492495
fillrange := nothing
493-
x := x
494-
y := y
496+
x := procx
497+
y := procy
495498
()
496499
end
497500
@deps bar shape

0 commit comments

Comments
 (0)