From 86a56e487abe1b0515e3122f32ded244dd78cf99 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Mon, 21 Sep 2015 14:38:03 -0400 Subject: [PATCH] working on aliases --- docs/example_generation.jl | 2 +- docs/readme_template.md | 0 src/Plots.jl | 1 + src/args.jl | 210 ++++++++++++++++++++++++++----------- src/backends/qwt.jl | 22 ++-- src/plot.jl | 4 +- src/subplot.jl | 4 +- src/utils.jl | 10 ++ 8 files changed, 177 insertions(+), 76 deletions(-) create mode 100644 docs/readme_template.md diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 12d66202..275ebb12 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -51,7 +51,7 @@ const examples = PlotExample[ [:(heatmap(randn(10000),randn(10000); nbins=100))]), PlotExample("Line types", "", - [:(types = intersect(supportedTypes(), [:line, :step, :stepinverted, :sticks, :scatter])), + [:(types = intersect(supportedTypes(), [:line, :path, :steppre, :steppost, :sticks, :scatter])), :(n = length(types)), :(x = Vector[sort(rand(20)) for i in 1:n]), :(y = rand(20,n)), diff --git a/docs/readme_template.md b/docs/readme_template.md new file mode 100644 index 00000000..e69de29b diff --git a/src/Plots.jl b/src/Plots.jl index a7b6af20..8885f716 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -38,6 +38,7 @@ export savepng, backends, + aliases, supportedArgs, supportedAxes, diff --git a/src/args.jl b/src/args.jl index f7908d83..463838e4 100644 --- a/src/args.jl +++ b/src/args.jl @@ -1,10 +1,73 @@ -const _allAxes = [:auto, :left, :right] -const _allTypes = [:none, :line, :step, :stepinverted, :sticks, :scatter, - :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] -const _allStyles = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] -const _allMarkers = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon, :octagon] +const _allAxes = [:auto, :left, :right] +const _axesAliases = Dict( + :a => :auto, + :l => :left, + :r => :right + ) + +const _allTypes = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, + :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] +const _typeAliases = Dict( + :n => :none, + :no => :none, + :l => :line, + :p => :path, + :stepinv => :steppre, + :stepinverted => :steppre, + :step => :steppost, + :step => :steppost, + :stair => :steppost, + :stairs => :steppost, + :stem => :sticks, + :dots => :scatter, + :histogram => :hist, + ) + +const _allStyles = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] +const _styleAliases = Dict( + :a => :auto, + :s => :solid, + :d => :dash, + :dd => :dashdot, + :ddd => :dashdotdot, + ) + +const _allMarkers = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon, :octagon] +const _markerAliases = Dict( + :n => :none, + :no => :none, + :a => :auto, + :circle => :ellipse, + :c => :ellipse, + :square => :rect, + :sq => :rect, + :r => :rect, + :d => :diamond, + :^ => :utriangle, + :ut => :utriangle, + :utri => :utriangle, + :uptri => :utriangle, + :uptriangle => :utriangle, + :v => :dtriangle, + :V => :dtriangle, + :dt => :dtriangle, + :dtri => :dtriangle, + :downtri => :dtriangle, + :downtriangle => :dtriangle, + :+ => :cross, + :plus => :cross, + :x => :xcross, + :X => :xcross, + :star => :star1, + :s => :star1, + :s2 => :star2, + :h => :hexagon, + :hex => :hexagon, + :o => :octagon, + :oct => :octagon, + ) supportedAxes(::PlottingPackage) = _allAxes supportedTypes(::PlottingPackage) = _allTypes @@ -27,7 +90,7 @@ _seriesDefaults[:axis] = :left _seriesDefaults[:color] = :auto _seriesDefaults[:label] = "AUTO" _seriesDefaults[:width] = 1 -_seriesDefaults[:linetype] = :line +_seriesDefaults[:linetype] = :path _seriesDefaults[:linestyle] = :solid _seriesDefaults[:marker] = :none _seriesDefaults[:markercolor] = :match @@ -77,51 +140,65 @@ autopick(notarr, idx::Integer) = notarr autopick_ignore_none_auto(arr::AVec, idx::Integer) = autopick(setdiff(arr, [:none, :auto]), idx) autopick_ignore_none_auto(notarr, idx::Integer) = notarr +function aliasesAndAutopick(d::Dict, sym::Symbol, aliases::Dict, options::AVec, plotIndex::Int) + if d[sym] == :auto + d[sym] = autopick_ignore_none_auto(options, plotIndex) + elseif haskey(aliases, d[sym]) + d[sym] = aliases[d[sym]] + end +end + +function aliases(aliasMap::Dict, val) + sort(vcat(val, collect(keys(filter((k,v)-> v==val, aliasMap))))) +end + # ----------------------------------------------------------------------------- # Alternate args const _keyAliases = Dict( - :c => :color, - :l => :label, - :w => :width, - :linewidth => :width, - :type => :linetype, - :t => :linetype, - :style => :linestyle, - :s => :linestyle, - :m => :marker, - :mc => :markercolor, - :mcolor => :markercolor, - :ms => :markersize, - :msize => :markersize, - :nb => :nbins, - :nbin => :nbins, - :fill => :fillto, - :g => :group, - :r => :ribbon, - :xlab => :xlabel, - :ylab => :ylabel, - :yrlab => :yrightlabel, - :ylabr => :yrightlabel, - :y2lab => :yrightlabel, - :ylab2 => :yrightlabel, - :ylabelright => :yrightlabel, - :ylabel2 => :yrightlabel, - :y2label => :yrightlabel, - :leg => :legend, - :bg => :background_color, - :bgcolor => :background_color, - :bg_color => :background_color, - :background => :background_color, - :fg => :foreground_color, - :fgcolor => :foreground_color, - :fg_color => :foreground_color, - :foreground => :foreground_color, - :windowsize => :size, - :wsize => :size, - :wtitle => :windowtitle, - :display => :show, + :c => :color, + :lab => :label, + :w => :width, + :linewidth => :width, + :type => :linetype, + :lt => :linetype, + :t => :linetype, + :style => :linestyle, + :s => :linestyle, + :ls => :linestyle, + :m => :marker, + :mc => :markercolor, + :mcolor => :markercolor, + :ms => :markersize, + :msize => :markersize, + :nb => :nbins, + :nbin => :nbins, + :fill => :fillto, + :g => :group, + :r => :ribbon, + :xlab => :xlabel, + :ylab => :ylabel, + :yrlab => :yrightlabel, + :ylabr => :yrightlabel, + :y2lab => :yrightlabel, + :ylab2 => :yrightlabel, + :ylabelright => :yrightlabel, + :ylabel2 => :yrightlabel, + :y2label => :yrightlabel, + :leg => :legend, + :bg => :background_color, + :bgcolor => :background_color, + :bg_color => :background_color, + :background => :background_color, + :fg => :foreground_color, + :fgcolor => :foreground_color, + :fg_color => :foreground_color, + :foreground => :foreground_color, + :windowsize => :size, + :wsize => :size, + :wtitle => :windowtitle, + :display => :show, ) # add all pluralized forms to the _keyAliases dict @@ -130,14 +207,14 @@ for arg in keys(_seriesDefaults) end -function replaceAliases!(d::Dict) - for (k,v) in d - if haskey(_keyAliases, k) - d[_keyAliases[k]] = v - delete!(d, k) - end - end -end +# function replaceAliases!(d::Dict) +# for (k,v) in d +# if haskey(_keyAliases, k) +# d[_keyAliases[k]] = v +# delete!(d, k) +# end +# end +# end # ----------------------------------------------------------------------------- @@ -204,6 +281,7 @@ end + # note: idx is the index of this series within this call, n is the index of the series from all calls to plot/subplot function getSeriesArgs(pkg::PlottingPackage, initargs::Dict, kw, commandIndex::Int, plotIndex::Int, globalIndex::Int) # TODO, pass in initargs, not plt d = Dict(kw) @@ -221,16 +299,20 @@ function getSeriesArgs(pkg::PlottingPackage, initargs::Dict, kw, commandIndex::I end end - # auto-pick - if d[:axis] == :auto - d[:axis] = autopick_ignore_none_auto(supportedAxes(pkg), plotIndex) - end - if d[:linestyle] == :auto - d[:linestyle] = autopick_ignore_none_auto(supportedStyles(pkg), plotIndex) - end - if d[:marker] == :auto - d[:marker] = autopick_ignore_none_auto(supportedMarkers(pkg), plotIndex) - end + aliasesAndAutopick(d, :axis, _axesAliases, supportedAxes(pkg), plotIndex) + aliasesAndAutopick(d, :linestyle, _styleAliases, supportedStyles(pkg), plotIndex) + aliasesAndAutopick(d, :marker, _markerAliases, supportedMarkers(pkg), plotIndex) + + # # auto-pick + # if d[:axis] == :auto + # d[:axis] = autopick_ignore_none_auto(supportedAxes(pkg), plotIndex) + # end + # if d[:linestyle] == :auto + # d[:linestyle] = autopick_ignore_none_auto(supportedStyles(pkg), plotIndex) + # end + # if d[:marker] == :auto + # d[:marker] = autopick_ignore_none_auto(supportedMarkers(pkg), plotIndex) + # end # update color d[:color] = getSeriesRGBColor(d[:color], initargs, plotIndex) diff --git a/src/backends/qwt.jl b/src/backends/qwt.jl index 23f16854..bd1f608f 100644 --- a/src/backends/qwt.jl +++ b/src/backends/qwt.jl @@ -6,19 +6,27 @@ immutable QwtPackage <: PlottingPackage end export qwt! qwt!() = plotter!(:qwt) -supportedTypes(::QwtPackage) = [:none, :line, :step, :stepinverted, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar] +supportedTypes(::QwtPackage) = [:none, :path, :step, :stepinverted, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar] # ------------------------------- +const _qwtAliases = Dict( + :nbins => :heatmap_n, + :hexbin => :heatmap, + :path => :line, + ) + function adjustQwtKeywords(iscreating::Bool; kw...) d = Dict(kw) - if !iscreating - d[:heatmap_n] = d[:nbins] - end + replaceAliases!(d, _qwtAliases) + # if !iscreating + # d[:heatmap_n] = d[:nbins] + # end - if d[:linetype] == :hexbin - d[:linetype] = :heatmap - elseif d[:linetype] == :scatter + # if d[:linetype] == :hexbin + # d[:linetype] = :heatmap + # elseif d[:linetype] == :scatter + if d[:linetype] == :scatter d[:linetype] = :none if d[:marker] == :none d[:marker] = :ellipse diff --git a/src/plot.jl b/src/plot.jl index afbfd398..47e60014 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -91,7 +91,7 @@ When plotting multiple lines, you can give every line the same trait by using th function plot(args...; kw...) pkg = plotter() d = Dict(kw) - replaceAliases!(d) + replaceAliases!(d, _keyAliases) # # ensure we're passing in an RGB # if haskey(d, :background_color) @@ -125,7 +125,7 @@ end function plot!(plt::Plot, args...; kw...) d = Dict(kw) - replaceAliases!(d) + replaceAliases!(d, _keyAliases) # TODO: handle a "group by" mechanism. # will probably want to check for the :group kw param, and split into diff --git a/src/subplot.jl b/src/subplot.jl index 9ca179a3..ca9591c8 100644 --- a/src/subplot.jl +++ b/src/subplot.jl @@ -60,7 +60,7 @@ Create a series of plots: """ function subplot(args...; kw...) d = Dict(kw) - replaceAliases!(d) + replaceAliases!(d, _keyAliases) # figure out the layout if haskey(d, :layout) @@ -117,7 +117,7 @@ function subplot!(subplt::Subplot, args...; kw...) end d = Dict(kw) - replaceAliases!(d) + replaceAliases!(d, _keyAliases) for k in keys(_plotDefaults) delete!(d, k) end diff --git a/src/utils.jl b/src/utils.jl index 08f7d74a..e7c65250 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -108,6 +108,16 @@ makevec(v::AVec) = v makevec{T}(v::T) = T[v] +function replaceAliases!(d::Dict, aliases::Dict) + for (k,v) in d + if haskey(aliases, k) + d[aliases[k]] = v + delete!(d, k) + end + end +end + + function regressionXY(x, y) # regress β, α = [x ones(length(x))] \ y