proper handling of colors

This commit is contained in:
Thomas Breloff 2015-09-09 00:06:15 -04:00
parent 0dec779cf4
commit fe87e985ec
5 changed files with 52 additions and 18 deletions

View File

@ -39,6 +39,7 @@ const IMG_DIR = "$(ENV["HOME"])/.julia/v0.4/Plots/img/"
type Plot type Plot
o # the underlying object o # the underlying object
plotter::PlottingPackage plotter::PlottingPackage
n::Int # number of series
end end

View File

@ -54,7 +54,7 @@ makeplural(s::Symbol) = Symbol(string(s,"s"))
autocolor(idx::Integer) = COLORS[mod1(idx,NUMCOLORS)] autocolor(idx::Integer) = COLORS[mod1(idx,NUMCOLORS)]
function getPlotKeywordArgs(kw, i::Int) function getPlotKeywordArgs(kw, i::Int, plt = nothing)
d = Dict(kw) d = Dict(kw)
outd = Dict() outd = Dict()
@ -69,6 +69,16 @@ function getPlotKeywordArgs(kw, i::Int)
end end
end end
# auto assign a color
if plt != nothing
if outd[:color] == :auto
outd[:color] = autocolor(plt.n)
end
if outd[:markercolor] == :auto
outd[:markercolor] = outd[:color]
end
end
outd outd
end end

View File

@ -11,7 +11,7 @@ function plot(pkg::GadflyPackage; kw...)
plt.mapping = Dict() plt.mapping = Dict()
plt.data_source = DataFrames.DataFrame() plt.data_source = DataFrames.DataFrame()
plt.layers = plt.layers[1:0] plt.layers = plt.layers[1:0]
Plot(plt, pkg) Plot(plt, pkg, 0)
end end
@ -44,14 +44,20 @@ function plot!(::GadflyPackage, plt::Plot; kw...)
# label # label
# color # color
if d[:color] == :auto c = d[:color]
color = convert(RGB{Float32}, autocolor(length(plt.o.layers)+1)) if isa(c, Symbol)
c = string(c)
end end
if isa(c, String)
c = parse(Colorant, c)
end
@assert isa(c, RGB)
push!(gfargs, Gadfly.Theme(default_color=c))
# legend # legend
# guides (x/y labels, title, background, ticks) # guides (x/y labels, title, background, ticks)
append!(plt.o.layers, Gadfly.layer(unique(gfargs)...; x = d[:x], y = d[:y], color = [color])) append!(plt.o.layers, Gadfly.layer(unique(gfargs)...; x = d[:x], y = d[:y]))
plt plt
end end

View File

@ -126,6 +126,12 @@ end
# this adds to a specific plot... most plot commands will flow through here # this adds to a specific plot... most plot commands will flow through here
function plot!(plt::Plot, args...; kw...) function plot!(plt::Plot, args...; kw...)
# increment n if we're going directly to the package's plot method
if length(args) == 0
plt.n += 1
end
plot!(plt.plotter, plt, args...; kw...) plot!(plt.plotter, plt, args...; kw...)
currentPlot!(plt) currentPlot!(plt)
@ -148,18 +154,21 @@ end
# These methods are various ways to add to an existing plot # These methods are various ways to add to an existing plot
function plot!(pkg::PlottingPackage, plt::Plot, y::AVec; kw...) function plot!(pkg::PlottingPackage, plt::Plot, y::AVec; kw...)
plot!(pkg, plt; x = 1:length(y), y = y, getPlotKeywordArgs(kw, 1)...) plt.n += 1
plot!(pkg, plt; x = 1:length(y), y = y, getPlotKeywordArgs(kw, 1, plt)...)
end end
function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AVec; kw...) # one line (will assert length(x) == length(y)) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AVec; kw...) # one line (will assert length(x) == length(y))
@assert length(x) == length(y) @assert length(x) == length(y)
plot!(pkg, plt; x=x, y=y, getPlotKeywordArgs(kw, 1)...) plt.n += 1
plot!(pkg, plt; x=x, y=y, getPlotKeywordArgs(kw, 1, plt)...)
end end
function plot!(pkg::PlottingPackage, plt::Plot, y::AMat; kw...) # multiple lines (one per column of x), all sharing x = 1:size(y,1) function plot!(pkg::PlottingPackage, plt::Plot, y::AMat; kw...) # multiple lines (one per column of x), all sharing x = 1:size(y,1)
n,m = size(y) n,m = size(y)
for i in 1:m for i in 1:m
plot!(pkg, plt; x = 1:n, y = y[:,i], getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = 1:n, y = y[:,i], getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
@ -168,7 +177,8 @@ function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AMat; kw...)
n,m = size(y) n,m = size(y)
for i in 1:m for i in 1:m
@assert length(x) == n @assert length(x) == n
plot!(pkg, plt; x = x, y = y[:,i], getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = x, y = y[:,i], getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
@ -176,33 +186,37 @@ end
function plot!(pkg::PlottingPackage, plt::Plot, x::AMat, y::AMat; kw...) # multiple lines (one per column of x/y... will assert size(x) == size(y)) function plot!(pkg::PlottingPackage, 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) @assert size(x) == size(y)
for i in 1:size(x,2) for i in 1:size(x,2)
plot!(pkg, plt; x = x[:,i], y = y[:,i], getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = x[:,i], y = y[:,i], getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, f::Function; kw...) # one line, y = f(x) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, f::Function; kw...) # one line, y = f(x)
plot!(pkg, plt; x = x, y = map(f,x), getPlotKeywordArgs(kw, 1)...) plot!(pkg, plt; x = x, y = map(f,x), getPlotKeywordArgs(kw, 1, plt)...)
end end
function plot!(pkg::PlottingPackage, plt::Plot, x::AMat, f::Function; kw...) # multiple lines, yᵢⱼ = f(xᵢⱼ) function plot!(pkg::PlottingPackage, plt::Plot, x::AMat, f::Function; kw...) # multiple lines, yᵢⱼ = f(xᵢⱼ)
for i in 1:size(x,2) for i in 1:size(x,2)
xi = x[:,i] xi = x[:,i]
plot!(pkg, plt; x = xi, y = map(f, xi), getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = xi, y = map(f, xi), getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, fs::AVec{Function}; kw...) # multiple lines, yᵢⱼ = fⱼ(xᵢ)
for i in 1:length(fs) for i in 1:length(fs)
plot!(pkg, plt; x = x, y = map(fs[i], x), getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = x, y = map(fs[i], x), getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
function plot!(pkg::PlottingPackage, plt::Plot, y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i]) function plot!(pkg::PlottingPackage, plt::Plot, y::AVec{AVec}; kw...) # multiple lines, each with x = 1:length(y[i])
for i in 1:length(y) for i in 1:length(y)
plot!(pkg, plt; x = 1:length(y[i]), y = y[i], getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = 1:length(y[i]), y = y[i], getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
@ -210,7 +224,8 @@ end
function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i]) function plot!(pkg::PlottingPackage, plt::Plot, x::AVec, y::AVec{AVec}; kw...) # multiple lines, will assert length(x) == length(y[i])
for i in 1:length(y) for i in 1:length(y)
@assert length(x) == length(y[i]) @assert length(x) == length(y[i])
plot!(pkg, plt; x = x, y = y[i], getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = x, y = y[i], getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
@ -219,14 +234,16 @@ function plot!(pkg::PlottingPackage, plt::Plot, x::AVec{AVec}, y::AVec{AVec}; kw
@assert length(x) == length(y) @assert length(x) == length(y)
for i in 1:length(x) for i in 1:length(x)
@assert length(x[i]) == length(y[i]) @assert length(x[i]) == length(y[i])
plot!(pkg, plt; x = x[i], y = y[i], getPlotKeywordArgs(kw, i)...) plt.n += 1
plot!(pkg, plt; x = x[i], y = y[i], getPlotKeywordArgs(kw, i, plt)...)
end end
plt plt
end end
function plot!(pkg::PlottingPackage, plt::Plot, n::Integer; kw...) # n lines, all empty (for updating plots) function plot!(pkg::PlottingPackage, plt::Plot, n::Integer; kw...) # n lines, all empty (for updating plots)
for i in 1:n for i in 1:n
plot(pkg, plt, x = zeros(0), y = zeros(0), getPlotKeywordArgs(kw, i)...) plt.n += 1
plot(pkg, plt, x = zeros(0), y = zeros(0), getPlotKeywordArgs(kw, i, plt)...)
end end
end end

View File

@ -3,7 +3,7 @@
immutable QwtPackage <: PlottingPackage end immutable QwtPackage <: PlottingPackage end
plot(pkg::QwtPackage; kw...) = Plot(Qwt.plot(zeros(0,0); kw...), pkg) plot(pkg::QwtPackage; kw...) = Plot(Qwt.plot(zeros(0,0); kw...), pkg, 0)
function plot!(::QwtPackage, plt::Plot; kw...) function plot!(::QwtPackage, plt::Plot; kw...)
Qwt.oplot(plt.o; kw...) Qwt.oplot(plt.o; kw...)