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

Binder #68

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4"
version = "0.2.5"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
73 changes: 38 additions & 35 deletions ext/CalculusWithJuliaPlotsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/plot-recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 0 additions & 29 deletions src/plot-utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading