pyplot and plotly integration of ColorPalettes

This commit is contained in:
Daniel Schwabeneder 2020-04-11 19:21:45 +02:00
parent ec5fa44f2a
commit becfa33ee8
2 changed files with 19 additions and 5 deletions

View File

@ -350,7 +350,7 @@ function plotly_layout_json(plt::Plot)
end
plotly_colorscale(grad::PlotUtils.AbstractColorList, α = nothing) = plotly_colorscale(PlotUtils.color_list(grad), α)
plotly_colorscale(cg::ColorGradient, α = nothing) = plotly_colorscale(PlotUtils.color_list(cg), α)
plotly_colorscale(c::Colorant,α) = plotly_colorscale(_as_gradient(c), α = nothing)
function plotly_colorscale(c::AbstractVector{<:Colorant}, α = nothing)
if length(c) == 1
@ -360,6 +360,12 @@ function plotly_colorscale(c::AbstractVector{<:Colorant}, α = nothing)
return [[vals[i], rgba_string(plot_color(c[i], α))] for i in eachindex(c)]
end
end
function plotly_colorscale(cp::ColorPalette, α = nothing)
n = length(cp)
inds = repeat(1:n, inner = 2)
vals = vcat((i-1:i for i in 1:n)...) ./ n
return [[vals[i], rgba_string(plot_color(cp[ind], α))] for (i, ind) in enumerate(inds)]
end
# plotly_colorscale(c, alpha = nothing) = plotly_colorscale(cgrad(), alpha)

View File

@ -64,15 +64,23 @@ end
py_color(s) = py_color(parse(Colorant, string(s)))
py_color(c::Colorant) = (red(c), green(c), blue(c), alpha(c))
py_color(cs::AVec) = map(py_color, cs)
py_color(grad::ColorGradient) = py_color(grad.colors)
py_color(grad::PlotUtils.AbstractColorList) = py_color(color_list(grad))
py_color(c::Colorant, α) = py_color(plot_color(c, α))
function py_colormap(grad::ColorGradient)
pyvals = [(z, py_color(grad[z])) for z in grad.values]
function py_colormap(cg::ColorGradient)
n = length(cg)
pyvals = collect(zip(range(0, 1, length = n), py_color(PlotUtils.color_list(cg))))
cm = pycolors."LinearSegmentedColormap"."from_list"("tmp", pyvals)
cm."set_bad"(color=(0,0,0,0.0), alpha=0.0)
cm
end
function py_colormap(cp::ColorPalette)
n = length(cp)
pyvals = collect(zip(range(0, 1, length = n), py_color(PlotUtils.color_list(cp))))
cm = pycolors."LinearSegmentedColormap"."from_list"("tmp", pyvals, n)
cm."set_bad"(color=(0,0,0,0.0), alpha=0.0)
cm
end
py_colormap(c::Colorant) = py_colormap(_as_gradient(c))
@ -751,7 +759,7 @@ function py_add_series(plt::Plot{PyPlotBackend}, series::Series)
end
handle = ax."imshow"(z;
zorder = series[:series_plotindex],
cmap = py_colormap(cgrad([:black, :white])),
cmap = py_colormap(cgrad(plot_color([:black, :white]))),
vmin = 0.0,
vmax = 1.0,
extent = (xmin-0.5, xmax+0.5, ymax+0.5, ymin-0.5)