Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary coloring functions for streamplot #2002

Merged
merged 12 commits into from
Jul 22, 2023
18 changes: 12 additions & 6 deletions src/basic_recipes/streamplot.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

"""
streamplot(f::function, xinterval, yinterval; kwargs...)
streamplot(f::Function, xinterval, yinterval; color = norm, kwargs...)

f must either accept `f(::Point)` or `f(x::Number, y::Number)`.
f must return a Point2.
Expand All @@ -10,6 +10,11 @@ Example:
v(x::Point2{T}) where T = Point2f(x[2], 4*x[1])
streamplot(v, -2..2, -2..2)
```

One can choose the color of the lines by passing a function `color_func(dx::Point)` to the `color` attribute.
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
By default this is set to `norm`, but can be set to any function or composition of functions.
The `dx` which is passed to `color_func` is the output of `f` at the point being colored.

## Attributes
$(ATTRIBUTES)

Expand All @@ -22,6 +27,7 @@ See the function `Makie.streamplot_impl` for implementation details.
stepsize = 0.01,
gridsize = (32, 32, 32),
maxsteps = 500,
color = norm,
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
colormap = theme(scene, :colormap),
colorrange = Makie.automatic,
arrow_size = 15,
Expand Down Expand Up @@ -73,7 +79,7 @@ Links:

[Quasirandom sequences](http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
"""
function streamplot_impl(CallType, f, limits::Rect{N, T}, resolutionND, stepsize, maxsteps=500, dens=1.0) where {N, T}
function streamplot_impl(CallType, f, limits::Rect{N, T}, resolutionND, stepsize, maxsteps=500, dens=1.0, color = norm) where {N, T}
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
resolution = to_ndim(Vec{N, Int}, resolutionND, last(resolutionND))
mask = trues(resolution...) # unvisited squares
arrow_pos = Point{N, Float32}[]
Expand Down Expand Up @@ -114,7 +120,7 @@ function streamplot_impl(CallType, f, limits::Rect{N, T}, resolutionND, stepsize
pnorm = norm(point)
push!(arrow_pos, x0)
push!(arrow_dir, point ./ pnorm)
push!(colors, pnorm)
push!(colors, color(point))
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
mask[c] = false
n_points += 1
for d in (-1, 1)
Expand Down Expand Up @@ -142,7 +148,7 @@ function streamplot_impl(CallType, f, limits::Rect{N, T}, resolutionND, stepsize
ccur = idx
end
push!(line_points, x)
push!(line_colors, pnorm)
push!(line_colors, color(point))
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
n_linepoints += 1
end
end
Expand All @@ -159,13 +165,13 @@ function streamplot_impl(CallType, f, limits::Rect{N, T}, resolutionND, stepsize
end

function plot!(p::StreamPlot)
data = lift(p.f, p.limits, p.gridsize, p.stepsize, p.maxsteps, p.density) do f, limits, resolution, stepsize, maxsteps, density
data = lift(p.f, p.limits, p.gridsize, p.stepsize, p.maxsteps, p.density, p.color) do f, limits, resolution, stepsize, maxsteps, density, color
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
P = if applicable(f, Point2f(0)) || applicable(f, Point3f(0))
Point
else
Number
end
streamplot_impl(P, f, limits, resolution, stepsize, maxsteps, density)
streamplot_impl(P, f, limits, resolution, stepsize, maxsteps, density, color)
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
end
lines!(
p,
Expand Down