Skip to content

Commit

Permalink
Binder (#68)
Browse files Browse the repository at this point in the history
* add vspan to plotif

* better plotif
  • Loading branch information
jverzani authored May 21, 2024
1 parent 1924cb9 commit 4549acc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 67 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.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

2 comments on commit 4549acc

@jverzani
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/107298

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.5 -m "<description of version>" 4549acc51a9e51ae4a6dc6ab43297acadaf6922f
git push origin v0.2.5

Please sign in to comment.