diff --git a/Project.toml b/Project.toml index ff12180..f1a1045 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "CalculusWithJulia" uuid = "a2e0e22d-7d4c-5312-9169-8b992201a882" -version = "0.2.4" +version = "0.2.5" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/ext/CalculusWithJuliaPlotsExt.jl b/ext/CalculusWithJuliaPlotsExt.jl index f1ebeef..51ebe82 100644 --- a/ext/CalculusWithJuliaPlotsExt.jl +++ b/ext/CalculusWithJuliaPlotsExt.jl @@ -21,7 +21,8 @@ import Plots import Plots: plot, plot!, scatter, scatter!, Shape, current, text, annotate!, surface, surface!, - quiver, quiver! + quiver, quiver!, + stroke, vspan! using Plots.RecipesBase import Contour @@ -49,46 +50,48 @@ function trimplot(f, a, b, c=20; color=:black, legend=false, kwargs...) end function plotif(f, g, a::Real, b::Real; - colors=(:orange, :black, "#d35100"), - title="Plot of f colored when g ≥ 0", + linecolor=(:orange, :black), + linewidth=(3,5), + linestyle=(:solid, :dot), + fill=(:red, 0.10, stroke(0)), + title = "Plot of f highlighting when g ≥ 0", + legend=false, kwargs...) - xs = range(a, b, 251) - h = x -> g(x) ≥ 0 ? f(x) : NaN - plot(f, a, b; title, legend=false, - linecolor=colors[1], linestyle=:solid, linewidth=3) - plot!(h; - linecolor=colors[2], - linestyle=:dot, - linewidth=5, - kwargs...) - plot!(zero) - xs = find_zeros(g, a, b) - if !isapprox(a, first(xs), atol=1e-6, rtol=1e-8) - pushfirst!(xs, a) - end - if !isapprox(b, last(xs), atol=1e-6, rtol=1e-8) - push!(xs, b) - end - n = length(xs) - inds = Int[] - x,X = eltype(xs)[], eltype(xs)[] - for i in 2:n - u,v = xs[i-1],xs[i] - w = (u+v)/2 - if g(w) ≥ 0 - push!(x,u); push!(X,v) + + # get shading + xs, ys = unzip(x -> g(x) ≥ 0 ? f(x) : NaN, a, b) + ls,rs = eltype(xs)[], eltype(xs)[] + + left = true + for (x,y) ∈ zip(xs, ys) + if left + if !isnan(y) + push!(ls, x); push!(rs, x) + left = false + end + else + if isnan(y) + left = true + else + rs[end] = x + end end end - Plots.vspan!(collect(Base.Iterators.flatten(zip(x,X))), - fill=(colors[3], 0.1, Plots.stroke(0))) + # make plot + plot(f, a, b; linecolor=linecolor[1], + linewidth=linewidth[1], + linestyle=linestyle[1], + title=title, + legend=legend, + kwargs...) + plot!(xs, ys; linecolor=linecolor[2], + linewidth=linewidth[2], + linestyle=linestyle[2]) + vspan!(collect(Base.Iterators.flatten(zip(ls,rs))), + fill=fill) end -# function plotif(f, g, a, b) -# xs = range(a, b, length=251) -# cols = identify_colors(g, xs) -# plot(xs, f.(xs), color=cols, legend=false) -# end function signchart(f, a, b) p = plotif(f, f, a, b) diff --git a/src/plot-recipes.jl b/src/plot-recipes.jl index 53017b0..8f35d60 100644 --- a/src/plot-recipes.jl +++ b/src/plot-recipes.jl @@ -50,7 +50,7 @@ end """ plotif(f, g, a, b) -Plot f colored depending on g < 0 or not. +Plot of `f` over `[a,b]` with the intervals where `g ≥ 0` highlighted. """ @userplot PlotIf @recipe function __(a::PlotIf) diff --git a/src/plot-utils.jl b/src/plot-utils.jl index 3a6553f..68690cb 100644 --- a/src/plot-utils.jl +++ b/src/plot-utils.jl @@ -14,35 +14,6 @@ By Gunter Fuchs. fisheye(f) = atan ∘ f ∘ tan ## --- -# for plotif. This identifies a vector of colors -function identify_colors(g, xs, colors=(:red, :blue, :black)) - F = (a,b) -> begin - ga,gb=g(a),g(b) - ga * gb < 0 && return nothing - ga >= 0 && return true - return false - end - find_colors(F, xs, colors) -end - -# F(a,b) returns true, false, or nothing -function find_colors(F, xs, colors=(:red, :blue, :black)) - n = length(xs) - cols = repeat([colors[1]], n) - for i in 1:n-1 - a,b = xs[i], xs[i+1] - val = F(a,b) - if val == nothing - cols[i] = colors[3] - elseif val - cols[i] = colors[1] - else - cols[i] = colors[2] - end - end - cols[end] = cols[end-1] - cols -end # some plotting utilities """ rangeclamp(f, hi=20, lo=-hi; replacement=NaN) diff --git a/src/plots.jl b/src/plots.jl index 2dfa288..f55063f 100644 --- a/src/plots.jl +++ b/src/plots.jl @@ -26,7 +26,7 @@ function trimplot end """ plotif(f, g, a, b) -Plot f colored depending on g ≥ 0 or not. +Plot of `f` over `[a,b]` with the intervals where `g ≥ 0` highlighted in many ways. """ function plotif end