diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 85dc6a71..8a0efd66 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -123,9 +123,10 @@ end # run it! # note: generate separately so it's easy to comment out -generate_markdown(:qwt) -generate_markdown(:gadfly) -generate_markdown(:unicodeplots) +# generate_markdown(:qwt) +# generate_markdown(:gadfly) +# generate_markdown(:unicodeplots) +generate_markdown(:pyplot) end # module diff --git a/src/Plots.jl b/src/Plots.jl index b37419ab..3e687b13 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -32,7 +32,8 @@ export backends, qwt!, gadfly!, - unicodeplots! + unicodeplots!, + pyplot! # --------------------------------------------------------- @@ -67,6 +68,7 @@ heatmap!(args...; kw...) = plot!(args...; kw..., linetype = :heatmap) savepng(args...; kw...) = savepng(currentPlot(), args...; kw...) savepng(plt::PlottingObject, args...; kw...) = savepng(plt.plotter, plt, args...; kw...) +savepng(::PlottingPackage, plt::PlottingObject, fn::String, args...) = error("unsupported") # fallback so multiple dispatch doesn't get confused if it's missing # --------------------------------------------------------- diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl new file mode 100644 index 00000000..f4424a68 --- /dev/null +++ b/src/backends/pyplot.jl @@ -0,0 +1,73 @@ + +# https://github.com/tbreloff/Qwt.jl + +immutable PyPlotPackage <: PlottingPackage end + +pyplot!() = plotter!(:pyplot) + +# ------------------------------- + +# function adjustQwtKeywords(iscreating::Bool; kw...) +# d = Dict(kw) +# d[:heatmap_n] = d[:nbins] + +# if d[:linetype] == :hexbin +# d[:linetype] = :heatmap +# elseif d[:linetype] == :dots +# d[:linetype] = :none +# d[:marker] = :hexagon +# elseif !iscreating && d[:linetype] == :bar +# return barHack(; kw...) +# elseif !iscreating && d[:linetype] == :hist +# return barHack(; histogramHack(; kw...)...) +# end +# d +# end + +function plot(pkg::PyPlotPackage; kw...) + # kw = adjustQwtKeywords(true; kw...) + # o = Qwt.plot(zeros(0,0); kw..., show=false) + plt = Plot(o, pkg, 0, kw, Dict[]) + plt +end + +function plot!(::PyPlotPackage, plt::Plot; kw...) + # kw = adjustQwtKeywords(false; kw...) + # Qwt.oplot(plt.o; kw...) + push!(plt.seriesargs, kw) + plt +end + +function Base.display(::PyPlotPackage, plt::Plot) + # Qwt.refresh(plt.o) + # Qwt.showwidget(plt.o) + display(plt.o) +end + +# ------------------------------- + +savepng(::PyPlotPackage, plt::PlottingObject, fn::String, args...) = error("unsupported") + +# ------------------------------- + +# create the underlying object (each backend will do this differently) +function buildSubplotObject!(::PyPlotPackage, subplt::Subplot) + # i = 0 + # rows = [] + # for rowcnt in subplt.layout.rowcounts + # push!(rows, Qwt.hsplitter([plt.o for plt in subplt.plts[(1:rowcnt) + i]]...)) + # i += rowcnt + # end + # subplt.o = Qwt.vsplitter(rows...) + error("unsupported") +end + + +function Base.display(::PyPlotPackage, subplt::Subplot) + # for plt in subplt.plts + # Qwt.refresh(plt.o) + # end + # Qwt.showwidget(subplt.o) + display(subplt.o) +end + diff --git a/src/plotter.jl b/src/plotter.jl index fb6aae75..c62a0636 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -3,6 +3,8 @@ include("backends/qwt.jl") include("backends/gadfly.jl") include("backends/unicodeplots.jl") +include("backends/pyplot.jl") + # --------------------------------------------------------- @@ -12,40 +14,61 @@ 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") Base.display(pkg::PlottingPackage, plt::Plot) = error("display($pkg, plt) is not implemented") +subplot(pkg::PlottingPackage; kw...) = error("subplot($pkg; kw...) is not implemented") +subplot!(pkg::PlottingPackage, subplt::Subplot; kw...) = error("subplot!($pkg, subplt; kw...) is not implemented") +Base.display(pkg::PlottingPackage, subplt::Subplot) = error("display($pkg, subplt) is not implemented") + # --------------------------------------------------------- -const AVAILABLE_PACKAGES = [:qwt, :gadfly, :unicodeplots] +const AVAILABLE_PACKAGES = [:qwt, :gadfly, :unicodeplots, :pyplot] const INITIALIZED_PACKAGES = Set{Symbol}() backends() = AVAILABLE_PACKAGES +function getPlottingPackage(sym::Symbol) + sym == :qwt && return QwtPackage() + sym == :gadfly && return GadflyPackage() + sym == :unicodeplots && return UnicodePlotsPackage() + sym == :pyplot && return PyPlotPackage() + error("Unsupported backend $sym") +end + + type CurrentPackage sym::Symbol pkg::PlottingPackage end +CurrentPackage(sym::Symbol) = CurrentPackage(sym, getPlottingPackage(sym)) + +# --------------------------------------------------------- function pickDefaultBackend() try Pkg.installed("Qwt") - return CurrentPackage(:qwt, QwtPackage()) + return CurrentPackage(:qwt) + end + try + Pkg.installed("PyPlot") + return CurrentPackage(:pyplot) end try Pkg.installed("Gadfly") - return CurrentPackage(:gadfly, GadflyPackage()) + return CurrentPackage(:gadfly) end try Pkg.installed("UnicodePlots") - return CurrentPackage(:unicodeplots, UnicodePlotsPackage()) + return CurrentPackage(:unicodeplots) end warn("You don't have any of the supported backends installed! Chose from ", backends()) - return CurrentPackage(:gadfly, GadflyPackage()) + return CurrentPackage(:gadfly) end const CURRENT_PACKAGE = pickDefaultBackend() println("[Plots.jl] Default backend: ", CURRENT_PACKAGE.sym) -# const CURRENT_PACKAGE = CurrentPackage(:gadfly, GadflyPackage()) +# --------------------------------------------------------- + doc""" Returns the current plotting package name. Initializes package on first call. """ @@ -74,6 +97,12 @@ function plotter() catch error("Couldn't import UnicodePlots. Install it with: Pkg.add(\"UnicodePlots\")") end + elseif currentPackageSymbol == :pyplot + try + @eval import PyPlot + catch + error("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\")") + end else error("Unknown plotter $currentPackageSymbol. Choose from: $AVAILABLE_PACKAGES") end @@ -96,6 +125,8 @@ function plotter!(modname) CURRENT_PACKAGE.pkg = GadflyPackage() elseif modname == :unicodeplots CURRENT_PACKAGE.pkg = UnicodePlotsPackage() + elseif modname == :pyplot + CURRENT_PACKAGE.pkg = PyPlotPackage() else error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES") end