working on org/docs/test
This commit is contained in:
parent
9584d5b441
commit
ef4a47f50f
14
README.md
14
README.md
@ -23,7 +23,7 @@ using Plots
|
||||
Do a plot in Qwt, then save a png:
|
||||
|
||||
```
|
||||
plotter(:Qwt)
|
||||
plotter!(:qwt)
|
||||
plot(1:10)
|
||||
savepng(ans, Plots.IMG_DIR * "qwt1.png")
|
||||
```
|
||||
@ -36,7 +36,7 @@ which saves:
|
||||
Do a plot in Gadfly, then save a png:
|
||||
|
||||
```
|
||||
plotter(:Gadfly)
|
||||
plotter!(:gadfly)
|
||||
plot(1:10)
|
||||
savepng(ans, Plots.IMG_DIR * "gadfly1.png", 6Gadfly.inch, 4Gadfly.inch)
|
||||
```
|
||||
@ -47,7 +47,7 @@ which saves:
|
||||
|
||||
|
||||
Note that you do not need all underlying packages to use this. I use Requires.jl to
|
||||
perform lazy loading of the modules, so there's no initialization until you call `plotter()`.
|
||||
perform lazy loading of the modules, so there's no initialization until you call `plotter!()`.
|
||||
This has an added benefit that you can call `using Plots` and it should return quickly...
|
||||
no more waiting for a plotting package to load when you don't even use it. :)
|
||||
|
||||
@ -60,15 +60,15 @@ WARNING: using Gadfly.Plots in module Main conflicts with an existing identifier
|
||||
elapsed time: 3.1334697 seconds
|
||||
```
|
||||
|
||||
# plot and plotter (proposal)
|
||||
# plot and plotter! (proposal)
|
||||
|
||||
The main plot command. You must call `plotter(:ModuleName)` to set the current plotting environment first.
|
||||
The main plot command. You must call `plotter!(:ModuleName)` to set the current plotting environment first.
|
||||
Commands are converted into the relevant plotting commands for that package:
|
||||
|
||||
```
|
||||
plotter(:Gadfly)
|
||||
plotter!(:gadfly)
|
||||
plot(1:10) # this calls `y = 1:10; Gadfly.plot(x=1:length(y), y=y)`
|
||||
plotter(:Qwt)
|
||||
plotter!(:qwt)
|
||||
plot(1:10) # this calls `Qwt.plot(1:10)`
|
||||
```
|
||||
|
||||
|
||||
98
src/Plots.jl
98
src/Plots.jl
@ -1,5 +1,7 @@
|
||||
module Plots
|
||||
|
||||
typealias AVec AbstractVector
|
||||
typealias AMat AbstractMatrix
|
||||
|
||||
# ---------------------------------------------------------
|
||||
|
||||
@ -8,10 +10,18 @@ const IMG_DIR = "$(ENV["HOME"])/.julia/v0.4/Plots/img/"
|
||||
|
||||
# ---------------------------------------------------------
|
||||
|
||||
type CurrentPlot
|
||||
nullableplot::Nullable
|
||||
type Plot
|
||||
o # the underlying object
|
||||
plotter::symbol
|
||||
xdata::Vector{AVec}
|
||||
ydata::Vector{AVec}
|
||||
end
|
||||
const CURRENT_PLOT = CurrentPlot(Nullable{Any}())
|
||||
|
||||
|
||||
type CurrentPlot
|
||||
nullableplot::Nullable{Plot}
|
||||
end
|
||||
const CURRENT_PLOT = CurrentPlot(Nullable{Plot}())
|
||||
|
||||
isplotnull() = isnull(CURRENT_PLOT.nullableplot)
|
||||
|
||||
@ -33,23 +43,26 @@ include("gadfly.jl")
|
||||
|
||||
include("args.jl")
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
|
||||
|
||||
export
|
||||
plotter,
|
||||
plotter!,
|
||||
plot,
|
||||
plot!,
|
||||
# subplot,
|
||||
savepng
|
||||
|
||||
doc"""
|
||||
The main plot command. You must call `plotter(:ModuleName)` to set the current plotting environment first.
|
||||
The main plot command. You must call `plotter!(:ModuleName)` to set the current plotting environment first.
|
||||
Commands are converted into the relevant plotting commands for that package:
|
||||
|
||||
```
|
||||
plotter(:Gadfly)
|
||||
plotter!(:gadfly)
|
||||
plot(1:10) # this calls `y = 1:10; Gadfly.plot(x=1:length(y), y=y)`
|
||||
plotter(:Qwt)
|
||||
plotter!(:qwt)
|
||||
plot(1:10) # this calls `Qwt.plot(1:10)`
|
||||
```
|
||||
|
||||
@ -152,22 +165,30 @@ When plotting multiple lines, you can give every line the same trait by using th
|
||||
|
||||
"""
|
||||
|
||||
function plot(; kw...)
|
||||
plt = Plot()
|
||||
currentPlot@(plt)
|
||||
plot!(plt; kw...)
|
||||
|
||||
# -------------------------
|
||||
|
||||
# this creates a new plot with args/kw and sets it to be the current plot
|
||||
function plot(args...; kw...)
|
||||
plt = newplot(plotter())
|
||||
currentPlot!(plt)
|
||||
plot!(plt, args...; kw...)
|
||||
plt
|
||||
end
|
||||
|
||||
# this adds to the current plot
|
||||
function plot!(args...; kw...)
|
||||
plt = currentPlot()
|
||||
plot!(plt, args...; kw...)
|
||||
plt
|
||||
end
|
||||
|
||||
# -------------------------
|
||||
|
||||
# These methods are various ways to add to an existing plot
|
||||
|
||||
function plot!(plt::Plot, y::AVec; kw...)
|
||||
plot!(plt; x = collect(1:length(y)); y=y, kw...)
|
||||
plot!(plt; x = 1:length(y); y = y, kw...)
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, x::AVec, y::AVec; kw...) # one line (will assert length(x) == length(y))
|
||||
@ -193,44 +214,85 @@ function plot!(plt::Plot, x::AVec, y::AMat; kw...) # multiple lines
|
||||
end
|
||||
|
||||
function plot!(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)
|
||||
for i in 1:size(x,2)
|
||||
plot!(plt; x = x[:,i], y = y[:,i], kw...)
|
||||
end
|
||||
plt
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, x::AVec, f::Function; kw...) # one line, y = f(x)
|
||||
plot!(plt; x = x, y = map(f,x), kw...)
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, x::AMat, f::Function; kw...) # multiple lines, yᵢⱼ = f(xᵢⱼ)
|
||||
for i in 1:size(x,2)
|
||||
xi = x[:,i]
|
||||
plot!(plt; x = xi, y = map(f, xi), kw...)
|
||||
end
|
||||
plt
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ)
|
||||
for i in 1:length(fs)
|
||||
plot!(plt; x = x, y = map(fs[i], x), kw...)
|
||||
end
|
||||
plt
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i])
|
||||
for i in 1:length(y)
|
||||
plot!(plt; x = 1:length(y[i]), y = y[i], kw...)
|
||||
end
|
||||
plt
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i])
|
||||
for i in 1:length(y)
|
||||
@assert length(x) == length(y[i])
|
||||
plot!(plt; x = x, y = y[i], kw...)
|
||||
end
|
||||
plt
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, x::AVec{AVec}, y::AVec{AVec}; kw...) # multiple lines, will assert length(x[i]) == length(y[i])
|
||||
@assert length(x) == length(y)
|
||||
for i in 1:length(x)
|
||||
@assert length(x[i]) == length(y[i])
|
||||
plot!(plt; x = x[i], y = y[i], kw...)
|
||||
end
|
||||
plt
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, n::Integer; kw...) # n lines, all empty (for updating plots)
|
||||
for i in 1:n
|
||||
plot(plt, x = zeros(0), y = zeros(0), kw...)
|
||||
end
|
||||
end
|
||||
|
||||
# -------------------------
|
||||
|
||||
# TODO: how do we handle NA values in dataframes?
|
||||
function plot!(plt::Plot, df::DataFrame; kw...) # one line per DataFrame column, labels == names(df)
|
||||
# this is the core method... add to a plot object using kwargs
|
||||
function plot!(plt::Plot; kw...)
|
||||
plot!(plotter(), plt; kw...)
|
||||
end
|
||||
|
||||
function plot!(plt::Plot, df::DataFrame, columns; kw...) # one line per column, but on a subset of column names
|
||||
end
|
||||
# -------------------------
|
||||
|
||||
# # TODO: how do we handle NA values in dataframes?
|
||||
# function plot!(plt::Plot, df::DataFrame; kw...) # one line per DataFrame column, labels == names(df)
|
||||
# end
|
||||
|
||||
# function plot!(plt::Plot, df::DataFrame, columns; kw...) # one line per column, but on a subset of column names
|
||||
# end
|
||||
|
||||
|
||||
plot(args...; kw...) = currentPlot!(plot(currentPackage(), args...; kw...))
|
||||
# plot(args...; kw...) = currentPlot!(plot(plotter(), args...; kw...))
|
||||
|
||||
|
||||
|
||||
# subplot(args...; kw...) = subplot(currentPackage(), args...; kw...)
|
||||
savepng(args...; kw...) = savepng(currentPackage(), args...; kw...)
|
||||
# subplot(args...; kw...) = subplot(plotter(), args...; kw...)
|
||||
savepng(args...; kw...) = savepng(plotter(), args...; kw...)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -3,7 +3,10 @@
|
||||
|
||||
immutable GadflyPackage <: PlottingPackage end
|
||||
|
||||
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; kw...) = Gadfly.plot(; kw...)
|
||||
savepng(::GadflyPackage, plt, fn::String, args...) = Gadfly.draw(Gadfly.PNG(fn, args...), plt)
|
||||
# newplot(pkg::QwtPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[])
|
||||
# plot(::QwtPackage, plt::Plot; kw...) = Qwt.oplot(plt.o; 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; kw...) = Gadfly.plot(; kw...)
|
||||
# savepng(::GadflyPackage, plt, fn::String, args...) = Gadfly.draw(Gadfly.PNG(fn, args...), plt)
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
immutable QwtPackage <: PlottingPackage end
|
||||
|
||||
plot(::QwtPackage, args...; kw...) = Qwt.plot(args...; kw...)
|
||||
subplot(::QwtPackage, args...; kw...) = Qwt.subplot(args...; kw...)
|
||||
savepng(::QwtPackage, plt, fn::String, args...) = Qwt.savepng(plt, fn)
|
||||
newplot(pkg::QwtPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[])
|
||||
plot(::QwtPackage, plt::Plot; kw...) = Qwt.oplot(plt.o; kw...)
|
||||
# subplot(::QwtPackage, args...; kw...) = Qwt.subplot(args...; kw...)
|
||||
# savepng(::QwtPackage, plt, fn::String, args...) = Qwt.savepng(plt, fn)
|
||||
|
||||
@ -5,11 +5,15 @@ using FactCheck
|
||||
|
||||
|
||||
facts("Qwt") do
|
||||
@fact plotter(:qwt) --> nothing
|
||||
@fact plotter!(:qwt) --> nothing
|
||||
@fact plotter() --> :qwt
|
||||
@fact tpye(plot(1:10, show=false)) --> Plot
|
||||
end
|
||||
|
||||
facts("Gadfly") do
|
||||
@fact plotter(:gadfly) --> nothing
|
||||
@fact plotter!(:gadfly) --> nothing
|
||||
@fact plotter() --> :gadfly
|
||||
@fact tpye(plot(1:10, show=false)) --> Plot
|
||||
end
|
||||
|
||||
end # module
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user