preparing for MLPlots; removed corrplot

This commit is contained in:
Thomas Breloff 2015-11-20 14:05:06 -05:00
parent 34831d42a4
commit 9cf92f37f5
3 changed files with 90 additions and 81 deletions

View File

@ -10,6 +10,14 @@ using Reexport
@reexport using Colors @reexport using Colors
export export
Plot,
Subplot,
SubplotLayout,
GridLayout,
FlexLayout,
AVec,
AMat,
plot, plot,
plot!, plot!,
# plot_display, # plot_display,
@ -94,8 +102,8 @@ export
# recipes # recipes
PlotRecipe, PlotRecipe,
EllipseRecipe, # EllipseRecipe,
spy, # spy,
corrplot corrplot
# --------------------------------------------------------- # ---------------------------------------------------------

View File

@ -285,6 +285,7 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...)
extra_kwargs[:alpha] = d[:markeralpha] extra_kwargs[:alpha] = d[:markeralpha]
end end
extra_kwargs[:edgecolors] = getPyPlotColor(d[:markerstrokecolor], d[:markerstrokealpha]) extra_kwargs[:edgecolors] = getPyPlotColor(d[:markerstrokecolor], d[:markerstrokealpha])
extra_kwargs[:linewidths] = d[:markerstrokewidth]
else else
extra_kwargs[:markersize] = d[:markersize] extra_kwargs[:markersize] = d[:markersize]
extra_kwargs[:markerfacecolor] = getPyPlotColor(d[:markercolor], d[:markeralpha]) extra_kwargs[:markerfacecolor] = getPyPlotColor(d[:markercolor], d[:markeralpha])

View File

@ -15,100 +15,100 @@ plot!(recipe::PlotRecipe, args...; kw...) = plot!(getRecipeXY(recipe)..., args..
plot!(plt::Plot, recipe::PlotRecipe, args...; kw...) = plot!(getRecipeXY(recipe)..., args...; getRecipeArgs(recipe)..., kw...) plot!(plt::Plot, recipe::PlotRecipe, args...; kw...) = plot!(getRecipeXY(recipe)..., args...; getRecipeArgs(recipe)..., kw...)
# ------------------------------------------------- # # -------------------------------------------------
function rotate(x::Real, y::Real, θ::Real; center = (0,0)) # function rotate(x::Real, y::Real, θ::Real; center = (0,0))
cx = x - center[1] # cx = x - center[1]
cy = y - center[2] # cy = y - center[2]
xrot = cx * cos(θ) - cy * sin(θ) # xrot = cx * cos(θ) - cy * sin(θ)
yrot = cy * cos(θ) + cx * sin(θ) # yrot = cy * cos(θ) + cx * sin(θ)
xrot + center[1], yrot + center[2] # xrot + center[1], yrot + center[2]
end # end
# ------------------------------------------------- # # -------------------------------------------------
type EllipseRecipe <: PlotRecipe # type EllipseRecipe <: PlotRecipe
w::Float64 # w::Float64
h::Float64 # h::Float64
x::Float64 # x::Float64
y::Float64 # y::Float64
θ::Float64 # θ::Float64
end # end
EllipseRecipe(w,h,x,y) = EllipseRecipe(w,h,x,y,0) # EllipseRecipe(w,h,x,y) = EllipseRecipe(w,h,x,y,0)
# return x,y coords of a rotated ellipse, centered at the origin # # return x,y coords of a rotated ellipse, centered at the origin
function rotatedEllipse(w, h, x, y, θ, rotθ) # function rotatedEllipse(w, h, x, y, θ, rotθ)
# # coord before rotation # # # coord before rotation
xpre = w * cos(θ) # xpre = w * cos(θ)
ypre = h * sin(θ) # ypre = h * sin(θ)
# rotate and translate # # rotate and translate
r = rotate(xpre, ypre, rotθ) # r = rotate(xpre, ypre, rotθ)
x + r[1], y + r[2] # x + r[1], y + r[2]
end # end
function getRecipeXY(ep::EllipseRecipe) # function getRecipeXY(ep::EllipseRecipe)
x, y = unzip([rotatedEllipse(ep.w, ep.h, ep.x, ep.y, u, ep.θ) for u in linspace(0,2π,100)]) # x, y = unzip([rotatedEllipse(ep.w, ep.h, ep.x, ep.y, u, ep.θ) for u in linspace(0,2π,100)])
top = rotate(0, ep.h, ep.θ) # top = rotate(0, ep.h, ep.θ)
right = rotate(ep.w, 0, ep.θ) # right = rotate(ep.w, 0, ep.θ)
linex = Float64[top[1], 0, right[1]] + ep.x # linex = Float64[top[1], 0, right[1]] + ep.x
liney = Float64[top[2], 0, right[2]] + ep.y # liney = Float64[top[2], 0, right[2]] + ep.y
Any[x, linex], Any[y, liney] # Any[x, linex], Any[y, liney]
end # end
function getRecipeArgs(ep::EllipseRecipe) # function getRecipeArgs(ep::EllipseRecipe)
[(:line, (3, [:dot :solid], [:red :blue], :path))] # [(:line, (3, [:dot :solid], [:red :blue], :path))]
end # end
# ------------------------------------------------- # # -------------------------------------------------
"Correlation scatter matrix" # "Correlation scatter matrix"
function corrplot{T<:Real,S<:Real}(mat::AMat{T}, corrmat::AMat{S} = cor(mat); # function corrplot{T<:Real,S<:Real}(mat::AMat{T}, corrmat::AMat{S} = cor(mat);
colors = :redsblues, # colors = :redsblues,
labels = nothing, kw...) # labels = nothing, kw...)
m = size(mat,2) # m = size(mat,2)
centers = Float64[mean(extrema(mat[:,i])) for i in 1:m] # centers = Float64[mean(extrema(mat[:,i])) for i in 1:m]
# might be a mistake? # # might be a mistake?
@assert m <= 20 # @assert m <= 20
@assert size(corrmat) == (m,m) # @assert size(corrmat) == (m,m)
# create a subplot grid, and a gradient from -1 to 1 # # create a subplot grid, and a gradient from -1 to 1
p = subplot(rand(0,m^2); n=m^2, leg=false, grid=false, kw...) # p = subplot(rand(0,m^2); n=m^2, leg=false, grid=false, kw...)
cgrad = ColorGradient(colors, [-1,1]) # cgrad = ColorGradient(colors, [-1,1])
# make all the plots # # make all the plots
for i in 1:m # for i in 1:m
for j in 1:m # for j in 1:m
idx = p.layout[i,j] # idx = p.layout[i,j]
plt = p.plts[idx] # plt = p.plts[idx]
if i==j # if i==j
# histogram on diagonal # # histogram on diagonal
histogram!(plt, mat[:,i], c=:black) # histogram!(plt, mat[:,i], c=:black)
i > 1 && plot!(plt, yticks = :none) # i > 1 && plot!(plt, yticks = :none)
elseif i < j # elseif i < j
# annotate correlation value in upper triangle # # annotate correlation value in upper triangle
mi, mj = centers[i], centers[j] # mi, mj = centers[i], centers[j]
plot!(plt, [mj], [mi], # plot!(plt, [mj], [mi],
ann = (mj, mi, text(@sprintf("Corr:\n%0.3f", corrmat[i,j]), 15)), # ann = (mj, mi, text(@sprintf("Corr:\n%0.3f", corrmat[i,j]), 15)),
yticks=:none) # yticks=:none)
else # else
# scatter plots in lower triangle; color determined by correlation # # scatter plots in lower triangle; color determined by correlation
c = RGBA(RGB(getColorZ(cgrad, corrmat[i,j])), 0.3) # c = RGBA(RGB(getColorZ(cgrad, corrmat[i,j])), 0.3)
scatter!(plt, mat[:,j], mat[:,i], w=0, ms=3, c=c, smooth=true) # scatter!(plt, mat[:,j], mat[:,i], w=0, ms=3, c=c, smooth=true)
end # end
if labels != nothing && length(labels) >= m # if labels != nothing && length(labels) >= m
i == m && xlabel!(plt, string(labels[j])) # i == m && xlabel!(plt, string(labels[j]))
j == 1 && ylabel!(plt, string(labels[i])) # j == 1 && ylabel!(plt, string(labels[i]))
end # end
end # end
end # end
# link the axes # # link the axes
subplot!(p, link = (r,c) -> (true, r!=c)) # subplot!(p, link = (r,c) -> (true, r!=c))
end # end
"Sparsity plot... heatmap of non-zero values of a matrix" "Sparsity plot... heatmap of non-zero values of a matrix"