From 78045fd2ab934d6a776e44eb1d0c7842fdb18dcf Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Sun, 4 Oct 2015 21:39:32 -0500 Subject: [PATCH] working on spy and nbins tuple --- examples/spy.ipynb | 107 +++++++++++++++++++++++++++++++++++++++++ src/Plots.jl | 8 ++- src/backends/gadfly.jl | 8 +-- src/backends/qwt.jl | 7 +++ src/utils.jl | 4 ++ 5 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 examples/spy.ipynb diff --git a/examples/spy.ipynb b/examples/spy.ipynb new file mode 100644 index 00000000..eb089de8 --- /dev/null +++ b/examples/spy.ipynb @@ -0,0 +1,107 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[Plots.jl] Default backend: immerse" + ] + } + ], + "source": [ + "using Plots\n", + "n = 1000\n", + "a = rand(n)\n", + "y = Float64[i*a[i]+j*a[j] for i in 1:n, j in 1:n]\n", + "y = float(y .> mean(y));" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "[Plots.jl] Initializing backend: immerse" + ] + } + ], + "source": [ + "spy(y, nbins=(20,100))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# get the indices (I,J) and the values (V) of non-zero values in y\n", + "I,J,V = findnz(y);\n", + "# plot the J's vs the I's in a heatmap to recreate the spy call\n", + "heatmap(J,I)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pyplot()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "histogram(randn(1000), " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.4.0-rc2", + "language": "julia", + "name": "julia-0.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.4.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/src/Plots.jl b/src/Plots.jl index 66d0d779..a54c78ab 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -33,6 +33,8 @@ export ohlc, ohlc!, + spy, + title!, xlabel!, ylabel!, @@ -95,6 +97,11 @@ vline!(args...; kw...) = plot!(args...; kw..., linetype = :vline) ohlc(args...; kw...) = plot(args...; kw..., linetype = :ohlc) ohlc!(args...; kw...) = plot!(args...; kw..., linetype = :ohlc) +"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, kw...) +end title!(s::AbstractString) = plot!(title = s) xlabel!(s::AbstractString) = plot!(xlabel = s) @@ -122,7 +129,6 @@ xticks!{T<:Real,S<:AbstractString}(plt::Plot, ticks::AVec{T}, labels::AVec{S}) yticks!{T<:Real,S<:AbstractString}(plt::Plot, ticks::AVec{T}, labels::AVec{S}) = plot!(plt; yticks = (ticks,labels)) annotate!(plt::Plot, anns) = plot!(plt; annotation = anns) - # --------------------------------------------------------- diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index cca7280b..b889a808 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -85,12 +85,12 @@ function createGadflyPlotObject(d::Dict) end -# function getGeoms(linetype::Symbol, marker::Symbol, markercolor::Colorant, nbins::Int) function getLineGeoms(d::Dict) lt = d[:linetype] - lt == :hexbin && return [Gadfly.Geom.hexbin(xbincount = d[:nbins], ybincount = d[:nbins])] - lt == :heatmap && return [Gadfly.Geom.histogram2d(xbincount = d[:nbins], ybincount = d[:nbins])] - lt == :hist && return [Gadfly.Geom.histogram(bincount = d[:nbins])] + xbins, ybins = maketuple(d[:nbins]) + lt == :hexbin && return [Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins)] + lt == :heatmap && return [Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins)] + lt == :hist && return [Gadfly.Geom.histogram(bincount = xbins)] # lt == :none && return [Gadfly.Geom.path] lt == :path && return [Gadfly.Geom.path] # lt == :scatter && return [Gadfly.Geom.point] diff --git a/src/backends/qwt.jl b/src/backends/qwt.jl index 1c415b4f..daadcb24 100644 --- a/src/backends/qwt.jl +++ b/src/backends/qwt.jl @@ -95,6 +95,13 @@ function adjustQwtKeywords(plt::Plot{QwtPackage}, iscreating::Bool; kw...) end replaceLinetypeAlias(d) + + for k in keys(d) + if haskey(_qwtAliases, k) + d[_qwtAliases[k]] = d[k] + end + end + d end diff --git a/src/utils.jl b/src/utils.jl index 7e18698e..6b720876 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -107,6 +107,10 @@ end makevec(v::AVec) = v makevec{T}(v::T) = T[v] +"duplicate a single value, or pass the 2-tuple through" +maketuple(x::Real) = (x,x) +maketuple{T,S}(x::Tuple{T,S}) = x + function replaceAliases!(d::Dict, aliases::Dict) for (k,v) in d