added pkg to plot

This commit is contained in:
Thomas Breloff 2015-09-03 22:40:58 -04:00
parent f80d6bdfc6
commit 9bc4286dcb
4 changed files with 39 additions and 34 deletions

View File

@ -3,8 +3,8 @@
immutable GadflyPackage <: PlottingPackage end immutable GadflyPackage <: PlottingPackage end
# plot(pkg::QwtPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[]) plot(pkg::GadflyPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[])
# plot!(::GadflyPackage, plt::Plot; kw...) = Qwt.oplot(plt.o; kw...) plot!(::GadflyPackage, plt::Plot; kw...) = Qwt.oplot(plt.o; kw...)
# plot(::GadflyPackage, y; kw...) = Gadfly.plot(; x = 1:length(y), y = y, kw...) # plot(::GadflyPackage, y; kw...) = Gadfly.plot(; x = 1:length(y), y = y, kw...)
# plot(::GadflyPackage, x, y; kw...) = Gadfly.plot(; x = x, y = y, kw...) # plot(::GadflyPackage, x, y; kw...) = Gadfly.plot(; x = x, y = y, kw...)

View File

@ -116,7 +116,7 @@ When plotting multiple lines, you can give every line the same trait by using th
# this creates a new plot with args/kw and sets it to be the current plot # this creates a new plot with args/kw and sets it to be the current plot
function plot(args...; kw...) function plot(args...; kw...)
plt = plot(plotter(); getPlotKeywordArgs(kw, 1)...) # create a new, blank plot plt = plot(plotter(); getPlotKeywordArgs(kw, 1)...) # create a new, blank plot
plot!(plt, args...; kw...) # add the series to that plot plot!(plotter(), plt, args...; kw...) # add the series to that plot
currentPlot!(plt) # set this as the current plot currentPlot!(plt) # set this as the current plot
plt plt
end end
@ -124,7 +124,7 @@ end
# this adds to the current plot # this adds to the current plot
function plot!(args...; kw...) function plot!(args...; kw...)
plt = currentPlot() plt = currentPlot()
plot!(plt, args...; kw...) plot!(plotter(), plt, args...; kw...)
plt plt
end end
@ -132,104 +132,104 @@ end
# These methods are various ways to add to an existing plot # These methods are various ways to add to an existing plot
function plot!(plt::Plot, y::AVec; kw...) function plot!(pkg::PlottingPackage, plt::Plot, y::AVec; kw...)
kw = getPlotKeywordArgs(kw, 1) kw = getPlotKeywordArgs(kw, 1)
plot!(plt; x = 1:length(y), y = y, kw...) plot!(pkg, plt; x = 1:length(y), y = y, kw...)
end end
function plot!(plt::Plot, x::AVec, y::AVec; kw...) # one line (will assert length(x) == length(y)) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AVec; kw...) # one line (will assert length(x) == length(y))
@assert length(x) == length(y) @assert length(x) == length(y)
kw = getPlotKeywordArgs(kw, 1) kw = getPlotKeywordArgs(kw, 1)
plot!(plt; x=x, y=y, kw...) plot!(pkg, plt; x=x, y=y, kw...)
end end
function plot!(plt::Plot, y::AMat; kw...) # multiple lines (one per column of x), all sharing x = 1:size(y,1) function plot!(pkg::PlottingPackage, plt::Plot, y::AMat; kw...) # multiple lines (one per column of x), all sharing x = 1:size(y,1)
n,m = size(y) n,m = size(y)
for i in 1:m for i in 1:m
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = 1:n, y = y[:,i], kw...) plot!(pkg, plt; x = 1:n, y = y[:,i], kw...)
end end
plt plt
end end
function plot!(plt::Plot, x::AVec, y::AMat; kw...) # multiple lines (one per column of x), all sharing x (will assert length(x) == size(y,1)) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AMat; kw...) # multiple lines (one per column of x), all sharing x (will assert length(x) == size(y,1))
n,m = size(y) n,m = size(y)
for i in 1:m for i in 1:m
@assert length(x) == n @assert length(x) == n
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = x, y = y[:,i], kw...) plot!(pkg, plt; x = x, y = y[:,i], kw...)
end end
plt plt
end end
function plot!(plt::Plot, x::AMat, y::AMat; kw...) # multiple lines (one per column of x/y... will assert size(x) == size(y)) function plot!(pkg::PlottingPackage, plt::Plot, x::AMat, y::AMat; kw...) # multiple lines (one per column of x/y... will assert size(x) == size(y))
@assert size(x) == size(y) @assert size(x) == size(y)
for i in 1:size(x,2) for i in 1:size(x,2)
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = x[:,i], y = y[:,i], kw...) plot!(pkg, plt; x = x[:,i], y = y[:,i], kw...)
end end
plt plt
end end
function plot!(plt::Plot, x::AVec, f::Function; kw...) # one line, y = f(x) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, f::Function; kw...) # one line, y = f(x)
kw = getPlotKeywordArgs(kw, 1) kw = getPlotKeywordArgs(kw, 1)
plot!(plt; x = x, y = map(f,x), kw...) plot!(pkg, plt; x = x, y = map(f,x), kw...)
end end
function plot!(plt::Plot, x::AMat, f::Function; kw...) # multiple lines, yᵢⱼ = f(xᵢⱼ) function plot!(pkg::PlottingPackage, plt::Plot, x::AMat, f::Function; kw...) # multiple lines, yᵢⱼ = f(xᵢⱼ)
for i in 1:size(x,2) for i in 1:size(x,2)
xi = x[:,i] xi = x[:,i]
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = xi, y = map(f, xi), kw...) plot!(pkg, plt; x = xi, y = map(f, xi), kw...)
end end
plt plt
end end
function plot!(plt::Plot, x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ)
for i in 1:length(fs) for i in 1:length(fs)
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = x, y = map(fs[i], x), kw...) plot!(pkg, plt; x = x, y = map(fs[i], x), kw...)
end end
plt plt
end end
function plot!(plt::Plot, y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i]) function plot!(pkg::PlottingPackage, plt::Plot, y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i])
for i in 1:length(y) for i in 1:length(y)
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = 1:length(y[i]), y = y[i], kw...) plot!(pkg, plt; x = 1:length(y[i]), y = y[i], kw...)
end end
plt plt
end end
function plot!(plt::Plot, x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i]) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i])
for i in 1:length(y) for i in 1:length(y)
@assert length(x) == length(y[i]) @assert length(x) == length(y[i])
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = x, y = y[i], kw...) plot!(pkg, plt; x = x, y = y[i], kw...)
end end
plt plt
end end
function plot!(plt::Plot, x::AVec{AVec}, y::AVec{AVec}; kw...) # multiple lines, will assert length(x[i]) == length(y[i]) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec{AVec}, y::AVec{AVec}; kw...) # multiple lines, will assert length(x[i]) == length(y[i])
@assert length(x) == length(y) @assert length(x) == length(y)
for i in 1:length(x) for i in 1:length(x)
@assert length(x[i]) == length(y[i]) @assert length(x[i]) == length(y[i])
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot!(plt; x = x[i], y = y[i], kw...) plot!(pkg, plt; x = x[i], y = y[i], kw...)
end end
plt plt
end end
function plot!(plt::Plot, n::Integer; kw...) # n lines, all empty (for updating plots) function plot!(pkg::PlottingPackage, plt::Plot, n::Integer; kw...) # n lines, all empty (for updating plots)
for i in 1:n for i in 1:n
kw = getPlotKeywordArgs(kw, i) kw = getPlotKeywordArgs(kw, i)
plot(plt, x = zeros(0), y = zeros(0), kw...) plot(pkg, plt, x = zeros(0), y = zeros(0), kw...)
end end
end end
# ------------------------- # -------------------------
# this is the core method... add to a plot object using kwargs, with args already converted into kwargs # # this is the core method... add to a plot object using kwargs, with args already converted into kwargs
function plot!(plt::Plot; kw...) # function plot!(pkg::PlottingPackage, plt::Plot; kw...)
plot!(plotter(), plt; kw...) # plot!(pl, plt; kw...)
end # end

View File

@ -6,6 +6,9 @@ using Requires
@lazymod Qwt @lazymod Qwt
@lazymod Gadfly @lazymod Gadfly
plot(pkg::PlottingPackage; kw...) = error("plot($pkg; kw...) is not implemented")
plot!(pkg::PlottingPackage, plt::Plot; kw...) = error("plot!($pkg, plt; kw...) is not implemented")
# --------------------------------------------------------- # ---------------------------------------------------------

View File

@ -42,14 +42,16 @@ facts("Qwt") do
# plot(x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ) # plot(x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ)
@fact plot(1:10, Function[sin,cos]) --> not(nothing) @fact plot(1:10, Function[sin,cos]) --> not(nothing)
@fact currentPlot().o.lines[1].y --> sin(collect(1:10)) @fact currentPlot().o.lines[1].y --> sin(collect(1:10))
@fact currentPlot().o.lines[1].y --> cos(collect(1:10)) @fact currentPlot().o.lines[2].y --> cos(collect(1:10))
# plot(y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i]) # plot(y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i])
@fact plot((11:20, rand(10))) --> not(nothing) @fact plot([11:20 ; rand(10)]) --> not(nothing)
@fact currentPlot().o.lines[1].x[4] --> 4 @fact currentPlot().o.lines[1].x[4] --> 4
@fact currentPlot().o.lines[1].y[4] --> 14 @fact currentPlot().o.lines[1].y[4] --> 14
# plot(x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i]) # plot(x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i])
# plot(x::AVec{AVec}, y::AVec{AVec}; kw...) # multiple lines, will assert length(x[i]) == length(y[i]) # plot(x::AVec{AVec}, y::AVec{AVec}; kw...) # multiple lines, will assert length(x[i]) == length(y[i])
# plot(n::Integer; kw...) # n lines, all empty (for updating plots) # plot(n::Integer; kw...) # n lines, all empty (for updating plots)
end end