From 341f671c7c6e09676f7791aba458eca5fde3f957 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Thu, 10 Sep 2015 11:19:24 -0400 Subject: [PATCH] reg line; reorg of plotter commands --- src/Plots.jl | 2 +- src/args.jl | 7 +++-- src/gadfly.jl | 67 ++++++++++++++++++++++++-------------------- src/plotter.jl | 76 ++++++++++++++++++++++++++++++++++---------------- src/qwt.jl | 9 +----- 5 files changed, 94 insertions(+), 67 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index c4cd26fc..68e90801 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -66,9 +66,9 @@ currentPlot!(plot) = (CURRENT_PLOT.nullableplot = Nullable(plot)) # --------------------------------------------------------- -include("plotter.jl") include("qwt.jl") include("gadfly.jl") +include("plotter.jl") # --------------------------------------------------------- diff --git a/src/args.jl b/src/args.jl index 34792f11..b03c12be 100644 --- a/src/args.jl +++ b/src/args.jl @@ -24,10 +24,11 @@ PLOT_DEFAULTS[:linetype] = :line PLOT_DEFAULTS[:linestyle] = :solid PLOT_DEFAULTS[:marker] = :none PLOT_DEFAULTS[:markercolor] = :match -PLOT_DEFAULTS[:markersize] = 10 -PLOT_DEFAULTS[:heatmap_n] = 100 +PLOT_DEFAULTS[:markersize] = 3 +PLOT_DEFAULTS[:nbins] = 100 # number of bins for heatmaps and hists 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_DEFAULTS[:title] = "" diff --git a/src/gadfly.jl b/src/gadfly.jl index 7bc93c39..77f0bea2 100644 --- a/src/gadfly.jl +++ b/src/gadfly.jl @@ -22,57 +22,62 @@ function plot(pkg::GadflyPackage; kw...) Plot(plt, pkg, 0) end -function getGeoms(linetype::Symbol, marker::Symbol, heatmap_n::Int) - geoms = [] - if linetype in (:heatmap,:hexbin) - push!(geoms, Gadfly.Geom.hexbin(xbincount=heatmap_n, ybincount=heatmap_n)) - else - if linetype == :line - push!(geoms, Gadfly.Geom.line) - elseif linetype == :dots - push!(geoms, Gadfly.Geom.point) - else - error("linetype $linetype not currently supported with Gadfly") - end +function getGeomFromLineType(linetype::Symbol) + linetype == :line && return Gadfly.Geom.line + linetype == :dots && return Gadfly.Geom.point + linetype == :bar && return Gadfly.Geom.bar + linetype == :step && return Gadfly.Geom.step + linetype == :hist && return Gadfly.Geom.hist + error("linetype $linetype not currently supported with Gadfly") +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 push!(geoms, Gadfly.Geom.point) end end + + geoms 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 function plot!(::GadflyPackage, plt::Plot; kw...) d = Dict(kw) 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], line_width = d[:width] * Gadfly.px, default_point_size = d[:markersize] * Gadfly.px) 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 end diff --git a/src/plotter.jl b/src/plotter.jl index 157178de..be7895fb 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -11,46 +11,74 @@ const AVAILABLE_PACKAGES = [:qwt, :gadfly] const INITIALIZED_PACKAGES = Set{Symbol}() type CurrentPackage - pkg::Nullable{PlottingPackage} + # pkg::Nullable{PlottingPackage} + sym::Symbol + pkg::PlottingPackage 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() - if isnull(CURRENT_PACKAGE.pkg) - error("Must choose a plotter. Example: `plotter!(:qwt)`") + # if isnull(CURRENT_PACKAGE.pkg) + # 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 + push!(INITIALIZED_PACKAGES, currentPackageSymbol) + # plotter!(CURRENT_PACKAGE.sym) + end - get(CURRENT_PACKAGE.pkg) + # get(CURRENT_PACKAGE.pkg) + println("Current package: $CURRENT_PACKAGE") + CURRENT_PACKAGE.pkg end doc""" -Setup the plot environment. -`plotter!(:qwt)` will load package Qwt.jl and map all subsequent plot commands to that package. -Same for `plotter!(:gadfly)`, etc. +Set the plot backend. Choose from: :qwt, :gadfly """ function plotter!(modname) if modname == :qwt - if !(modname in INITIALIZED_PACKAGES) - # qwt() - @eval import Qwt - push!(INITIALIZED_PACKAGES, modname) - end + # if !(modname in INITIALIZED_PACKAGES) + # # qwt() + # @eval import Qwt + # push!(INITIALIZED_PACKAGES, modname) + # end # global Qwt = Main.Qwt - CURRENT_PACKAGE.pkg = Nullable(QwtPackage()) - return + # CURRENT_PACKAGE.sym = modname + # CURRENT_PACKAGE.pkg = Nullable(QwtPackage()) + CURRENT_PACKAGE.pkg = QwtPackage() + # return elseif modname == :gadfly - if !(modname in INITIALIZED_PACKAGES) - # gadfly() - @eval import Gadfly - push!(INITIALIZED_PACKAGES, modname) - end + # if !(modname in INITIALIZED_PACKAGES) + # # gadfly() + # @eval import Gadfly + # push!(INITIALIZED_PACKAGES, modname) + # end # global Gadfly = Main.Gadfly - CURRENT_PACKAGE.pkg = Nullable(GadflyPackage()) - return + # CURRENT_PACKAGE.sym = modname + # CURRENT_PACKAGE.pkg = Nullable(GadflyPackage()) + CURRENT_PACKAGE.pkg = GadflyPackage() + # return + else + error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES") end - error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES") + + CURRENT_PACKAGE.sym = modname + CURRENT_PACKAGE.pkg end diff --git a/src/qwt.jl b/src/qwt.jl index 8da61d5c..40790961 100644 --- a/src/qwt.jl +++ b/src/qwt.jl @@ -8,25 +8,18 @@ function adjustQwtKeywords(; kw...) if d[:linetype] == :hexbin d[:linetype] = :heatmap end + d[:heatmap_n] = d[:nbins] d end function plot(pkg::QwtPackage; kw...) kw = adjustQwtKeywords(;kw...) 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 end function plot!(::QwtPackage, plt::Plot; 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...) end