From f568803af27860653701ee8f7a8427abde6606f9 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Wed, 9 Mar 2016 17:48:43 -0500 Subject: [PATCH] heatmaps, colorscales, and spy... oh my --- src/Plots.jl | 4 ++-- src/backends/plotly.jl | 44 +++++++++++++++++++++++++++------------ src/backends/supported.jl | 4 ++-- src/colors.jl | 4 ++++ src/plot.jl | 10 ++++----- src/recipes.jl | 7 ++++--- 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index f643789b..352d8aae 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -124,8 +124,8 @@ export # recipes PlotRecipe, # EllipseRecipe, - # spy, - corrplot + spy + # corrplot # --------------------------------------------------------- diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index d58953aa..e444260c 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -249,10 +249,10 @@ function get_plot_json(plt::Plot{PlotlyPackage}) end -function plotly_colorscale(grad::ColorGradient) - [[grad.values[i], webcolor(grad.colors[i])] for i in 1:length(grad.colors)] +function plotly_colorscale(grad::ColorGradient, alpha = nothing) + [[grad.values[i], webcolor(grad.colors[i], alpha)] for i in 1:length(grad.colors)] end -plotly_colorscale(c) = plotly_colorscale(ColorGradient(:bluesreds)) +plotly_colorscale(c, alpha = nothing) = plotly_colorscale(ColorGradient(:bluesreds), alpha) const _plotly_markers = Dict{Symbol,Any}( :rect => "square", @@ -316,16 +316,26 @@ function plotly_series(d::Dict; plot_index = nothing) d_out[:histnorm] = "probability density" end - elseif lt in (:contour, :surface, :wireframe) - d_out[:type] = lt == :wireframe ? :surface : string(lt) + elseif lt == :heatmap + d_out[:type] = "heatmap" d_out[:x], d_out[:y] = x, y d_out[:z] = d[:z].surf - # d_out[:showscale] = d[:legend] - if lt == :contour - d_out[:ncontours] = d[:levels] - d_out[:contours] = Dict{Symbol,Any}(:coloring => d[:fillrange] != nothing ? "fill" : "lines") - end - d_out[:colorscale] = plotly_colorscale(d[lt == :contour ? :linecolor : :fillcolor]) + d_out[:colorscale] = plotly_colorscale(d[:fillcolor], d[:fillalpha]) + + elseif lt == :contour + d_out[:type] = "contour" + d_out[:x], d_out[:y] = x, y + d_out[:z] = d[:z].surf + # d_out[:showscale] = d[:colorbar] != :none + d_out[:ncontours] = d[:levels] + d_out[:contours] = Dict{Symbol,Any}(:coloring => d[:fillrange] != nothing ? "fill" : "lines") + d_out[:colorscale] = plotly_colorscale(d[:linecolor], d[:linealpha]) + + elseif lt in (:surface, :wireframe) + d_out[:type] = "surface" + d_out[:x], d_out[:y] = x, y + d_out[:z] = d[:z].surf + d_out[:colorscale] = plotly_colorscale(d[:fillcolor], d[:fillalpha]) elseif lt == :pie d_out[:type] = "pie" @@ -360,11 +370,19 @@ function plotly_series(d::Dict; plot_index = nothing) :width => d[:markerstrokewidth], ), ) + + # gotta hack this (for now?) since plotly can't handle rgba values inside the gradient if d[:zcolor] != nothing - d_out[:marker][:color] = d[:zcolor] - d_out[:marker][:colorscale] = plotly_colorscale(d[:markercolor]) + # d_out[:marker][:color] = d[:zcolor] + # d_out[:marker][:colorscale] = plotly_colorscale(d[:markercolor], d[:markeralpha]) + # d_out[:showscale] = true + grad = ColorGradient(d[:markercolor], alpha=d[:markeralpha]) + zmin, zmax = extrema(d[:zcolor]) + d_out[:marker][:color] = [webcolor(getColorZ(grad, (zi - zmin) / (zmax - zmin))) for zi in d[:zcolor]] end + end + dumpdict(d_out, "", true) # add "line" if hasline diff --git a/src/backends/supported.jl b/src/backends/supported.jl index 664de905..087559fe 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -563,7 +563,7 @@ supportedArgs(::PlotlyPackage) = [ supportedAxes(::PlotlyPackage) = [:auto, :left] supportedTypes(::PlotlyPackage) = [:none, :line, :path, :scatter, :steppre, :steppost, :hist2d, :hist, :density, :bar, :contour, :surface, :path3d, :scatter3d, - :pie] #,, :sticks, :hexbin, :hline, :vline] + :pie, :heatmap] #,, :sticks, :hexbin, :hline, :vline] supportedStyles(::PlotlyPackage) = [:auto, :solid, :dash, :dot, :dashdot] supportedMarkers(::PlotlyPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :pentagon, :hexagon, :octagon, :vline, :hline] #vcat(_allMarkers, Shape) @@ -633,7 +633,7 @@ supportedArgs(::PlotlyJSPackage) = [ supportedAxes(::PlotlyJSPackage) = [:auto, :left] supportedTypes(::PlotlyJSPackage) = [:none, :line, :path, :scatter, :steppre, :steppost, :hist2d, :hist, :density, :bar, :contour, :surface, :path3d, :scatter3d, - :pie] #,, :sticks, :hexbin, :hline, :vline] + :pie, :heatmap] #,, :sticks, :hexbin, :hline, :vline] supportedStyles(::PlotlyJSPackage) = [:auto, :solid, :dash, :dot, :dashdot] supportedMarkers(::PlotlyJSPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :pentagon, :hexagon, :octagon, :vline, :hline] #vcat(_allMarkers, Shape) diff --git a/src/colors.jl b/src/colors.jl index dc703a84..4d1b9a60 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -107,6 +107,10 @@ function ColorGradient{T<:Real}(cs::AVec{Symbol}, vals::AVec{T} = linspace(0, 1, ColorGradient(map(convertColor, cs), vals; kw...) end +function ColorGradient(grad::ColorGradient; alpha = nothing) + ColorGradient(convertColor(grad.colors, alpha), grad.values) +end + getColor(gradient::ColorGradient, idx::Int) = gradient.colors[mod1(idx, length(gradient.colors))] function getColorZ(gradient::ColorGradient, z::Real) diff --git a/src/plot.jl b/src/plot.jl index c0405412..ef8d5da8 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -228,7 +228,7 @@ end typealias FuncOrFuncs @compat(Union{Function, AVec{Function}}) -all3D(d::Dict) = trueOrAllTrue(lt -> lt in (:contour, :surface, :wireframe, :image), get(d, :linetype, :none)) +all3D(d::Dict) = trueOrAllTrue(lt -> lt in (:contour, :heatmap, :surface, :wireframe), get(d, :linetype, :none)) # missing convertToAnyVector(v::@compat(Void), d::Dict) = Any[nothing], nothing @@ -328,10 +328,10 @@ function createKWargsList(plt::PlottingObject, x, y; kw...) lt = d[:linetype] if isa(d[:y], Surface) - if lt in (:contour, :surface, :wireframe, :image) + if lt in (:contour, :heatmap, :surface, :wireframe) z = d[:y] d[:y] = 1:size(z,2) - d[lt == :image ? :zcolor : :z] = z + d[:z] = z end end @@ -429,7 +429,7 @@ function createKWargsList{T<:Real}(plt::PlottingObject, x::AVec, y::AVec, zmat:: # surf[1,1] = convert(Matrix{Float64}, zmat) d = Dict(kw) d[:z] = Surface(convert(Matrix{Float64}, zmat)) - if !(get(d, :linetype, :none) in (:contour, :surface, :wireframe)) + if !(get(d, :linetype, :none) in (:contour, :heatmap, :surface, :wireframe)) d[:linetype] = :contour end createKWargsList(plt, x, y; d...) #, z = surf) @@ -443,7 +443,7 @@ function createKWargsList{T<:Real}(plt::PlottingObject, x::AMat{T}, y::AMat{T}, # surf[1,1] = convert(Matrix{Float64}, zmat) d = Dict(kw) d[:z] = Surface(convert(Matrix{Float64}, zmat)) - if !(get(d, :linetype, :none) in (:contour, :surface, :wireframe)) + if !(get(d, :linetype, :none) in (:contour, :heatmap, :surface, :wireframe)) d[:linetype] = :contour end createKWargsList(plt, Any[x], Any[y]; d...) #kw..., z = surf, linetype = :contour) diff --git a/src/recipes.jl b/src/recipes.jl index dd883dec..c133e813 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -125,9 +125,10 @@ end "Sparsity plot... heatmap of non-zero values of a matrix" -function spy{T<:Real}(y::AMat{T}; kw...) - I,J,V = findnz(y) - heatmap(J, I; leg=false, yflip=true, kw...) +function spy{T<:Real}(z::AMat{T}; kw...) + # I,J,V = findnz(z) + # heatmap(J, I; leg=false, yflip=true, kw...) + heatmap(map(zi->float(zi!=0), z); leg=false, yflip=true, kw...) end "Adds a+bx... straight line over the current plot"