working on colors/args and examples
This commit is contained in:
parent
1c70346a61
commit
91da261128
@ -17,26 +17,38 @@ type PlotExample
|
|||||||
end
|
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
|
# the examples we'll run for each
|
||||||
const examples = PlotExample[
|
const examples = PlotExample[
|
||||||
PlotExample("Lines",
|
PlotExample("Lines",
|
||||||
"A simple line plot of the columns.",
|
"A simple line plot of the columns.",
|
||||||
[:(plot(cumsum(randn(50,10),1), w=3))]),
|
[:(plot(fakedata(50,5), w=3))]),
|
||||||
PlotExample("Functions",
|
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 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]))]),
|
[:(plot(0:0.01:4π, [sin,cos]))]),
|
||||||
PlotExample("",
|
PlotExample("",
|
||||||
"Or make a parametric plot (i.e. plot: (fx(u), fy(u))) with plot(fx, fy, umin, umax).",
|
"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)))]),
|
[:(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",
|
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!`",
|
"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))]),
|
[:(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",
|
PlotExample("Two-axis",
|
||||||
"Use the `axis` arguments.\n\nNote: Currently only supported with Qwt and PyPlot",
|
"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"))]),
|
[:(plot(Vector[randn(100), randn(100)*100]; axis = [:l :r], ylabel="LEFT", yrightlabel="RIGHT"))]),
|
||||||
PlotExample("Vectors w/ pluralized args",
|
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).",
|
"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, markersize=8, c=(:red, :blue)))]),
|
[:(plot(Vector[rand(10), rand(20)]; marker=(:ellipse,8), line=(:dot,3,[:black :orange])))]),
|
||||||
PlotExample("Build plot in pieces",
|
PlotExample("Build plot in pieces",
|
||||||
"Start with a base plot...",
|
"Start with a base plot...",
|
||||||
[:(plot(rand(100)/3, reg=true, fill=(0,:green)))]),
|
[:(plot(rand(100)/3, reg=true, fill=(0,:green)))]),
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
11
src/Plots.jl
11
src/Plots.jl
@ -15,7 +15,6 @@ export
|
|||||||
|
|
||||||
current,
|
current,
|
||||||
default,
|
default,
|
||||||
colorscheme,
|
|
||||||
|
|
||||||
scatter,
|
scatter,
|
||||||
scatter!,
|
scatter!,
|
||||||
@ -59,6 +58,16 @@ export
|
|||||||
dataframes,
|
dataframes,
|
||||||
OHLC,
|
OHLC,
|
||||||
|
|
||||||
|
colorscheme,
|
||||||
|
ColorScheme,
|
||||||
|
ColorGradient,
|
||||||
|
ColorVector,
|
||||||
|
ColorWrapper,
|
||||||
|
ColorFunction,
|
||||||
|
ColorZFunction,
|
||||||
|
getColor,
|
||||||
|
getColorZ,
|
||||||
|
|
||||||
supportedArgs,
|
supportedArgs,
|
||||||
supportedAxes,
|
supportedAxes,
|
||||||
supportedTypes,
|
supportedTypes,
|
||||||
|
|||||||
13
src/args.jl
13
src/args.jl
@ -149,6 +149,7 @@ _plotDefaults[:layout] = nothing
|
|||||||
_plotDefaults[:n] = -1
|
_plotDefaults[:n] = -1
|
||||||
_plotDefaults[:nr] = -1
|
_plotDefaults[:nr] = -1
|
||||||
_plotDefaults[:nc] = -1
|
_plotDefaults[:nc] = -1
|
||||||
|
_plotDefaults[:color_palette] = :auto
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -257,6 +258,7 @@ const _keyAliases = Dict(
|
|||||||
:wtitle => :windowtitle,
|
:wtitle => :windowtitle,
|
||||||
:gui => :show,
|
:gui => :show,
|
||||||
:display => :show,
|
:display => :show,
|
||||||
|
:palette => :color_palette,
|
||||||
)
|
)
|
||||||
|
|
||||||
# add all pluralized forms to the _keyAliases dict
|
# 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)
|
function handleColors!(d::Dict, arg, csym::Symbol)
|
||||||
try
|
try
|
||||||
c = colorscheme(arg)
|
# @show arg typeof(arg)
|
||||||
d[csym] = c
|
if arg == :auto
|
||||||
|
d[csym] = :auto
|
||||||
|
else
|
||||||
|
c = colorscheme(arg)
|
||||||
|
d[csym] = c
|
||||||
|
end
|
||||||
return true
|
return true
|
||||||
|
# catch err
|
||||||
|
# dump(err)
|
||||||
end
|
end
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,8 +14,9 @@ supportedArgs(::GadflyPackage) = [
|
|||||||
# :axis,
|
# :axis,
|
||||||
:background_color,
|
:background_color,
|
||||||
:color,
|
:color,
|
||||||
|
:color_palette,
|
||||||
:fillrange,
|
:fillrange,
|
||||||
# :fillcolor,
|
:fillcolor,
|
||||||
# :foreground_color,
|
# :foreground_color,
|
||||||
:group,
|
:group,
|
||||||
# :heatmap_c,
|
# :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_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 = isa(d[:color], AbstractVector) ? colorant"black" : d[:color]
|
||||||
line_color = getColor(d[:color])
|
line_color = getColor(d[:color])
|
||||||
|
fillcolor = getColor(d[:fillcolor])
|
||||||
|
@show fillcolor
|
||||||
# fg = initargs[:foreground_color]
|
# fg = initargs[:foreground_color]
|
||||||
theme = Gadfly.Theme(; default_color = line_color,
|
theme = Gadfly.Theme(; default_color = line_color,
|
||||||
line_width = line_width,
|
line_width = line_width,
|
||||||
default_point_size = 0.5 * d[:markersize] * Gadfly.px,
|
default_point_size = d[:markersize] * Gadfly.px,
|
||||||
# grid_color = fg,
|
# grid_color = fg,
|
||||||
# minor_label_color = fg,
|
# minor_label_color = fg,
|
||||||
# major_label_color = fg,
|
# major_label_color = fg,
|
||||||
# key_title_color = fg,
|
# key_title_color = fg,
|
||||||
# key_label_color = fg,
|
# key_label_color = fg,
|
||||||
|
lowlight_color = x->RGB(fillcolor),
|
||||||
|
lowlight_opacity = alpha(fillcolor),
|
||||||
|
bar_highlight = RGB(line_color),
|
||||||
extra_theme_args...)
|
extra_theme_args...)
|
||||||
push!(gfargs, theme)
|
push!(gfargs, theme)
|
||||||
|
|
||||||
@ -222,7 +228,7 @@ function addGadflySeries!(gplt, d::Dict, initargs::Dict)
|
|||||||
if !isa(d[:markercolor], ColorGradient)
|
if !isa(d[:markercolor], ColorGradient)
|
||||||
d[:markercolor] = colorscheme(:bluesreds)
|
d[:markercolor] = colorscheme(:bluesreds)
|
||||||
end
|
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...
|
# nothing special...
|
||||||
else
|
else
|
||||||
|
|||||||
@ -70,11 +70,11 @@ const _masterColorList = [
|
|||||||
|
|
||||||
|
|
||||||
function darken(c, v=0.1)
|
function darken(c, v=0.1)
|
||||||
rgb = RGB(c)
|
rgba = RGBA(c)
|
||||||
r = max(0, min(rgb.r - v, 1))
|
r = max(0, min(rgba.r - v, 1))
|
||||||
g = max(0, min(rgb.g - v, 1))
|
g = max(0, min(rgba.g - v, 1))
|
||||||
b = max(0, min(rgb.b - v, 1))
|
b = max(0, min(rgba.b - v, 1))
|
||||||
RGB(r,g,b)
|
RGBA(r,g,b,rgba.alpha)
|
||||||
end
|
end
|
||||||
function lighten(c, v=0.3)
|
function lighten(c, v=0.3)
|
||||||
darken(c, -v)
|
darken(c, -v)
|
||||||
@ -103,7 +103,7 @@ colorscheme(cs::AVec, vs::AVec) = ColorGradient(cs, vs)
|
|||||||
colorscheme{T<:Colorant}(cs::AVec{T}) = ColorGradient(cs)
|
colorscheme{T<:Colorant}(cs::AVec{T}) = ColorGradient(cs)
|
||||||
colorscheme(f::Function) = ColorFunction(f)
|
colorscheme(f::Function) = ColorFunction(f)
|
||||||
colorscheme(v::AVec) = ColorVector(v)
|
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)
|
colorscheme(c::Colorant) = ColorWrapper(c)
|
||||||
|
|
||||||
const _rainbowColors = [colorant"blue", colorant"purple", colorant"green", colorant"orange", colorant"red"]
|
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)
|
ColorGradient(cs, vals)
|
||||||
end
|
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)
|
function getColorZ(gradient::ColorGradient, z::Real)
|
||||||
cs = gradient.colors
|
cs = gradient.colors
|
||||||
@ -173,22 +173,23 @@ end
|
|||||||
|
|
||||||
getColorVector(gradient::ColorGradient) = gradient.colors
|
getColorVector(gradient::ColorGradient) = gradient.colors
|
||||||
|
|
||||||
function interpolate_lab(c1::Colorant, c2::Colorant, w::Real)
|
# function interpolate_lab(c1::Colorant, c2::Colorant, w::Real)
|
||||||
lab1 = Lab(c1)
|
# lab1 = Lab(c1)
|
||||||
lab2 = Lab(c2)
|
# lab2 = Lab(c2)
|
||||||
l = interpolate(lab1.l, lab2.l, w)
|
# l = interpolate(lab1.l, lab2.l, w)
|
||||||
a = interpolate(lab1.a, lab2.a, w)
|
# a = interpolate(lab1.a, lab2.a, w)
|
||||||
b = interpolate(lab1.b, lab2.b, w)
|
# b = interpolate(lab1.b, lab2.b, w)
|
||||||
RGB(Lab(l, a, b))
|
# RGB(Lab(l, a, b))
|
||||||
end
|
# end
|
||||||
|
|
||||||
function interpolate_rgb(c1::Colorant, c2::Colorant, w::Real)
|
function interpolate_rgb(c1::Colorant, c2::Colorant, w::Real)
|
||||||
rgb1 = RGB(c1)
|
rgb1 = RGBA(c1)
|
||||||
rgb2 = RGB(c2)
|
rgb2 = RGBA(c2)
|
||||||
r = interpolate(rgb1.r, rgb2.r, w)
|
r = interpolate(rgb1.r, rgb2.r, w)
|
||||||
g = interpolate(rgb1.g, rgb2.g, w)
|
g = interpolate(rgb1.g, rgb2.g, w)
|
||||||
b = interpolate(rgb1.b, rgb2.b, 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
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -233,6 +234,8 @@ immutable ColorWrapper{C<:Colorant} <: ColorScheme
|
|||||||
c::C
|
c::C
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ColorWrapper(s::Symbol) = ColorWrapper(parse(Colorant, s))
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
@ -297,10 +300,15 @@ function getPaletteUsingColorDiffFromBackground(bgcolor::Colorant, numcolors::In
|
|||||||
filter(c -> colordiff(c, bgcolor) >= mindiff, _allColors)
|
filter(c -> colordiff(c, bgcolor) >= mindiff, _allColors)
|
||||||
end
|
end
|
||||||
|
|
||||||
function getPaletteUsingRainbow(bgcolor::Colorant, numcolors::Int = _defaultNumColors)
|
function getPaletteUsingGradientSymbol(bgcolor::Colorant, numcolors::Int = _defaultNumColors; gradientsym::Symbol = :auto)
|
||||||
grad = ColorGradient(_gradients[isdark(bgcolor) ? :lightrainbow : :darkrainbow])
|
# @show gradientsym
|
||||||
|
if gradientsym == :auto
|
||||||
|
grad = ColorGradient(_gradients[isdark(bgcolor) ? :lightrainbow : :darkrainbow])
|
||||||
|
else
|
||||||
|
grad = ColorGradient(gradientsym)
|
||||||
|
end
|
||||||
zrng = getpctrange(numcolors)
|
zrng = getpctrange(numcolors)
|
||||||
RGB[getColorZ(grad, z) for z in zrng]
|
RGBA[getColorZ(grad, z) for z in zrng]
|
||||||
end
|
end
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------
|
||||||
@ -345,7 +353,7 @@ function handlePlotColors(::PlottingPackage, d::Dict)
|
|||||||
# d[:color_palette] = getPaletteUsingDistinguishableColors(bgcolor)
|
# d[:color_palette] = getPaletteUsingDistinguishableColors(bgcolor)
|
||||||
# d[:color_palette] = getPaletteUsingFixedColorList(bgcolor)
|
# d[:color_palette] = getPaletteUsingFixedColorList(bgcolor)
|
||||||
# d[:color_palette] = getPaletteUsingColorDiffFromBackground(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
|
# set the foreground color (text, ticks, gridlines) to be white or black depending
|
||||||
# on how dark the background is.
|
# on how dark the background is.
|
||||||
|
|||||||
@ -117,7 +117,7 @@ function plot!(plt::Plot, args...; kw...)
|
|||||||
|
|
||||||
warnOnUnsupportedScales(plt.backend, d)
|
warnOnUnsupportedScales(plt.backend, d)
|
||||||
|
|
||||||
dumpdict(d, "Updating plot items:")
|
dumpdict(d, "Updating plot items")
|
||||||
|
|
||||||
# add title, axis labels, ticks, etc
|
# add title, axis labels, ticks, etc
|
||||||
updatePlotItems(plt, d)
|
updatePlotItems(plt, d)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user