Skip to content

Commit 69f5087

Browse files
committed
Merge pull request #1 from snotskie/master
Added direct plotting for Polynomial.Poly objects
2 parents 32177c0 + dcd6e2c commit 69f5087

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ That last line is what the REPL prints out,
3535
as a Firefox tab opens with the plot.
3636
You can also just call `plot` by itself, and you'll get a String that's the url of your chart.
3737

38+
## Plot Functions and Polynomials
39+
julia> Plotly.plot(abs)
40+
julia> Plotly.plot([sqrt, log], ["left"=>10, "right"=>20, "step"=>0.1])
41+
42+
You can now plot functions directly.
43+
The first line shows how to plot the absolute value function, and the second line plots
44+
the square root and logarithm functions, both from 10 to 20 at increments of 0.1.
45+
46+
julia> using Polynomial
47+
julia> x = Poly([1,0])
48+
julia> Plotly.plot(3x^3 + 2x^2 - x + 1)
49+
50+
If you have the Polynomial package installed, you can plot them directly the same way as math functions.
51+
3852
## Style and Layout
3953
julia> Plotly.style(["line"=>["color"=>"rgb(255,0,0)","width"=>10]])
4054

src/Plotly.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ function plot(data::Array,options=Dict())
7979
end
8080
end
8181

82+
include("plot.jl")
83+
8284
function layout(layout_opts::Dict,meta_opts=Dict())
8385
global plotlyaccount
8486
if !isdefined(Plotly,:plotlyaccount)

src/plot.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function get_points(f::Function, options=Dict())
2+
opt = merge(["left"=>-10, "right"=>10, "step"=>0.5, "name"=>"$f"], options)
3+
n::Int = (opt["right"] - opt["left"]) / opt["step"] + 1
4+
X = Float64[0 for i in 1:n]
5+
Y = Float64[0 for i in 1:n]
6+
for i in 1:n
7+
x = opt["step"]*(i-1) + opt["left"]
8+
y = f(x)
9+
X[i] = round(x, 8)
10+
Y[i] = round(y, 8)
11+
end
12+
13+
return ["x"=>X, "y"=>Y, "type"=>"scatter", "mode"=>"lines", "name"=>opt["name"]]
14+
end
15+
16+
function plot(fs::Array{Function,1}, options=Dict())
17+
data = [get_points(f, options) for f in fs]
18+
return plot([data], options)
19+
end
20+
21+
function plot(f::Function, options=Dict())
22+
return plot([f], options)
23+
end
24+
25+
if Pkg.installed("Polynomial") !== nothing
26+
using Polynomial
27+
28+
function plot{T<:Number}(ps::Array{Poly{T},1}, options=Dict())
29+
data = [get_points(x->polyval(p,x), merge(["name"=>"$p"], options)) for p in ps]
30+
return plot([data], options)
31+
end
32+
33+
function plot(p::Poly, options=Dict())
34+
return plot([p], options)
35+
end
36+
end

0 commit comments

Comments
 (0)