working on pyplot

This commit is contained in:
Thomas Breloff 2015-09-13 17:02:03 -04:00
parent 09a685e977
commit 60a22b1897
4 changed files with 117 additions and 10 deletions

View File

@ -123,9 +123,10 @@ end
# run it! # run it!
# note: generate separately so it's easy to comment out # note: generate separately so it's easy to comment out
generate_markdown(:qwt) # generate_markdown(:qwt)
generate_markdown(:gadfly) # generate_markdown(:gadfly)
generate_markdown(:unicodeplots) # generate_markdown(:unicodeplots)
generate_markdown(:pyplot)
end # module end # module

View File

@ -32,7 +32,8 @@ export
backends, backends,
qwt!, qwt!,
gadfly!, gadfly!,
unicodeplots! unicodeplots!,
pyplot!
# --------------------------------------------------------- # ---------------------------------------------------------
@ -67,6 +68,7 @@ heatmap!(args...; kw...) = plot!(args...; kw..., linetype = :heatmap)
savepng(args...; kw...) = savepng(currentPlot(), args...; kw...) savepng(args...; kw...) = savepng(currentPlot(), args...; kw...)
savepng(plt::PlottingObject, args...; kw...) = savepng(plt.plotter, plt, 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
# --------------------------------------------------------- # ---------------------------------------------------------

73
src/backends/pyplot.jl Normal file
View File

@ -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

View File

@ -3,6 +3,8 @@
include("backends/qwt.jl") include("backends/qwt.jl")
include("backends/gadfly.jl") include("backends/gadfly.jl")
include("backends/unicodeplots.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") 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") 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}() const INITIALIZED_PACKAGES = Set{Symbol}()
backends() = AVAILABLE_PACKAGES 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 type CurrentPackage
sym::Symbol sym::Symbol
pkg::PlottingPackage pkg::PlottingPackage
end end
CurrentPackage(sym::Symbol) = CurrentPackage(sym, getPlottingPackage(sym))
# ---------------------------------------------------------
function pickDefaultBackend() function pickDefaultBackend()
try try
Pkg.installed("Qwt") Pkg.installed("Qwt")
return CurrentPackage(:qwt, QwtPackage()) return CurrentPackage(:qwt)
end
try
Pkg.installed("PyPlot")
return CurrentPackage(:pyplot)
end end
try try
Pkg.installed("Gadfly") Pkg.installed("Gadfly")
return CurrentPackage(:gadfly, GadflyPackage()) return CurrentPackage(:gadfly)
end end
try try
Pkg.installed("UnicodePlots") Pkg.installed("UnicodePlots")
return CurrentPackage(:unicodeplots, UnicodePlotsPackage()) return CurrentPackage(:unicodeplots)
end end
warn("You don't have any of the supported backends installed! Chose from ", backends()) warn("You don't have any of the supported backends installed! Chose from ", backends())
return CurrentPackage(:gadfly, GadflyPackage()) return CurrentPackage(:gadfly)
end end
const CURRENT_PACKAGE = pickDefaultBackend() const CURRENT_PACKAGE = pickDefaultBackend()
println("[Plots.jl] Default backend: ", CURRENT_PACKAGE.sym) println("[Plots.jl] Default backend: ", CURRENT_PACKAGE.sym)
# const CURRENT_PACKAGE = CurrentPackage(:gadfly, GadflyPackage())
# ---------------------------------------------------------
doc""" doc"""
Returns the current plotting package name. Initializes package on first call. Returns the current plotting package name. Initializes package on first call.
""" """
@ -74,6 +97,12 @@ function plotter()
catch catch
error("Couldn't import UnicodePlots. Install it with: Pkg.add(\"UnicodePlots\")") error("Couldn't import UnicodePlots. Install it with: Pkg.add(\"UnicodePlots\")")
end end
elseif currentPackageSymbol == :pyplot
try
@eval import PyPlot
catch
error("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\")")
end
else else
error("Unknown plotter $currentPackageSymbol. Choose from: $AVAILABLE_PACKAGES") error("Unknown plotter $currentPackageSymbol. Choose from: $AVAILABLE_PACKAGES")
end end
@ -96,6 +125,8 @@ function plotter!(modname)
CURRENT_PACKAGE.pkg = GadflyPackage() CURRENT_PACKAGE.pkg = GadflyPackage()
elseif modname == :unicodeplots elseif modname == :unicodeplots
CURRENT_PACKAGE.pkg = UnicodePlotsPackage() CURRENT_PACKAGE.pkg = UnicodePlotsPackage()
elseif modname == :pyplot
CURRENT_PACKAGE.pkg = PyPlotPackage()
else else
error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES") error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES")
end end