reg line; reorg of plotter commands

This commit is contained in:
Thomas Breloff 2015-09-10 11:19:24 -04:00
parent c2d7704d3f
commit 341f671c7c
5 changed files with 94 additions and 67 deletions

View File

@ -66,9 +66,9 @@ currentPlot!(plot) = (CURRENT_PLOT.nullableplot = Nullable(plot))
# --------------------------------------------------------- # ---------------------------------------------------------
include("plotter.jl")
include("qwt.jl") include("qwt.jl")
include("gadfly.jl") include("gadfly.jl")
include("plotter.jl")
# --------------------------------------------------------- # ---------------------------------------------------------

View File

@ -24,10 +24,11 @@ PLOT_DEFAULTS[:linetype] = :line
PLOT_DEFAULTS[:linestyle] = :solid PLOT_DEFAULTS[:linestyle] = :solid
PLOT_DEFAULTS[:marker] = :none PLOT_DEFAULTS[:marker] = :none
PLOT_DEFAULTS[:markercolor] = :match PLOT_DEFAULTS[:markercolor] = :match
PLOT_DEFAULTS[:markersize] = 10 PLOT_DEFAULTS[:markersize] = 3
PLOT_DEFAULTS[:heatmap_n] = 100 PLOT_DEFAULTS[:nbins] = 100 # number of bins for heatmaps and hists
PLOT_DEFAULTS[:heatmap_c] = (0.15, 0.5) PLOT_DEFAULTS[:heatmap_c] = (0.15, 0.5)
PLOT_DEFAULTS[:fillto] = nothing # fills in the area PLOT_DEFAULTS[:fillto] = nothing # fills in the area
PLOT_DEFAULTS[:reg] = false # regression line?
# plot globals # plot globals
PLOT_DEFAULTS[:title] = "" PLOT_DEFAULTS[:title] = ""

View File

@ -22,57 +22,62 @@ function plot(pkg::GadflyPackage; kw...)
Plot(plt, pkg, 0) Plot(plt, pkg, 0)
end end
function getGeoms(linetype::Symbol, marker::Symbol, heatmap_n::Int) function getGeomFromLineType(linetype::Symbol)
geoms = [] linetype == :line && return Gadfly.Geom.line
if linetype in (:heatmap,:hexbin) linetype == :dots && return Gadfly.Geom.point
push!(geoms, Gadfly.Geom.hexbin(xbincount=heatmap_n, ybincount=heatmap_n)) linetype == :bar && return Gadfly.Geom.bar
else linetype == :step && return Gadfly.Geom.step
if linetype == :line linetype == :hist && return Gadfly.Geom.hist
push!(geoms, Gadfly.Geom.line)
elseif linetype == :dots
push!(geoms, Gadfly.Geom.point)
else
error("linetype $linetype not currently supported with Gadfly") error("linetype $linetype not currently supported with Gadfly")
end end
function getGeoms(linetype::Symbol, marker::Symbol, nbins::Int)
geoms = []
# handle heatmaps (hexbins) specially
if linetype in (:heatmap,:hexbin)
push!(geoms, Gadfly.Geom.hexbin(xbincount=nbins, ybincount=nbins))
else
# for other linetypes, get the correct Geom
push!(geoms, getGeomFromLineType(linetype))
# for any marker, add Geom.point
if marker != :none if marker != :none
push!(geoms, Gadfly.Geom.point) push!(geoms, Gadfly.Geom.point)
end end
end end
geoms
end end
# # note: currently only accepts lines and dots
# function getGeomLine(linetype::Symbol, heatmap_n::Int)
# linetype == :line && return [Gadfly.Geom.line]
# linetype == :dots && return [Gadfly.Geom.point]
# linetype in (:heatmap, :hexbin) && return [Gadfly.Geom.hexbin(xbincount=heatmap_n, ybincount=heatmap_n)]
# error("linetype $linetype not currently supported with Gadfly")
# end
# # note: currently map any marker to point
# function getGeomPoint(linetype::Syombol, marker::Symbol)
# if marker == :none || linetype in (:heatmap, :hexbin)
# return []
# end
# [Gadfly.Geom.point]
# end
# plot one data series # plot one data series
function plot!(::GadflyPackage, plt::Plot; kw...) function plot!(::GadflyPackage, plt::Plot; kw...)
d = Dict(kw) d = Dict(kw)
gfargs = [] gfargs = []
# append!(gfargs, getGeomLine(d[:linetype], d[:heatmap_n]))
# append!(gfargs, getGeomPoint(d[:marker]))
append!(gfargs, getGeoms(d[:linetype], d[:marker], d[:heatmap_n]))
# add the Geoms
println(d)
append!(gfargs, getGeoms(d[:linetype], d[:marker], d[:nbins]))
# set color, line width, and point size
theme = Gadfly.Theme(default_color = d[:color], theme = Gadfly.Theme(default_color = d[:color],
line_width = d[:width] * Gadfly.px, line_width = d[:width] * Gadfly.px,
default_point_size = d[:markersize] * Gadfly.px) default_point_size = d[:markersize] * Gadfly.px)
push!(gfargs, theme) push!(gfargs, theme)
# add a regression line?
if d[:reg]
push!(gfargs, Gadfly.Geom.smooth(method=:lm))
end
append!(plt.o.layers, Gadfly.layer(unique(gfargs)...; x = d[:x], y = d[:y])) # for histograms, set x=y
x = d[d[:linetype] == :hist ? :y : :x]
# add the layer to the Gadfly.Plot
append!(plt.o.layers, Gadfly.layer(unique(gfargs)...; x = x, y = d[:y]))
plt plt
end end

View File

@ -11,46 +11,74 @@ const AVAILABLE_PACKAGES = [:qwt, :gadfly]
const INITIALIZED_PACKAGES = Set{Symbol}() const INITIALIZED_PACKAGES = Set{Symbol}()
type CurrentPackage type CurrentPackage
pkg::Nullable{PlottingPackage} # pkg::Nullable{PlottingPackage}
sym::Symbol
pkg::PlottingPackage
end end
const CURRENT_PACKAGE = CurrentPackage(Nullable{PlottingPackage}()) # const CURRENT_PACKAGE = CurrentPackage(Nullable{PlottingPackage}())
const CURRENT_PACKAGE = CurrentPackage(:qwt, QwtPackage())
doc"""Returns the current plotting package name.""" doc"""
Returns the current plotting package name. Initializes package on first use.
"""
function plotter() function plotter()
if isnull(CURRENT_PACKAGE.pkg) # if isnull(CURRENT_PACKAGE.pkg)
error("Must choose a plotter. Example: `plotter!(:qwt)`") # error("Must choose a plotter. Example: `plotter!(:qwt)`")
# end
currentPackageSymbol = CURRENT_PACKAGE.sym
if !(currentPackageSymbol in INITIALIZED_PACKAGES)
# initialize
if currentPackageSymbol == :qwt
@eval import Qwt
elseif currentPackageSymbol == :gadfly
@eval import Gadfly
else
error("Unknown plotter $currentPackageSymbol. Choose from: $AVAILABLE_PACKAGES")
end end
get(CURRENT_PACKAGE.pkg) push!(INITIALIZED_PACKAGES, currentPackageSymbol)
# plotter!(CURRENT_PACKAGE.sym)
end
# get(CURRENT_PACKAGE.pkg)
println("Current package: $CURRENT_PACKAGE")
CURRENT_PACKAGE.pkg
end end
doc""" doc"""
Setup the plot environment. Set the plot backend. Choose from: :qwt, :gadfly
`plotter!(:qwt)` will load package Qwt.jl and map all subsequent plot commands to that package.
Same for `plotter!(:gadfly)`, etc.
""" """
function plotter!(modname) function plotter!(modname)
if modname == :qwt if modname == :qwt
if !(modname in INITIALIZED_PACKAGES) # if !(modname in INITIALIZED_PACKAGES)
# qwt() # # qwt()
@eval import Qwt # @eval import Qwt
push!(INITIALIZED_PACKAGES, modname) # push!(INITIALIZED_PACKAGES, modname)
end # end
# global Qwt = Main.Qwt # global Qwt = Main.Qwt
CURRENT_PACKAGE.pkg = Nullable(QwtPackage()) # CURRENT_PACKAGE.sym = modname
return # CURRENT_PACKAGE.pkg = Nullable(QwtPackage())
CURRENT_PACKAGE.pkg = QwtPackage()
# return
elseif modname == :gadfly elseif modname == :gadfly
if !(modname in INITIALIZED_PACKAGES) # if !(modname in INITIALIZED_PACKAGES)
# gadfly() # # gadfly()
@eval import Gadfly # @eval import Gadfly
push!(INITIALIZED_PACKAGES, modname) # push!(INITIALIZED_PACKAGES, modname)
end # end
# global Gadfly = Main.Gadfly # global Gadfly = Main.Gadfly
CURRENT_PACKAGE.pkg = Nullable(GadflyPackage()) # CURRENT_PACKAGE.sym = modname
return # CURRENT_PACKAGE.pkg = Nullable(GadflyPackage())
CURRENT_PACKAGE.pkg = GadflyPackage()
# return
end else
error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES") error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES")
end end
CURRENT_PACKAGE.sym = modname
CURRENT_PACKAGE.pkg
end

View File

@ -8,25 +8,18 @@ function adjustQwtKeywords(; kw...)
if d[:linetype] == :hexbin if d[:linetype] == :hexbin
d[:linetype] = :heatmap d[:linetype] = :heatmap
end end
d[:heatmap_n] = d[:nbins]
d d
end end
function plot(pkg::QwtPackage; kw...) function plot(pkg::QwtPackage; kw...)
kw = adjustQwtKeywords(;kw...) kw = adjustQwtKeywords(;kw...)
plt = Plot(Qwt.plot(zeros(0,0); kw..., show=false), pkg, 0) plt = Plot(Qwt.plot(zeros(0,0); kw..., show=false), pkg, 0)
# d = Dict(kw)
# if haskey(d, :background_color)
# Qwt.background!(plt.o, Dict(kw)[:background_color])
# end
plt plt
end end
function plot!(::QwtPackage, plt::Plot; kw...) function plot!(::QwtPackage, plt::Plot; kw...)
kw = adjustQwtKeywords(;kw...) kw = adjustQwtKeywords(;kw...)
# d = Dict(kw)
# if haskey(d, :background_color)
# Qwt.background!(plt.o, Dict(kw)[:background_color])
# end
Qwt.oplot(plt.o; kw...) Qwt.oplot(plt.o; kw...)
end end