Skip to content

Commit

Permalink
add vspan to plotif (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani authored May 19, 2024
1 parent 7b3d431 commit 1924cb9
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

2 comments on commit 1924cb9

@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/107167

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.4 -m "<description of version>" 1924cb9a3d7139bfe4cffa8233ca2b8e1cf0b0cd
git push origin v0.2.4

Please sign in to comment.