alpha kw for colors; pyplot fix, debugshow

This commit is contained in:
Thomas Breloff 2015-10-27 12:40:38 -04:00
parent 9ea0585d71
commit 20689af7dd
9 changed files with 50 additions and 29 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 132 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 132 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 132 B

View File

@ -345,7 +345,6 @@ trueOrAllTrue(f::Function, x) = f(x)
function handleColors!(d::Dict, arg, csym::Symbol) function handleColors!(d::Dict, arg, csym::Symbol)
try try
# @show arg typeof(arg)
if arg == :auto if arg == :auto
d[csym] = :auto d[csym] = :auto
else else
@ -353,8 +352,6 @@ function handleColors!(d::Dict, arg, csym::Symbol)
d[csym] = c d[csym] = c
end end
return true return true
# catch err
# dump(err)
end end
false false
end end
@ -372,7 +369,6 @@ function processAxisArg(d::Dict, axisletter::@compat(AbstractString), arg)
elseif arg in (:flip, :invert, :inverted) elseif arg in (:flip, :invert, :inverted)
d[symbol(axisletter * "flip")] = true d[symbol(axisletter * "flip")] = true
# end
elseif T <: @compat(AbstractString) elseif T <: @compat(AbstractString)
d[symbol(axisletter * "label")] = arg d[symbol(axisletter * "label")] = arg

View File

@ -144,7 +144,7 @@ function addGadflyMarker!(plt::Plot, d::Dict, initargs::Dict, geoms...)
if !isa(d[:markercolor], ColorGradient) if !isa(d[:markercolor], ColorGradient)
d[:markercolor] = colorscheme(:bluesreds) d[:markercolor] = colorscheme(:bluesreds)
end end
push!(getGadflyContext(plt).scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p)))) push!(getGadflyContext(plt).scales, Gadfly.Scale.ContinuousColorScale(p -> RGBA(getColorZ(d[:markercolor], p))))
end end
Gadfly.layer(gfargs...; x = d[:x], y = d[:y], kwargs...) Gadfly.layer(gfargs...; x = d[:x], y = d[:y], kwargs...)

View File

@ -87,6 +87,13 @@ convertColor(c::@compat(Union{AbstractString, Symbol})) = parse(Colorant, string
convertColor(c::Colorant) = c convertColor(c::Colorant) = c
convertColor(cvec::AbstractVector) = map(convertColor, cvec) convertColor(cvec::AbstractVector) = map(convertColor, cvec)
function convertColor(c, α::Real)
c = convertColor(c)
RGBA(c, α)
end
convertColor(cs::AVec, α::Real) = map(c -> convertColor(c, α), cs)
convertColor(c, α::Void) = convertColor(c)
# backup... try to convert # backup... try to convert
getColor(c) = convertColor(c) getColor(c) = convertColor(c)
@ -98,14 +105,14 @@ getColor(scheme::ColorScheme) = getColor(scheme, 1)
getColorVector(scheme::ColorScheme) = [getColor(scheme)] getColorVector(scheme::ColorScheme) = [getColor(scheme)]
colorscheme(scheme::ColorScheme) = scheme colorscheme(scheme::ColorScheme) = scheme
colorscheme(s::Symbol) = haskey(_gradients, s) ? ColorGradient(s) : ColorWrapper(convertColor(s)) colorscheme(s::Symbol; kw...) = haskey(_gradients, s) ? ColorGradient(s; kw...) : ColorWrapper(convertColor(s); kw...)
colorscheme{T<:Real}(s::Symbol, vals::AVec{T}) = ColorGradient(s, vals) colorscheme{T<:Real}(s::Symbol, vals::AVec{T}; kw...) = ColorGradient(s, vals; kw...)
colorscheme(cs::AVec, vs::AVec) = ColorGradient(cs, vs) colorscheme(cs::AVec, vs::AVec; kw...) = ColorGradient(cs, vs; kw...)
colorscheme{T<:Colorant}(cs::AVec{T}) = ColorGradient(cs) colorscheme{T<:Colorant}(cs::AVec{T}; kw...) = ColorGradient(cs; kw...)
colorscheme(f::Function) = ColorFunction(f) colorscheme(f::Function; kw...) = ColorFunction(f; kw...)
colorscheme(v::AVec) = ColorVector(v) colorscheme(v::AVec; kw...) = ColorVector(v; kw...)
colorscheme(m::AMat) = size(m,1) == 1 ? map(colorscheme, m) : [colorscheme(m[:,i]) for i in 1:size(m,2)]' colorscheme(m::AMat; kw...) = size(m,1) == 1 ? map(c->colorscheme(c; kw...), m) : [colorscheme(m[:,i]; kw...) for i in 1:size(m,2)]'
colorscheme(c::Colorant) = ColorWrapper(c) colorscheme(c::Colorant; kw...) = ColorWrapper(c; kw...)
# -------------------------------------------------------------- # --------------------------------------------------------------
@ -138,23 +145,29 @@ immutable ColorGradient <: ColorScheme
colors::Vector{Colorant} colors::Vector{Colorant}
values::Vector{Float64} values::Vector{Float64}
function ColorGradient{T<:Colorant,S<:Real}(cs::AVec{T}, vals::AVec{S} = 0:1) function ColorGradient{T<:Colorant,S<:Real}(cs::AVec{T}, vals::AVec{S} = 0:1; alpha = nothing)
length(cs) == length(vals) && return new(cs, collect(vals)) if length(cs) == length(vals)
return new(convertColor(cs,alpha), collect(vals))
end
# otherwise interpolate evenly between the minval and maxval # otherwise interpolate evenly between the minval and maxval
minval, maxval = minimum(vals), maximum(vals) minval, maxval = minimum(vals), maximum(vals)
vs = Float64[interpolate(minval, maxval, w) for w in linspace(0, 1, length(cs))] vs = Float64[interpolate(minval, maxval, w) for w in linspace(0, 1, length(cs))]
new(cs, vs) new(convertColor(cs,alpha), vs)
end end
end end
# create a gradient from a symbol (blues, reds, etc) and vector of boundary values # create a gradient from a symbol (blues, reds, etc) and vector of boundary values
function ColorGradient{T<:Real}(s::Symbol, vals::AVec{T} = 0:1) function ColorGradient{T<:Real}(s::Symbol, vals::AVec{T} = 0:1; kw...)
haskey(_gradients, s) || error("Invalid gradient symbol. Choose from: ", sort(collect(keys(_gradients)))) haskey(_gradients, s) || error("Invalid gradient symbol. Choose from: ", sort(collect(keys(_gradients))))
# if we passed in the right number of values, create the gradient directly # if we passed in the right number of values, create the gradient directly
cs = _gradients[s] cs = _gradients[s]
ColorGradient(cs, vals) ColorGradient(cs, vals; kw...)
end
function ColorGradient{T<:Real}(cs::AVec{Symbol}, vals::AVec{T} = 0:1; kw...)
ColorGradient(map(convertColor, cs), vals; kw...)
end end
getColor(gradient::ColorGradient, idx::Int) = gradient.colors[mod1(idx, length(gradient.colors))] getColor(gradient::ColorGradient, idx::Int) = gradient.colors[mod1(idx, length(gradient.colors))]
@ -225,7 +238,7 @@ getColorZ(scheme::ColorFunction, z::Real) = scheme.f(z)
"Wraps a vector of colors... may be vector of Symbol/String/Colorant" "Wraps a vector of colors... may be vector of Symbol/String/Colorant"
immutable ColorVector <: ColorScheme immutable ColorVector <: ColorScheme
v::Vector{Colorant} v::Vector{Colorant}
ColorVector(v::AVec) = new(convertColor(v)) ColorVector(v::AVec; alpha = nothing) = new(convertColor(v,alpha))
end end
getColor(scheme::ColorVector, idx::Int) = convertColor(scheme.v[mod1(idx, length(scheme.v))]) getColor(scheme::ColorVector, idx::Int) = convertColor(scheme.v[mod1(idx, length(scheme.v))])
@ -235,11 +248,12 @@ getColorVector(scheme::ColorVector) = scheme.v
# -------------------------------------------------------------- # --------------------------------------------------------------
"Wraps a single color" "Wraps a single color"
immutable ColorWrapper{C<:Colorant} <: ColorScheme immutable ColorWrapper <: ColorScheme
c::C c::RGBA
ColorWrapper(c::Colorant; alpha = nothing) = new(convertColor(c, alpha))
end end
ColorWrapper(s::Symbol) = ColorWrapper(parse(Colorant, s)) ColorWrapper(s::Symbol; alpha = nothing) = ColorWrapper(convertColor(parse(Colorant, s), alpha))
getColor(scheme::ColorWrapper, idx::Int) = scheme.c getColor(scheme::ColorWrapper, idx::Int) = scheme.c
getColorZ(scheme::ColorWrapper, z::Real) = scheme.c getColorZ(scheme::ColorWrapper, z::Real) = scheme.c
@ -290,19 +304,19 @@ const _lightness_lightbg = [60.0]
const _lch_c_const = [60] const _lch_c_const = [60]
function adjust_lch(color, l, c) function adjust_lch(color, l, c)
lch = convert(LCHab,color) lch = convert(LCHab, color)
convert(RGB, LCHab(l, c, lch.h)) convert(RGB, LCHab(l, c, lch.h))
end end
function lightness_from_background(bgcolor) function lightness_from_background(bgcolor)
bglight = convert(LCHab,bgcolor).l bglight = convert(LCHab, bgcolor).l
bglight < 50.0 ? _lightness_darkbg[1] : _lightness_lightbg[1] bglight < 50.0 ? _lightness_darkbg[1] : _lightness_lightbg[1]
end end
function gradient_from_list(cs) function gradient_from_list(cs)
zvalues = Plots.get_zvalues(length(cs)) zvalues = Plots.get_zvalues(length(cs))
indices = sortperm(zvalues) indices = sortperm(zvalues)
sorted_colors = map(RGB, cs[indices]) sorted_colors = map(RGBA, cs[indices])
sorted_zvalues = zvalues[indices] sorted_zvalues = zvalues[indices]
ColorGradient(sorted_colors, sorted_zvalues) ColorGradient(sorted_colors, sorted_zvalues)
end end

View File

@ -160,12 +160,19 @@ function backend()
@eval const pypath = PyPlot.pywrap(PyPlot.pyimport("matplotlib.path")) @eval const pypath = PyPlot.pywrap(PyPlot.pyimport("matplotlib.path"))
# @eval const pycolorbar = PyPlot.pywrap(PyPlot.pyimport("matplotlib.colorbar")) # @eval const pycolorbar = PyPlot.pywrap(PyPlot.pyimport("matplotlib.colorbar"))
if !isa(Base.Multimedia.displays[end], Base.REPL.REPLDisplay) if !isa(Base.Multimedia.displays[end], Base.REPL.REPLDisplay)
PyPlot.ioff() PyPlot.ioff() # stops wierd behavior of displaying incomplete graphs in IJulia
# # TODO: how the hell can I use PyQt4??
# "pyqt4"=>:qt_pyqt4 # "pyqt4"=>:qt_pyqt4
# PyPlot.backend[1] = "pyqt4" # PyPlot.backend[1] = "pyqt4"
# PyPlot.gui[1] = :qt_pyqt4 # PyPlot.gui[1] = :qt_pyqt4
# PyPlot.switch_backend("Qt4Agg") # PyPlot.switch_backend("Qt4Agg")
PyPlot.pygui(true)
# only turn on the gui if we want it
if PyPlot.gui != :none
PyPlot.pygui(true)
end
end end
catch err catch err
warn("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\").") warn("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\").")

View File

@ -118,6 +118,6 @@ end
"Sparsity plot... heatmap of non-zero values of a matrix" "Sparsity plot... heatmap of non-zero values of a matrix"
function spy{T<:Real}(y::AMat{T}; kw...) function spy{T<:Real}(y::AMat{T}; kw...)
I,J,V = findnz(y) I,J,V = findnz(y)
heatmap(J, I; leg=false, yflip=true, kw...) heatmap(J, I; leg=false, yflip=true, nbins=size(y), kw...)
end end

View File

@ -206,6 +206,9 @@ function debugplots(on = true)
_debugMode.on = on _debugMode.on = on
end end
debugshow(x) = show(x)
debugshow(x::AbstractArray) = print(summary(x))
function dumpdict(d::Dict, prefix = "") function dumpdict(d::Dict, prefix = "")
_debugMode.on || return _debugMode.on || return
println() println()
@ -214,7 +217,8 @@ function dumpdict(d::Dict, prefix = "")
end end
for k in sort(collect(keys(d))) for k in sort(collect(keys(d)))
@printf("%14s: ", k) @printf("%14s: ", k)
println(d[k]) debugshow(d[k])
println()
end end
println() println()
end end