Skip to content

Commit

Permalink
add vspan to plotif
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed May 19, 2024
1 parent 7b3d431 commit eb9832b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "CalculusWithJulia"
uuid = "a2e0e22d-7d4c-5312-9169-8b992201a882"
version = "0.2.3"
version = "0.2.4"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
44 changes: 40 additions & 4 deletions ext/CalculusWithJuliaPlotsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,48 @@ function trimplot(f, a, b, c=20; color=:black, legend=false, kwargs...)
plot(xs, f.(xs), colors=cols, legend=legend, kwargs...)
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)
function plotif(f, g, a::Real, b::Real;
colors=(:orange, :black, "#d35100"),
title="Plot of f colored when g ≥ 0",
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)
end
end

Plots.vspan!(collect(Base.Iterators.flatten(zip(x,X))),
fill=(colors[3], 0.1, Plots.stroke(0)))
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)
plot!(p, zero)
Expand Down

0 comments on commit eb9832b

Please sign in to comment.