pyplot heatmaps working; ColorGradient fix

This commit is contained in:
Thomas Breloff 2016-03-09 23:34:56 -05:00
parent 581fdfdfe0
commit 2e6b8a067d
3 changed files with 21 additions and 16 deletions

View File

@ -176,7 +176,7 @@ function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol)
:scatter3d => :scatter, :scatter3d => :scatter,
:surface => :plot_surface, :surface => :plot_surface,
:wireframe => :plot_wireframe, :wireframe => :plot_wireframe,
:heatmap => :imshow, :heatmap => :pcolor,
# :surface => pycolors.pymember("LinearSegmentedColormap")[:from_list] # :surface => pycolors.pymember("LinearSegmentedColormap")[:from_list]
) )
return ax[get(fmap, linetype, :plot)] return ax[get(fmap, linetype, :plot)]
@ -373,9 +373,6 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...)
elseif lt == :heatmap elseif lt == :heatmap
extra_kwargs[:cmap] = getPyPlotColorMap(d[:fillcolor], d[:fillalpha]) extra_kwargs[:cmap] = getPyPlotColorMap(d[:fillcolor], d[:fillalpha])
left, right = extrema(d[:x])
bottom, top = extrema(d[:y])
extra_kwargs[:extent] = left, right, bottom, top
else else
@ -425,7 +422,7 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...)
# end # end
# set these for all types # set these for all types
if !(lt in (:contour,:surface,:wireframe)) if !(lt in (:contour,:surface,:wireframe,:heatmap))
if !(lt in (:scatter, :scatter3d)) if !(lt in (:scatter, :scatter3d))
extra_kwargs[:color] = color extra_kwargs[:color] = color
extra_kwargs[:linewidth] = d[:linewidth] extra_kwargs[:linewidth] = d[:linewidth]
@ -474,7 +471,8 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...)
plotfunc(d[:x], d[:y]; extra_kwargs...) plotfunc(d[:x], d[:y]; extra_kwargs...)
elseif lt == :heatmap elseif lt == :heatmap
plotfunc(d[:z]; extra_kwargs...) x, y, z = d[:x], d[:y], d[:z].surf'
plotfunc(heatmap_edges(x), heatmap_edges(y), z; extra_kwargs...)
else # plot else # plot
plotfunc(d[:x], d[:y]; extra_kwargs...)[1] plotfunc(d[:x], d[:y]; extra_kwargs...)[1]
@ -798,7 +796,7 @@ function addPyPlotLegend(plt::Plot, ax)
leg = plt.plotargs[:legend] leg = plt.plotargs[:legend]
if leg != :none if leg != :none
# gotta do this to ensure both axes are included # gotta do this to ensure both axes are included
args = filter(x -> !(x[:linetype] in (:hist,:density,:hexbin,:hist2d,:hline,:vline,:contour, :surface, :wireframe, :path3d, :scatter3d)), plt.seriesargs) args = filter(x -> !(x[:linetype] in (:hist,:density,:hexbin,:hist2d,:hline,:vline,:contour,:surface,:wireframe,:heatmap,:path3d,:scatter3d)), plt.seriesargs)
args = filter(x -> x[:label] != "", args) args = filter(x -> x[:label] != "", args)
if length(args) > 0 if length(args) > 0
leg = ax[:legend]([d[:serieshandle] for d in args], leg = ax[:legend]([d[:serieshandle] for d in args],

View File

@ -72,10 +72,10 @@ const _testColors = [colorant"darkblue", colorant"blueviolet", colorant"darkcya
"Continuous gradient between values. Wraps a list of bounding colors and the values they represent." "Continuous gradient between values. Wraps a list of bounding colors and the values they represent."
immutable ColorGradient <: ColorScheme immutable ColorGradient <: ColorScheme
colors::Vector{Colorant} colors::Vector
values::Vector{Float64} values::Vector
function ColorGradient{T<:Colorant,S<:Real}(cs::AVec{T}, vals::AVec{S} = linspace(0, 1, length(cs)); alpha = nothing) function ColorGradient{S<:Real}(cs::AVec, vals::AVec{S} = linspace(0, 1, length(cs)); alpha = nothing)
if length(cs) == length(vals) if length(cs) == length(vals)
return new(convertColor(cs,alpha), collect(vals)) return new(convertColor(cs,alpha), collect(vals))
end end
@ -103,13 +103,13 @@ function ColorGradient{T<:Real}(s::Symbol, vals::AVec{T} = 0:0; kw...)
ColorGradient(cs, vals; kw...) ColorGradient(cs, vals; kw...)
end end
function ColorGradient{T<:Real}(cs::AVec, vals::AVec{T} = linspace(0, 1, length(cs)); kw...) # function ColorGradient{T<:Real}(cs::AVec, vals::AVec{T} = linspace(0, 1, length(cs)); kw...)
ColorGradient(map(convertColor, cs), vals; kw...) # ColorGradient(map(convertColor, cs), vals; kw...)
end # end
function ColorGradient(grad::ColorGradient; alpha = nothing) # function ColorGradient(grad::ColorGradient; alpha = nothing)
ColorGradient(convertColor(grad.colors, alpha), grad.values) # ColorGradient(convertColor(grad.colors, alpha), grad.values)
end # end
getColor(gradient::ColorGradient, idx::Int) = gradient.colors[mod1(idx, length(gradient.colors))] getColor(gradient::ColorGradient, idx::Int) = gradient.colors[mod1(idx, length(gradient.colors))]

View File

@ -181,6 +181,13 @@ Base.first(x::Symbol) = x
sortedkeys(d::Dict) = sort(collect(keys(d))) sortedkeys(d::Dict) = sort(collect(keys(d)))
"create an (n+1) list of the outsides of heatmap rectangles"
function heatmap_edges(v::AVec)
vmin, vmax = extrema(v)
extra = 0.5 * (vmax-vmin) / (length(v)-1)
vcat(vmin-extra, 0.5 * (v[1:end-1] + v[2:end]), vmax+extra)
end
function fakedata(sz...) function fakedata(sz...)
y = zeros(sz...) y = zeros(sz...)