working on corrplot and recipes

This commit is contained in:
Thomas Breloff 2015-10-09 17:03:54 -04:00
parent a51a8abf27
commit 15a4b50ce4
4 changed files with 127 additions and 69 deletions

File diff suppressed because one or more lines are too long

View File

@ -72,9 +72,6 @@ export
getColor, getColor,
getColorZ, getColorZ,
PlotRecipe,
EllipseRecipe,
debugplots, debugplots,
supportedArgs, supportedArgs,
@ -82,7 +79,12 @@ export
supportedTypes, supportedTypes,
supportedStyles, supportedStyles,
supportedMarkers, supportedMarkers,
subplotSupported subplotSupported,
# recipes
PlotRecipe,
EllipseRecipe,
corrplot
# --------------------------------------------------------- # ---------------------------------------------------------

View File

@ -85,7 +85,10 @@ function createGadflyPlotObject(d::Dict)
unshift!(gplt.guides, Gadfly.Guide.manual_color_key("", @compat(AbstractString)[], Color[])) unshift!(gplt.guides, Gadfly.Guide.manual_color_key("", @compat(AbstractString)[], Color[]))
end end
gplt.theme = Gadfly.Theme(background_color = getColor(d[:background_color])) gplt.theme = Gadfly.Theme(
background_color = getColor(d[:background_color]),
plot_padding = 1 * Gadfly.mm,
)
gplt gplt
end end
@ -493,7 +496,7 @@ function linkXAxis(subplt::Subplot{GadflyPackage})
for (i,(r,c)) in enumerate(subplt.layout) for (i,(r,c)) in enumerate(subplt.layout)
gplt = subplt.plts[i].o gplt = subplt.plts[i].o
if r < nrows(subplt.layout) if r < nrows(subplt.layout)
#addOrReplace(gplt.guides, Gadfly.Guide.xticks; label=false) addOrReplace(gplt.guides, Gadfly.Guide.xticks; label=false)
addOrReplace(gplt.guides, Gadfly.Guide.xlabel, "") addOrReplace(gplt.guides, Gadfly.Guide.xlabel, "")
end end
end end
@ -516,7 +519,7 @@ function linkYAxis(subplt::Subplot{GadflyPackage})
for (i,(r,c)) in enumerate(subplt.layout) for (i,(r,c)) in enumerate(subplt.layout)
gplt = subplt.plts[i].o gplt = subplt.plts[i].o
if c > 1 if c > 1
#addOrReplace(gplt.guides, Gadfly.Guide.yticks; label=false) addOrReplace(gplt.guides, Gadfly.Guide.yticks; label=false)
addOrReplace(gplt.guides, Gadfly.Guide.ylabel, "") addOrReplace(gplt.guides, Gadfly.Guide.ylabel, "")
end end
end end

View File

@ -12,45 +12,81 @@ plot!(plt::Plot, recipe::PlotRecipe, args...; kw...) = plot!(getRecipeXY(recipe)
# ------------------------------------------------- # -------------------------------------------------
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
# -------------------------------------------------
"Do a correlation plot"
function corrplot{T<:Real,S<:Real}(mat::AMat{T}, corrmat::AMat{S};
colors = :redsblues)
m = size(mat,2)
# might be a mistake?
@assert m <= 10
@assert size(corrmat) == (m,m)
# create a subplot grid, and a gradient from -1 to 1
p = subplot(zeros(1,m^2), n=m^2, link=true)
cgrad = ColorGradient(:redsblues,[-1,1])
# make all the plots
for i in 1:m
for j in 1:m
idx = p.layout[i,j]
if i==j
# histogram on diagonal
plt = histogram(mat[:,i], c=:black)
else
# scatter plots off-diagonal, color determined by correlation
plt = scatter(mat[:,j], mat[:,i], ms=4, c=getColorZ(cgrad, corrmat[i,j]))
end
# replace the plt
p.plts[idx] = plt
end
end
# link the axes
subplot!(p, link=true)
end end