Skip to content

Commit e39db0a

Browse files
committed
Add stem plots for sampled data systems
1 parent 6b9da6a commit e39db0a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

PlotsBase/src/recipes.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,4 +1620,30 @@ end
16201620
end
16211621
end
16221622

1623+
# -------------------------------------------------
1624+
# Stem plots are useful for visualizing discretely sampled time series data
1625+
@userplot Stem
1626+
1627+
# If the second arg is a function, eagerly apply it so we can create the stem lines
1628+
process_stem_args(x, f::Function) = (x, f.(x))
1629+
process_stem_args(x, y) = (x, y)
1630+
1631+
@recipe function f(s::Stem)
1632+
if length(s.args) != 2
1633+
throw(MethodError(stem, s.args))
1634+
end
1635+
x, y = process_stem_args(s.args...)
1636+
@series begin
1637+
primary := true
1638+
seriestype := :scatter
1639+
markerstrokewidth --> 0
1640+
x, y
1641+
end
1642+
@series begin
1643+
primary := false
1644+
seriestype := :line
1645+
[x x]', [zero(y) y]'
1646+
end
1647+
end
1648+
16231649
@specialize

PlotsBase/test/test_recipes.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ with(:gr) do
143143
@test_nowarn show(devnull, scatter3d(1:2, 1:2, 1:2))
144144
end
145145

146+
@testset "stem" begin
147+
n = 0:0.5:10
148+
149+
# Make sure array and function arguments work
150+
@test stem(n, sin.(n)) isa PlotsBase.Plot
151+
@test stem(n, sin) isa PlotsBase.Plot
152+
153+
# Should only accept two positional arguments, otherwise throw an error
154+
@test_throws MethodError stem()
155+
@test_throws MethodError stem(n)
156+
@test_throws MethodError stem(n, n, n)
157+
@test_throws MethodError stem(n, n, n, n)
158+
end
159+
146160
@testset "sticks" begin
147161
@test_nowarn show(devnull, sticks(1:2, marker = :circle))
148162
end

0 commit comments

Comments
 (0)