Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
add stem (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel authored Feb 23, 2021
1 parent ea40065 commit 4bc3bf2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/src/plotting_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,38 @@ current_figure()
```


## `stem`

```@docs
stem
```

### Examples

```@example
using GLMakie
AbstractPlotting.inline!(true) # hide
xs = LinRange(0, 4pi, 30)
f = Figure()
stem(f[1, 1], xs, sin.(xs))
stem(f[1, 2], xs, sin,
offset = 0.5, trunkcolor = :blue, marker = :rect,
stemcolor = :red, color = :orange,
markersize = 15, strokecolor = :red, strokewidth = 3,
trunklinestyle = :dash, stemlinestyle = :dashdot)
stem(f[2, 1], xs, sin.(xs),
offset = LinRange(-0.5, 0.5, 30),
color = LinRange(0, 1, 30), colorrange = (0, 0.5),
trunkcolor = LinRange(0, 1, 30), trunkwidth = 5)
stem(f[2, 2], 0.5xs, 2 .* sin.(xs), 2 .* cos.(xs),
offset = Point3f0.(0.5xs, sin.(xs), cos.(xs)),
stemcolor = LinRange(0, 1, 30), stemcolormap = :Spectral, stemcolorrange = (0, 0.5))
f
```


## `surface`

```@docs
Expand Down
1 change: 1 addition & 0 deletions src/AbstractPlotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ include("basic_recipes/poly.jl")
include("basic_recipes/scatterlines.jl")
include("basic_recipes/series.jl")
include("basic_recipes/spy.jl")
include("basic_recipes/stem.jl")
include("basic_recipes/streamplot.jl")
include("basic_recipes/timeseries.jl")
include("basic_recipes/title.jl")
Expand Down
83 changes: 83 additions & 0 deletions src/basic_recipes/stem.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
stem(xs, ys, [zs]; kwargs...)
Plots markers at the given positions extending from `offset` along stem lines.
`offset` can be a number, in which case it sets y for 2D and also z for 3D stems.
It can be a Point2 for 2D plots and 3D plots, as well as a Point3 for 3D plots.
It can also be an iterable of any of these at the same length as xs, ys, zs.
The conversion trait of stem is `PointBased`.
## Attributes
$(ATTRIBUTES)
"""
@recipe(Stem) do scene
Attributes(
stemcolor = :black,
stemcolormap = :viridis,
stemcolorrange = automatic,
stemwidth = 1,
stemlinestyle = nothing,
trunkwidth = 1,
trunklinestyle = nothing,
trunkcolor = :black,
trunkcolormap = :viridis,
trunkcolorrange = automatic,
offset = 0,
marker = :circle,
markersize = 10,
color = :gray65,
colormap = :viridis,
colorrange = automatic,
strokecolor = :black,
strokewidth = 1,
visible = true,
)
end


conversion_trait(::Type{<:Stem}) = PointBased()


trunkpoint(stempoint::P, offset::Number) where P <: Point2 = P(stempoint[1], offset)
trunkpoint(stempoint::P, offset::Point2) where P <: Point2 = P(offset...)
trunkpoint(stempoint::P, offset::Number) where P <: Point3 = P(stempoint[1], offset, offset)
trunkpoint(stempoint::P, offset::Point2) where P <: Point3 = P(stempoint[1], offset[1], offset[2])
trunkpoint(stempoint::P, offset::Point3) where P <: Point3 = P(offset...)


function plot!(s::Stem{<:Tuple{<:AbstractVector{<:Point}}})
points = s[1]

stemtuples = lift(points, s.offset) do ps, to
tuple.(ps, trunkpoint.(ps, to))
end

trunkpoints = @lift(last.($stemtuples))

lines!(s, trunkpoints,
linewidth = s.trunkwidth,
color = s.trunkcolor,
colormap = s.trunkcolormap,
colorrange = s.trunkcolorrange,
visible = s.visible,
linestyle = s.trunklinestyle)
linesegments!(s, stemtuples,
linewidth = s.stemwidth,
color = s.stemcolor,
colormap = s.stemcolormap,
colorrange = s.stemcolorrange,
visible = s.visible,
linestyle = s.stemlinestyle)
scatter!(s, s[1],
color = s.color,
colormap = s.colormap,
colorrange = s.colorrange,
markersize = s.markersize,
marker = s.marker,
strokecolor = s.strokecolor,
strokewidth = s.strokewidth,
visible = s.visible)
s
end

0 comments on commit 4bc3bf2

Please sign in to comment.