From 646b88c5bedaec3e71d80e7cfec95c3e0c0f3ae2 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Tue, 22 Sep 2015 23:30:20 -0400 Subject: [PATCH] working on ticks --- src/args.jl | 6 +- src/backends/gadfly.jl | 29 ++++- src/plot.jl | 234 ----------------------------------------- src/utils.jl | 4 + 4 files changed, 36 insertions(+), 237 deletions(-) diff --git a/src/args.jl b/src/args.jl index 64497612..909afbb9 100644 --- a/src/args.jl +++ b/src/args.jl @@ -116,8 +116,8 @@ _plotDefaults[:yrightlabel] = "" _plotDefaults[:legend] = true _plotDefaults[:background_color] = colorant"white" _plotDefaults[:foreground_color] = :auto -_plotDefaults[:xticks] = true -_plotDefaults[:yticks] = true +_plotDefaults[:xticks] = :auto +_plotDefaults[:yticks] = :auto _plotDefaults[:size] = (800,600) _plotDefaults[:windowtitle] = "Plots.jl" _plotDefaults[:show] = false @@ -198,6 +198,8 @@ const _keyAliases = Dict( :fgcolor => :foreground_color, :fg_color => :foreground_color, :foreground => :foreground_color, + :xlim => :xticks, + :ylim => :yticks, :windowsize => :size, :wsize => :size, :wtitle => :windowtitle, diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index 41577ee2..c1efffce 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -184,7 +184,6 @@ function addGadflySeries!(gplt, d::Dict, initargs::Dict) push!(gplt.guides[1].colors, d[:marker] == :none ? d[:color] : d[:markercolor]) end - # for histograms, set x=y x = d[d[:linetype] == :hist ? :y : :x] @@ -201,6 +200,32 @@ function addGadflySeries!(gplt, d::Dict, initargs::Dict) nothing end +function addTicksGuideOrScale(gplt, ticks, isx::Bool) + ticks == :auto && return + ttype = ticksType(ticks) + if ttype == :limits + minvalue = min(ticks...) + maxvalue = max(ticks...) + scale = (isx ? Gadfly.Scale.x_continuous : Gadfly.Scale.y_continuous)(minvalue=minvalue, maxvalue=maxvalue) + push!(gplt.scales, scale) + elseif ttype == :ticks + if isx + ticks = map(Compose.x_measure, sort(collect(ticks))) + push!(gplt.statistics, Gadfly.Stat.xticks(ticks=ticks)) + else + ticks = map(Compose.y_measure, sort(collect(ticks))) + push!(gplt.statistics, Gadfly.Stat.yticks(ticks=ticks)) + end + # ticks = map(Composesort(ticks) + # # guide = (isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks)(ticks=ticks) + # # push!(gplt.guides, guide) + # stat = (isx ? Gadfly.Stat.xticks : Gadfly.Stat.yticks)(ticks=ticks) + # push!(gplt.statistics, stat) + else + error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks) + end +end + # --------------------------------------------------------------------------- # create a blank Gadfly.Plot object @@ -233,6 +258,8 @@ function updateGadflyGuides(gplt, d::Dict) haskey(d, :title) && findGuideAndSet(gplt, Gadfly.Guide.title, d[:title]) haskey(d, :xlabel) && findGuideAndSet(gplt, Gadfly.Guide.xlabel, d[:xlabel]) haskey(d, :ylabel) && findGuideAndSet(gplt, Gadfly.Guide.ylabel, d[:ylabel]) + haskey(d, :xticks) && addTicksGuideOrScale(gplt, d[:xticks], true) + haskey(d, :yticks) && addTicksGuideOrScale(gplt, d[:yticks], true) end function updatePlotItems(plt::Plot{GadflyPackage}, d::Dict) diff --git a/src/plot.jl b/src/plot.jl index dabd32ce..d6a8e613 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -259,237 +259,3 @@ end # -------------------------------------------------------------------- - - -# doc"Build a vector of dictionaries which hold the keyword arguments for a call to plot!" - -# # no args... 1 series -# function createKWargsList(plt::PlottingObject; kw...) -# d = Dict(kw) -# @assert haskey(d, :y) -# if !haskey(d, :x) -# d[:x] = 1:length(d[:y]) -# end -# [getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), d, 1, plt.n + 1)] -# end - - -# # ---------------------------------------------------------------------------- -# # Arrays of numbers -# # ---------------------------------------------------------------------------- - - -# # create one series where y is vectors of numbers -# function createKWargsList{T<:Real}(plt::PlottingObject, y::AVec{T}; kw...) -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), kw, 1, plt.n + 1) -# d[:x] = 1:length(y) -# d[:y] = y -# [d] -# end - -# # create one series where x/y are vectors of numbers -# function createKWargsList{T<:Real,S<:Real}(plt::PlottingObject, x::AVec{T}, y::AVec{S}; kw...) -# @assert length(x) == length(y) -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), kw, 1, plt.n + 1) -# d[:x] = x -# d[:y] = y -# [d] -# end - -# # create m series, 1 for each column of y -# function createKWargsList{T<:Real}(plt::PlottingObject, y::AMat{T}; kw...) -# n,m = size(y) -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = 1:n -# d[:y] = y[:,i] -# push!(ret, d) -# end -# ret -# end - -# # create m series, 1 for each column of y -# function createKWargsList{T<:Real,S<:Real}(plt::PlottingObject, x::AVec{T}, y::AMat{S}; kw...) -# n,m = size(y) -# @assert length(x) == n -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x -# d[:y] = y[:,i] -# push!(ret, d) -# end -# ret -# end - -# # create m series, 1 for each column of y -# function createKWargsList{T<:Real,S<:Real}(plt::PlottingObject, x::AMat{T}, y::AMat{S}; kw...) -# @assert size(x) == size(y) -# n,m = size(y) -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x[:,i] -# d[:y] = y[:,i] -# push!(ret, d) -# end -# ret -# end - -# # ---------------------------------------------------------------------------- -# # Functions -# # ---------------------------------------------------------------------------- - - -# # create 1 series, y = f(x), x ∈ [xmin, xmax] -# function createKWargsList(plt::PlottingObject, f::Function, xmin::Real, xmax::Real; kw...) -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), kw, 1, plt.n + 1) -# width = plt.initargs[:size][1] -# d[:x] = collect(linspace(xmin, xmax, width)) # we don't need more than the width -# d[:y] = map(f, d[:x]) -# [d] -# end - -# # create m series, yᵢ = fᵢ(x), x ∈ [xmin, xmax] -# function createKWargsList(plt::PlottingObject, fs::Vector{Function}, xmin::Real, xmax::Real; kw...) -# m = length(fs) -# ret = [] -# width = plt.initargs[:size][1] -# x = collect(linspace(xmin, xmax, width)) # we don't need more than the width -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x -# d[:y] = map(fs[i], x) -# push!(ret, d) -# end -# ret -# end - -# # create 1 series, x = fx(u), y = fy(u); u ∈ [umin, umax] -# function createKWargsList(plt::PlottingObject, fx::Function, fy::Function, umin::Real, umax::Real; kw...) -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), kw, 1, plt.n + 1) -# width = plt.initargs[:size][1] -# u = collect(linspace(umin, umax, width)) # we don't need more than the width -# d[:x] = map(fx, u) -# d[:y] = map(fy, u) -# [d] -# end - -# # create 1 series, y = f(x) -# function createKWargsList{T<:Real}(plt::PlottingObject, x::AVec{T}, f::Function; kw...) -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), kw, 1, plt.n + 1) -# d[:x] = x -# d[:y] = map(f, x) -# [d] -# end -# createKWargsList{T<:Real}(plt::PlottingObject, f::Function, x::AVec{T}; kw...) = createKWargsList(plt, x, f; kw...) - -# # create m series, y = f(x), 1 for each column of x -# function createKWargsList{T<:Real}(plt::PlottingObject, x::AMat{T}, f::Function; kw...) -# n,m = size(x) -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x[:,i] -# d[:y] = map(f, d[:x]) -# push!(ret, d) -# end -# ret -# end -# createKWargsList{T<:Real}(plt::PlottingObject, f::Function, x::AMat{T}; kw...) = createKWargsList(plt, x, f; kw...) - - -# # ---------------------------------------------------------------------------- -# # Other combinations... lists of vectors, etc -# # ---------------------------------------------------------------------------- - - -# # create m series, 1 for each item in y (assumes vectors of something other than numbers... functions? vectors?) -# function createKWargsList(plt::PlottingObject, y::AVec; kw...) -# m = length(y) -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = 1:length(y[i]) -# d[:y] = y[i] -# push!(ret, d) -# end -# ret -# end - -# function getyvec(x::AVec, y::AVec) -# @assert length(x) == length(y) -# y -# end -# getyvec(x::AVec, f::Function) = map(f, x) -# getyvec(x, y) = error("Couldn't create yvec from types: x ($(typeof(x))), y ($(typeof(y)))") - -# # same, but given an x to use for all series -# function createKWargsList{T<:Real}(plt::PlottingObject, x::AVec{T}, y::AVec; kw...) -# m = length(y) -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x -# d[:y] = getyvec(x, y[i]) -# push!(ret, d) -# end -# ret -# end - -# # x is vec of vec, but y is a matrix -# function createKWargsList{T<:Real}(plt::PlottingObject, x::AVec, y::AMat{T}; kw...) -# n,m = size(y) -# @assert length(x) == m -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x[i] -# d[:y] = getyvec(x[i], y[:,i]) -# push!(ret, d) -# end -# ret -# end - -# # same, but m series of (x[i],y[i]) -# function createKWargsList(plt::PlottingObject, x::AVec, y::AVec; kw...) -# @assert length(x) == length(y) -# m = length(y) -# ret = [] -# for i in 1:m -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = x[i] -# d[:y] = getyvec(x[i], y[i]) -# push!(ret, d) -# end -# ret -# end - -# # n empty series -# function createKWargsList(plt::PlottingObject, n::Integer; kw...) -# ret = [] -# for i in 1:n -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+i), kw, i, plt.n + i) -# d[:x] = zeros(0) -# d[:y] = zeros(0) -# push!(ret, d) -# end -# ret -# end - -# # vector of tuples... to be used for things like OHLC -# createKWargsList{T<:Tuple}(plt::PlottingObject, y::AVec{T}; kw...) = createKWargsList(plt, 1:length(y), y; kw...) - -# function createKWargsList{S<:Real, T<:Tuple}(plt::PlottingObject, x::AVec{S}, y::AVec{T}; kw...) -# d = getSeriesArgs(plt.plotter, getinitargs(plt, plt.n+1), kw, 1, plt.n + 1) -# d[:x] = x -# d[:y] = y -# [d] -# end - - -# TODO: handle DataFrames (might have NAs!) - -# ------------------------- - diff --git a/src/utils.jl b/src/utils.jl index 4a5d4cc7..2f7e620b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -128,6 +128,10 @@ function regressionXY(x, y) regx, regy end +ticksType{T<:Real,S<:Real}(ticks::Tuple{T,S}) = :limits +ticksType{T<:Real}(ticks::AVec{T}) = :ticks +ticksType(ticks) = :invalid + # Some conversion functions # note: I borrowed these conversion constants from Compose.jl's Measure