working on colors/args and examples

This commit is contained in:
Thomas Breloff 2015-10-07 16:04:30 -04:00
parent 1c70346a61
commit 91da261128
7 changed files with 1181 additions and 93 deletions

View File

@ -17,26 +17,38 @@ type PlotExample
end
function fakedata(sz...)
y = Array(Float64, sz...)
for r in 2:size(y,1)
y[r,:] = 0.9 * y[r-1,:] + randn(size(y,2))'
end
y
end
# the examples we'll run for each
const examples = PlotExample[
PlotExample("Lines",
"A simple line plot of the columns.",
[:(plot(cumsum(randn(50,10),1), w=3))]),
[:(plot(fakedata(50,5), w=3))]),
PlotExample("Functions",
"Plot multiple functions. You can also put the function first, or use the form `plot(f, xmin, xmax)` where f is a Function or AbstractVector{Function}.",
[:(plot(0:0.01:4π, [sin,cos]))]),
PlotExample("",
"Or make a parametric plot (i.e. plot: (fx(u), fy(u))) with plot(fx, fy, umin, umax).",
[:(plot(sin, x->sin(2x), 0, 2π, leg=false, fill=(0,:orange)))]),
PlotExample("Colors",
"Access predefined palettes (or build your own with the `colorscheme` method). Line/marker colors are auto-generated from the plot's palette, unless overridden. Set the `z` argument to turn on series gradients.",
[:(y = rand(100)), :(plot(0:10:100,rand(11,4),lab="lines",w=3, palette=:grays, fill=(0.5,:auto))), :(scatter!(y, z=abs(y-.5), m=(10,:heat), lab="grad"))]),
PlotExample("Global",
"Change the guides/background/limits/ticks. Convenience args `xaxis` and `yaxis` allow you to pass a tuple or value which will be mapped to the relevant args automatically. The `xaxis` below will be replaced with `xlabel` and `xlims` args automatically during the preprocessing step. You can also use shorthand functions: `title!`, `xaxis!`, `yaxis!`, `xlabel!`, `ylabel!`, `xlims!`, `ylims!`, `xticks!`, `yticks!`",
[:(plot(rand(20,3), title="TITLE", xaxis=("XLABEL",(-5,30),0:2:20,:flip), yaxis=("YLABEL",:log10), background_color = RGB(0.2,0.2,0.2), leg=false))]),
PlotExample("Two-axis",
"Use the `axis` arguments.\n\nNote: Currently only supported with Qwt and PyPlot",
[:(plot(Vector[randn(100), randn(100)*100]; axis = [:l :r], ylabel="LEFT", yrightlabel="RIGHT"))]),
PlotExample("Vectors w/ pluralized args",
"Plot multiple series with different numbers of points. Mix arguments that apply to all series (marker/markersize) with arguments unique to each series (colors).",
[:(plot(Vector[rand(10), rand(20)]; marker=:ellipse, markersize=8, c=(:red, :blue)))]),
PlotExample("Arguments",
"Plot multiple series with different numbers of points. Mix arguments that apply to all series (marker/markersize) with arguments unique to each series (colors). Special arguments `line`, `marker`, and `fill` will automatically figure out what arguments to set (for example, we are setting the `linestyle`, `linewidth`, and `color` arguments with `line`.) Note that we pass a matrix of colors, and this applies the colors to each series.",
[:(plot(Vector[rand(10), rand(20)]; marker=(:ellipse,8), line=(:dot,3,[:black :orange])))]),
PlotExample("Build plot in pieces",
"Start with a base plot...",
[:(plot(rand(100)/3, reg=true, fill=(0,:green)))]),

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,6 @@ export
current,
default,
colorscheme,
scatter,
scatter!,
@ -59,6 +58,16 @@ export
dataframes,
OHLC,
colorscheme,
ColorScheme,
ColorGradient,
ColorVector,
ColorWrapper,
ColorFunction,
ColorZFunction,
getColor,
getColorZ,
supportedArgs,
supportedAxes,
supportedTypes,

View File

@ -149,6 +149,7 @@ _plotDefaults[:layout] = nothing
_plotDefaults[:n] = -1
_plotDefaults[:nr] = -1
_plotDefaults[:nc] = -1
_plotDefaults[:color_palette] = :auto
@ -257,6 +258,7 @@ const _keyAliases = Dict(
:wtitle => :windowtitle,
:gui => :show,
:display => :show,
:palette => :color_palette,
)
# add all pluralized forms to the _keyAliases dict
@ -314,9 +316,16 @@ trueOrAllTrue(f::Function, x) = f(x)
function handleColors!(d::Dict, arg, csym::Symbol)
try
c = colorscheme(arg)
d[csym] = c
# @show arg typeof(arg)
if arg == :auto
d[csym] = :auto
else
c = colorscheme(arg)
d[csym] = c
end
return true
# catch err
# dump(err)
end
false
end

View File

@ -14,8 +14,9 @@ supportedArgs(::GadflyPackage) = [
# :axis,
:background_color,
:color,
:color_palette,
:fillrange,
# :fillcolor,
:fillcolor,
# :foreground_color,
:group,
# :heatmap_c,
@ -157,15 +158,20 @@ function addGadflySeries!(gplt, d::Dict, initargs::Dict)
line_width = d[:linewidth] * (d[:linetype] in (:none, :ohlc, :scatter) ? 0 : 1) * Gadfly.px # 0 linewidth when we don't show a line
# line_color = isa(d[:color], AbstractVector) ? colorant"black" : d[:color]
line_color = getColor(d[:color])
fillcolor = getColor(d[:fillcolor])
@show fillcolor
# fg = initargs[:foreground_color]
theme = Gadfly.Theme(; default_color = line_color,
line_width = line_width,
default_point_size = 0.5 * d[:markersize] * Gadfly.px,
default_point_size = d[:markersize] * Gadfly.px,
# grid_color = fg,
# minor_label_color = fg,
# major_label_color = fg,
# key_title_color = fg,
# key_label_color = fg,
lowlight_color = x->RGB(fillcolor),
lowlight_opacity = alpha(fillcolor),
bar_highlight = RGB(line_color),
extra_theme_args...)
push!(gfargs, theme)
@ -222,7 +228,7 @@ function addGadflySeries!(gplt, d::Dict, initargs::Dict)
if !isa(d[:markercolor], ColorGradient)
d[:markercolor] = colorscheme(:bluesreds)
end
push!(gplt.scales, Gadfly.Scale.ContinuousColorScale(p -> getColorZ(d[:markercolor], p))) # minz + p * (maxz - minz))))
push!(gplt.scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p)))) # minz + p * (maxz - minz))))
# nothing special...
else

View File

@ -70,11 +70,11 @@ const _masterColorList = [
function darken(c, v=0.1)
rgb = RGB(c)
r = max(0, min(rgb.r - v, 1))
g = max(0, min(rgb.g - v, 1))
b = max(0, min(rgb.b - v, 1))
RGB(r,g,b)
rgba = RGBA(c)
r = max(0, min(rgba.r - v, 1))
g = max(0, min(rgba.g - v, 1))
b = max(0, min(rgba.b - v, 1))
RGBA(r,g,b,rgba.alpha)
end
function lighten(c, v=0.3)
darken(c, -v)
@ -103,7 +103,7 @@ colorscheme(cs::AVec, vs::AVec) = ColorGradient(cs, vs)
colorscheme{T<:Colorant}(cs::AVec{T}) = ColorGradient(cs)
colorscheme(f::Function) = ColorFunction(f)
colorscheme(v::AVec) = ColorVector(v)
colorscheme(m::AMat) = Any[ColorVector(m[:,i]) for i in 1:size(m,2)]
colorscheme(m::AMat) = size(m,1) == 1 ? map(colorscheme, m) : [colorscheme(m[:,i]) for i in 1:size(m,2)]'
colorscheme(c::Colorant) = ColorWrapper(c)
const _rainbowColors = [colorant"blue", colorant"purple", colorant"green", colorant"orange", colorant"red"]
@ -147,7 +147,7 @@ function ColorGradient{T<:Real}(s::Symbol, vals::AVec{T} = 0:1)
ColorGradient(cs, vals)
end
getColor(gradient::ColorGradient, idx::Int) = gradient.cs[mod1(idx, length(gradient.cs))]
getColor(gradient::ColorGradient, idx::Int) = gradient.colors[mod1(idx, length(gradient.colors))]
function getColorZ(gradient::ColorGradient, z::Real)
cs = gradient.colors
@ -173,22 +173,23 @@ end
getColorVector(gradient::ColorGradient) = gradient.colors
function interpolate_lab(c1::Colorant, c2::Colorant, w::Real)
lab1 = Lab(c1)
lab2 = Lab(c2)
l = interpolate(lab1.l, lab2.l, w)
a = interpolate(lab1.a, lab2.a, w)
b = interpolate(lab1.b, lab2.b, w)
RGB(Lab(l, a, b))
end
# function interpolate_lab(c1::Colorant, c2::Colorant, w::Real)
# lab1 = Lab(c1)
# lab2 = Lab(c2)
# l = interpolate(lab1.l, lab2.l, w)
# a = interpolate(lab1.a, lab2.a, w)
# b = interpolate(lab1.b, lab2.b, w)
# RGB(Lab(l, a, b))
# end
function interpolate_rgb(c1::Colorant, c2::Colorant, w::Real)
rgb1 = RGB(c1)
rgb2 = RGB(c2)
rgb1 = RGBA(c1)
rgb2 = RGBA(c2)
r = interpolate(rgb1.r, rgb2.r, w)
g = interpolate(rgb1.g, rgb2.g, w)
b = interpolate(rgb1.b, rgb2.b, w)
RGB(r, g, b)
a = interpolate(rgb1.alpha, rgb2.alpha, w)
RGBA(r, g, b, a)
end
@ -233,6 +234,8 @@ immutable ColorWrapper{C<:Colorant} <: ColorScheme
c::C
end
ColorWrapper(s::Symbol) = ColorWrapper(parse(Colorant, s))
getColor(scheme::ColorWrapper, idx::Int) = scheme.c
getColorZ(scheme::ColorWrapper, z::Real) = scheme.c
@ -297,10 +300,15 @@ function getPaletteUsingColorDiffFromBackground(bgcolor::Colorant, numcolors::In
filter(c -> colordiff(c, bgcolor) >= mindiff, _allColors)
end
function getPaletteUsingRainbow(bgcolor::Colorant, numcolors::Int = _defaultNumColors)
grad = ColorGradient(_gradients[isdark(bgcolor) ? :lightrainbow : :darkrainbow])
function getPaletteUsingGradientSymbol(bgcolor::Colorant, numcolors::Int = _defaultNumColors; gradientsym::Symbol = :auto)
# @show gradientsym
if gradientsym == :auto
grad = ColorGradient(_gradients[isdark(bgcolor) ? :lightrainbow : :darkrainbow])
else
grad = ColorGradient(gradientsym)
end
zrng = getpctrange(numcolors)
RGB[getColorZ(grad, z) for z in zrng]
RGBA[getColorZ(grad, z) for z in zrng]
end
# ----------------------------------------------------------------------------------
@ -345,7 +353,7 @@ function handlePlotColors(::PlottingPackage, d::Dict)
# d[:color_palette] = getPaletteUsingDistinguishableColors(bgcolor)
# d[:color_palette] = getPaletteUsingFixedColorList(bgcolor)
# d[:color_palette] = getPaletteUsingColorDiffFromBackground(bgcolor)
d[:color_palette] = getPaletteUsingRainbow(bgcolor)
d[:color_palette] = getPaletteUsingGradientSymbol(bgcolor; gradientsym = get(d, :color_palette, :auto))
# set the foreground color (text, ticks, gridlines) to be white or black depending
# on how dark the background is.

View File

@ -117,7 +117,7 @@ function plot!(plt::Plot, args...; kw...)
warnOnUnsupportedScales(plt.backend, d)
dumpdict(d, "Updating plot items:")
dumpdict(d, "Updating plot items")
# add title, axis labels, ticks, etc
updatePlotItems(plt, d)