From 55a85cdaafdb28b6a603f9fc982a6e13bce684cd Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Thu, 3 Sep 2015 18:35:50 -0400 Subject: [PATCH] args --- src/Plots.jl | 2 ++ src/args.jl | 75 ++++++++++++++++++++++++++++++++++++++---------- src/gadfly.jl | 2 +- src/plot.jl | 20 ++++++++++--- src/qwt.jl | 2 +- test/runtests.jl | 10 ++++--- 6 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index 0803a119..09054711 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -8,6 +8,8 @@ export plot!, currentPlot, currentPlot!, + plotDefault, + plotDefault!, savepng # --------------------------------------------------------- diff --git a/src/args.jl b/src/args.jl index 41e722da..5509391e 100644 --- a/src/args.jl +++ b/src/args.jl @@ -10,19 +10,64 @@ const LINE_TYPES = (:line, :step, :stepinverted, :sticks, :dots, :none, :heatmap const LINE_STYLES = (:solid, :dash, :dot, :dashdot, :dashdotdot) const LINE_MARKERS = (:none, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon) -const DEFAULT_axis = LINE_AXES[1] -const DEFAULT_color = :auto -const DEFAULT_label = "AUTO" -const DEFAULT_width = 2 -const DEFAULT_linetype = LINE_TYPES[1] -const DEFAULT_linestyle = LINE_STYLES[1] -const DEFAULT_marker = LINE_MARKERS[1] -const DEFAULT_markercolor = :auto -const DEFAULT_markersize = 10 -const DEFAULT_heatmap_n = 100 -const DEFAULT_heatmap_c = (0.15, 0.5) -const DEFAULT_title = "" -const DEFAULT_xlabel = "" -const DEFAULT_ylabel = "" -const DEFAULT_yrightlabel = "" +const PLOT_DEFAULTS = Dict{Symbol, Any}() +PLOT_DEFAULTS[:axis] = :left +PLOT_DEFAULTS[:color] = :auto +PLOT_DEFAULTS[:label] = "AUTO" +PLOT_DEFAULTS[:width] = 1 +PLOT_DEFAULTS[:linetype] = :line +PLOT_DEFAULTS[:linestyle] = :solid +PLOT_DEFAULTS[:marker] = :none +PLOT_DEFAULTS[:markercolor] = :auto +PLOT_DEFAULTS[:markersize] = 10 +PLOT_DEFAULTS[:heatmap_n] = 100 +PLOT_DEFAULTS[:heatmap_c] = (0.15, 0.5) +PLOT_DEFAULTS[:title] = "" +PLOT_DEFAULTS[:xlabel] = "" +PLOT_DEFAULTS[:ylabel] = "" +PLOT_DEFAULTS[:yrightlabel] = "" + +plotDefault(sym::Symbol) = PLOT_DEFAULTS[sym] +function plotDefault!(sym::Symbol, val) + PLOT_DEFAULTS[sym] = val +end + +makeplural(s::Symbol) = Symbol(string(s,"s")) +autocolor(idx::Integer) = COLORS[mod1(idx,NUMCOLORS)] + + +function getPlotKeywordArgs(kw, i::Int) + d = Dict(kw) + kw = Dict() + + for k in keys(PLOT_DEFAULTS) + plural = makeplural(k) + if haskey(d, plural) + kw[k] = d[plural][i] + elseif haskey(d, k) + kw[k] = d[k] + else + kw[k] = PLOT_DEFAULTS[k] + end + end + + kw +end + +# const DEFAULT_axis = LINE_AXES[1] +# const DEFAULT_color = :auto +# const DEFAULT_label = "AUTO" +# const DEFAULT_width = 2 +# const DEFAULT_linetype = LINE_TYPES[1] +# const DEFAULT_linestyle = LINE_STYLES[1] +# const DEFAULT_marker = LINE_MARKERS[1] +# const DEFAULT_markercolor = :auto +# const DEFAULT_markersize = 10 +# const DEFAULT_heatmap_n = 100 +# const DEFAULT_heatmap_c = (0.15, 0.5) + +# const DEFAULT_title = "" +# const DEFAULT_xlabel = "" +# const DEFAULT_ylabel = "" +# const DEFAULT_yrightlabel = "" diff --git a/src/gadfly.jl b/src/gadfly.jl index 1c70dea1..bd2be2b8 100644 --- a/src/gadfly.jl +++ b/src/gadfly.jl @@ -3,7 +3,7 @@ immutable GadflyPackage <: PlottingPackage end -# newplot(pkg::QwtPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[]) +# plot(pkg::QwtPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[]) # plot!(::GadflyPackage, plt::Plot; kw...) = Qwt.oplot(plt.o; kw...) # plot(::GadflyPackage, y; kw...) = Gadfly.plot(; x = 1:length(y), y = y, kw...) diff --git a/src/plot.jl b/src/plot.jl index 9129aa34..88b4ba15 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -115,9 +115,9 @@ When plotting multiple lines, you can give every line the same trait by using th # this creates a new plot with args/kw and sets it to be the current plot function plot(args...; kw...) - plt = newplot(plotter()) - plot!(plt, args...; kw...) - currentPlot!(plt) + plt = plot(plotter(); getPlotKeywordArgs(kw, 1)...) # create a new, blank plot + plot!(plt, args...; kw...) # add the series to that plot + currentPlot!(plt) # set this as the current plot plt end @@ -133,17 +133,20 @@ end # These methods are various ways to add to an existing plot function plot!(plt::Plot, y::AVec; kw...) + kw = getPlotKeywordArgs(kw, 1) 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)) @assert length(x) == length(y) + kw = getPlotKeywordArgs(kw, 1) plot!(plt; x=x, y=y, kw...) end function plot!(plt::Plot, y::AMat; kw...) # multiple lines (one per column of x), all sharing x = 1:size(y,1) n,m = size(y) for i in 1:m + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = 1:n, y = y[:,i], kw...) end plt @@ -153,6 +156,7 @@ function plot!(plt::Plot, x::AVec, y::AMat; kw...) # multiple lines n,m = size(y) for i in 1:m @assert length(x) == n + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = x, y = y[:,i], kw...) end plt @@ -161,18 +165,21 @@ 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) + kw = getPlotKeywordArgs(kw, i) 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) + kw = getPlotKeywordArgs(kw, 1) 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] + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = xi, y = map(f, xi), kw...) end plt @@ -180,6 +187,7 @@ end function plot!(plt::Plot, x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ) for i in 1:length(fs) + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = x, y = map(fs[i], x), kw...) end plt @@ -187,6 +195,7 @@ end function plot!(plt::Plot, y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i]) for i in 1:length(y) + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = 1:length(y[i]), y = y[i], kw...) end plt @@ -195,6 +204,7 @@ 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]) + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = x, y = y[i], kw...) end plt @@ -204,6 +214,7 @@ function plot!(plt::Plot, x::AVec{AVec}, y::AVec{AVec}; kw...) # multiple lines @assert length(x) == length(y) for i in 1:length(x) @assert length(x[i]) == length(y[i]) + kw = getPlotKeywordArgs(kw, i) plot!(plt; x = x[i], y = y[i], kw...) end plt @@ -211,13 +222,14 @@ end function plot!(plt::Plot, n::Integer; kw...) # n lines, all empty (for updating plots) for i in 1:n + kw = getPlotKeywordArgs(kw, i) plot(plt, x = zeros(0), y = zeros(0), kw...) end end # ------------------------- -# this is the core method... add to a plot object using kwargs +# this is the core method... add to a plot object using kwargs, with args already converted into kwargs function plot!(plt::Plot; kw...) plot!(plotter(), plt; kw...) end diff --git a/src/qwt.jl b/src/qwt.jl index f56a1d20..4d3ec0b8 100644 --- a/src/qwt.jl +++ b/src/qwt.jl @@ -3,7 +3,7 @@ immutable QwtPackage <: PlottingPackage end -newplot(pkg::QwtPackage) = Plot(Qwt.plot(zeros(0,0)), pkg, AVec[], AVec[]) +plot(pkg::QwtPackage; kw...) = Plot(Qwt.plot(zeros(0,0); kw...), 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) diff --git a/test/runtests.jl b/test/runtests.jl index 4f8c236d..8cbe76e2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,16 +5,18 @@ using FactCheck facts("Qwt") do + plotDefault!(:show, false) + @fact plotter!(:qwt) --> nothing @fact plotter() --> Plots.QwtPackage() - @fact typeof(plot(1:10, show=false)) --> Plot + @fact typeof(plot(1:10)) --> Plot # plot(y::AVec; kw...) # one line... x = 1:length(y) - @fact plot(1:10, show=false) --> not(nothing) + @fact plot(1:10) --> not(nothing) @fact length(currentPlot().o.lines) --> 1 # plot(x::AVec, y::AVec; kw...) # one line (will assert length(x) == length(y)) - @fact plot(Int[1,2,3], rand(3); show=false) --> not(nothing) + @fact plot(Int[1,2,3], rand(3)) --> not(nothing) @fact_throws plot(1:5, 1:4) # plot(y::AMat; kw...) # multiple lines (one per column of x), all sharing x = 1:size(y,1) @@ -37,7 +39,7 @@ end facts("Gadfly") do @fact plotter!(:gadfly) --> nothing @fact plotter() --> Plots.GadflyPackage() - @fact typeof(plot(1:10, show=false)) --> Plot + @fact typeof(plot(1:10)) --> Plot end FactCheck.exitstatus()