Skip to content

Commit 96b9a21

Browse files
authored
Merge pull request #165 from hotplot/plotrecipes
Added plot recipe
2 parents f3af1de + c80cec0 commit 96b9a21

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.7
2+
RecipesBase

src/PlotRecipes.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using RecipesBase
2+
3+
4+
function poly_interval(p::Poly)
5+
# Find points of interest
6+
zero_pts = roots(p)
7+
crit_pts = roots(polyder(p, 1))
8+
infl_pts = roots(polyder(p, 2))
9+
pts = sort([ real(pt) for pt in [zero_pts; crit_pts; infl_pts] if isreal(pt) ])
10+
11+
# Choose a range that shows all interesting points with some margin
12+
min_x, max_x = length(pts) > 0 ? (pts[1], pts[end]) : (-1, 1)
13+
diff = max(max_x - min_x, 1)
14+
a = min_x - diff/2
15+
b = max_x + diff/2
16+
17+
return a : diff/50 : b
18+
end
19+
20+
21+
function poly_label(p::Poly)
22+
buff = IOBuffer()
23+
printpoly(buff, p)
24+
String(take!(buff))
25+
end
26+
27+
28+
@recipe function poly_recipe(p::Poly, range = poly_interval(p))
29+
label --> poly_label(p)
30+
range, map(x -> polyval(p, x), range)
31+
end
32+
33+
34+
@recipe function poly_recipe(p::Poly, a, b)
35+
label --> poly_label(p)
36+
step = (b-a)/100
37+
xs = a:step:b
38+
ys = map(x -> polyval(p, x), xs)
39+
xs, ys
40+
end

src/Polynomials.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,9 @@ function polyfit(x, y, n::Int=length(x)-1, sym::Symbol=:x)
716716
end
717717
polyfit(x,y,sym::Symbol) = polyfit(x,y,length(x)-1, sym)
718718

719+
719720
### Pull in others
720721
include("pade.jl")
722+
include("PlotRecipes.jl")
721723

722724
end # module Poly

test/REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
SpecialFunctions
1+
SpecialFunctions

0 commit comments

Comments
 (0)