Skip to content

Commit 4549acc

Browse files
authored
Binder (#68)
* add vspan to plotif * better plotif
1 parent 1924cb9 commit 4549acc

File tree

5 files changed

+41
-67
lines changed

5 files changed

+41
-67
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "CalculusWithJulia"
22
uuid = "a2e0e22d-7d4c-5312-9169-8b992201a882"
3-
version = "0.2.4"
3+
version = "0.2.5"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

ext/CalculusWithJuliaPlotsExt.jl

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import Plots
2121
import Plots: plot, plot!, scatter, scatter!, Shape, current,
2222
text, annotate!,
2323
surface, surface!,
24-
quiver, quiver!
24+
quiver, quiver!,
25+
stroke, vspan!
2526

2627
using Plots.RecipesBase
2728
import Contour
@@ -49,46 +50,48 @@ function trimplot(f, a, b, c=20; color=:black, legend=false, kwargs...)
4950
end
5051

5152
function plotif(f, g, a::Real, b::Real;
52-
colors=(:orange, :black, "#d35100"),
53-
title="Plot of f colored when g ≥ 0",
53+
linecolor=(:orange, :black),
54+
linewidth=(3,5),
55+
linestyle=(:solid, :dot),
56+
fill=(:red, 0.10, stroke(0)),
57+
title = "Plot of f highlighting when g ≥ 0",
58+
legend=false,
5459
kwargs...)
55-
xs = range(a, b, 251)
56-
h = x -> g(x) 0 ? f(x) : NaN
57-
plot(f, a, b; title, legend=false,
58-
linecolor=colors[1], linestyle=:solid, linewidth=3)
59-
plot!(h;
60-
linecolor=colors[2],
61-
linestyle=:dot,
62-
linewidth=5,
63-
kwargs...)
64-
plot!(zero)
65-
xs = find_zeros(g, a, b)
66-
if !isapprox(a, first(xs), atol=1e-6, rtol=1e-8)
67-
pushfirst!(xs, a)
68-
end
69-
if !isapprox(b, last(xs), atol=1e-6, rtol=1e-8)
70-
push!(xs, b)
71-
end
72-
n = length(xs)
73-
inds = Int[]
74-
x,X = eltype(xs)[], eltype(xs)[]
75-
for i in 2:n
76-
u,v = xs[i-1],xs[i]
77-
w = (u+v)/2
78-
if g(w) 0
79-
push!(x,u); push!(X,v)
60+
61+
# get shading
62+
xs, ys = unzip(x -> g(x) 0 ? f(x) : NaN, a, b)
63+
ls,rs = eltype(xs)[], eltype(xs)[]
64+
65+
left = true
66+
for (x,y) zip(xs, ys)
67+
if left
68+
if !isnan(y)
69+
push!(ls, x); push!(rs, x)
70+
left = false
71+
end
72+
else
73+
if isnan(y)
74+
left = true
75+
else
76+
rs[end] = x
77+
end
8078
end
8179
end
8280

83-
Plots.vspan!(collect(Base.Iterators.flatten(zip(x,X))),
84-
fill=(colors[3], 0.1, Plots.stroke(0)))
81+
# make plot
82+
plot(f, a, b; linecolor=linecolor[1],
83+
linewidth=linewidth[1],
84+
linestyle=linestyle[1],
85+
title=title,
86+
legend=legend,
87+
kwargs...)
88+
plot!(xs, ys; linecolor=linecolor[2],
89+
linewidth=linewidth[2],
90+
linestyle=linestyle[2])
91+
vspan!(collect(Base.Iterators.flatten(zip(ls,rs))),
92+
fill=fill)
8593
end
8694

87-
# function plotif(f, g, a, b)
88-
# xs = range(a, b, length=251)
89-
# cols = identify_colors(g, xs)
90-
# plot(xs, f.(xs), color=cols, legend=false)
91-
# end
9295

9396
function signchart(f, a, b)
9497
p = plotif(f, f, a, b)

src/plot-recipes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
"""
5151
plotif(f, g, a, b)
5252
53-
Plot f colored depending on g < 0 or not.
53+
Plot of `f` over `[a,b]` with the intervals where `g ≥ 0` highlighted.
5454
"""
5555
@userplot PlotIf
5656
@recipe function __(a::PlotIf)

src/plot-utils.jl

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,6 @@ By Gunter Fuchs.
1414
fisheye(f) = atan f tan
1515

1616
## ---
17-
# for plotif. This identifies a vector of colors
18-
function identify_colors(g, xs, colors=(:red, :blue, :black))
19-
F = (a,b) -> begin
20-
ga,gb=g(a),g(b)
21-
ga * gb < 0 && return nothing
22-
ga >= 0 && return true
23-
return false
24-
end
25-
find_colors(F, xs, colors)
26-
end
27-
28-
# F(a,b) returns true, false, or nothing
29-
function find_colors(F, xs, colors=(:red, :blue, :black))
30-
n = length(xs)
31-
cols = repeat([colors[1]], n)
32-
for i in 1:n-1
33-
a,b = xs[i], xs[i+1]
34-
val = F(a,b)
35-
if val == nothing
36-
cols[i] = colors[3]
37-
elseif val
38-
cols[i] = colors[1]
39-
else
40-
cols[i] = colors[2]
41-
end
42-
end
43-
cols[end] = cols[end-1]
44-
cols
45-
end
4617
# some plotting utilities
4718
"""
4819
rangeclamp(f, hi=20, lo=-hi; replacement=NaN)

src/plots.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function trimplot end
2626
"""
2727
plotif(f, g, a, b)
2828
29-
Plot f colored depending on g ≥ 0 or not.
29+
Plot of `f` over `[a,b]` with the intervals where `g ≥ 0` highlighted in many ways.
3030
"""
3131
function plotif end
3232

0 commit comments

Comments
 (0)