add showtheme recipe
This commit is contained in:
parent
0f3928ec9a
commit
c76ca2dd6e
123
src/themes.jl
123
src/themes.jl
@ -4,11 +4,16 @@
|
|||||||
Specify the colour theme for plots.
|
Specify the colour theme for plots.
|
||||||
"""
|
"""
|
||||||
function theme(s::Symbol; kw...)
|
function theme(s::Symbol; kw...)
|
||||||
|
defaults = _get_defaults(s)
|
||||||
|
_theme(s, defaults; kw...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function _get_defaults(s::Symbol)
|
||||||
thm = PlotThemes._themes[s]
|
thm = PlotThemes._themes[s]
|
||||||
defaults = if :defaults in fieldnames(thm)
|
if :defaults in fieldnames(thm)
|
||||||
thm.defaults
|
return thm.defaults
|
||||||
else # old PlotTheme type
|
else # old PlotTheme type
|
||||||
defs = KW(
|
defaults = KW(
|
||||||
:bg => thm.bg_secondary,
|
:bg => thm.bg_secondary,
|
||||||
:bginside => thm.bg_primary,
|
:bginside => thm.bg_primary,
|
||||||
:fg => thm.lines,
|
:fg => thm.lines,
|
||||||
@ -18,14 +23,12 @@ function theme(s::Symbol; kw...)
|
|||||||
:palette => thm.palette,
|
:palette => thm.palette,
|
||||||
)
|
)
|
||||||
if thm.gradient != nothing
|
if thm.gradient != nothing
|
||||||
push!(defs, :gradient => thm.gradient)
|
push!(defaults, :gradient => thm.gradient)
|
||||||
end
|
end
|
||||||
defs
|
return defaults
|
||||||
end
|
end
|
||||||
_theme(s, defaults; kw...)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function _theme(s::Symbol, defaults::KW; kw...)
|
function _theme(s::Symbol, defaults::KW; kw...)
|
||||||
# Reset to defaults to overwrite active theme
|
# Reset to defaults to overwrite active theme
|
||||||
reset_defaults()
|
reset_defaults()
|
||||||
@ -58,3 +61,109 @@ function _theme(s::Symbol, defaults::KW; kw...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
@deprecate set_theme(s) theme(s)
|
@deprecate set_theme(s) theme(s)
|
||||||
|
|
||||||
|
@userplot ShowTheme
|
||||||
|
|
||||||
|
_color_functions = KW(
|
||||||
|
:protanopic => protanopic,
|
||||||
|
:deuteranopic => deuteranopic,
|
||||||
|
:tritanopic => tritanopic,
|
||||||
|
)
|
||||||
|
_get_showtheme_args(thm::Symbol) = thm, identity
|
||||||
|
_get_showtheme_args(thm::Symbol, func::Symbol) = thm, get(_color_functions, func, identity)
|
||||||
|
|
||||||
|
@recipe function showtheme(st::ShowTheme)
|
||||||
|
thm, cfunc = _get_showtheme_args(st.args...)
|
||||||
|
defaults = _get_defaults(thm)
|
||||||
|
|
||||||
|
# get the gradient
|
||||||
|
gradient_colors = pop!(defaults, :gradient, cgrad(:inferno).colors)
|
||||||
|
gradient = cgrad(cfunc.(RGB.(gradient_colors)))
|
||||||
|
|
||||||
|
# get the palette
|
||||||
|
palette = pop!(defaults, :palette, get_color_palette(:auto, plot_color(:white), 17))
|
||||||
|
palette = cfunc.(RGB.(palette))
|
||||||
|
|
||||||
|
# apply the theme
|
||||||
|
for k in keys(defaults)
|
||||||
|
def = defaults[k]
|
||||||
|
arg = get(_keyAliases, k, k)
|
||||||
|
plotattributes[arg] = if typeof(def) <: Colorant
|
||||||
|
cfunc(RGB(def))
|
||||||
|
elseif eltype(def) <: Colorant
|
||||||
|
cfunc.(RGB.(def))
|
||||||
|
elseif contains(string(arg), "color")
|
||||||
|
cfunc.(RGB.(plot_color.(def)))
|
||||||
|
else
|
||||||
|
def
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
srand(1)
|
||||||
|
|
||||||
|
label := ""
|
||||||
|
colorbar := false
|
||||||
|
layout := (2, 3)
|
||||||
|
|
||||||
|
for j in 1:4
|
||||||
|
@series begin
|
||||||
|
subplot := 1
|
||||||
|
palette := palette
|
||||||
|
seriestype := :path
|
||||||
|
linewidth := 2
|
||||||
|
cumsum(randn(50))
|
||||||
|
end
|
||||||
|
|
||||||
|
@series begin
|
||||||
|
subplot := 2
|
||||||
|
seriestype := :scatter
|
||||||
|
palette := palette
|
||||||
|
markersize := 5
|
||||||
|
markerstrokewidth := 0
|
||||||
|
marker := (:circle, :diamond, :star5, :square)[j]
|
||||||
|
randn(10), randn(10)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@series begin
|
||||||
|
subplot := 3
|
||||||
|
seriestype := :histogram
|
||||||
|
palette := palette
|
||||||
|
randn(1000) .+ (0:2:4)'
|
||||||
|
end
|
||||||
|
|
||||||
|
f(r) = sin(r) / r
|
||||||
|
_norm(x, y) = norm([x, y])
|
||||||
|
x = y = linspace(-3π, 3π, 30)
|
||||||
|
z = f.(_norm.(x, y'))
|
||||||
|
wi = 2:3:30
|
||||||
|
|
||||||
|
@series begin
|
||||||
|
subplot := 4
|
||||||
|
seriestype := :heatmap
|
||||||
|
seriescolor := gradient
|
||||||
|
x, y, z
|
||||||
|
end
|
||||||
|
|
||||||
|
@series begin
|
||||||
|
subplot := 5
|
||||||
|
seriestype := :surface
|
||||||
|
seriescolor := gradient
|
||||||
|
x, y, z
|
||||||
|
end
|
||||||
|
|
||||||
|
n = 100
|
||||||
|
ts = linspace(0, 10π, n)
|
||||||
|
x = ts .* cos.(ts)
|
||||||
|
y = (0.1ts) .* sin.(ts)
|
||||||
|
z = 1:n
|
||||||
|
|
||||||
|
@series begin
|
||||||
|
subplot := 6
|
||||||
|
seriescolor := gradient
|
||||||
|
linewidth := 3
|
||||||
|
line_z := z
|
||||||
|
x, y, z
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user