From 4bc3bf212a00c14ea30f3de10946c0d726cad0c0 Mon Sep 17 00:00:00 2001 From: jkrumbiegel <22495855+jkrumbiegel@users.noreply.github.com> Date: Tue, 23 Feb 2021 12:06:59 +0100 Subject: [PATCH] add stem (#643) --- docs/src/plotting_functions.md | 32 +++++++++++++ src/AbstractPlotting.jl | 1 + src/basic_recipes/stem.jl | 83 ++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/basic_recipes/stem.jl diff --git a/docs/src/plotting_functions.md b/docs/src/plotting_functions.md index 9634ea90c..c18959038 100644 --- a/docs/src/plotting_functions.md +++ b/docs/src/plotting_functions.md @@ -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 diff --git a/src/AbstractPlotting.jl b/src/AbstractPlotting.jl index fe9324f07..99bd819f7 100644 --- a/src/AbstractPlotting.jl +++ b/src/AbstractPlotting.jl @@ -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") diff --git a/src/basic_recipes/stem.jl b/src/basic_recipes/stem.jl new file mode 100644 index 000000000..00916fce0 --- /dev/null +++ b/src/basic_recipes/stem.jl @@ -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