From 002603c388e16a7d31d73b8db0bac976a5ec570d Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Thu, 17 Dec 2015 11:55:26 -0500 Subject: [PATCH] added wireframe linetype; support for surface and wireframe in pyplot --- src/Plots.jl | 4 ++++ src/args.jl | 5 +++-- src/backends/plotly.jl | 6 +++--- src/backends/pyplot.jl | 21 ++++++++++++++++++++- src/backends/supported.jl | 2 +- src/components.jl | 2 ++ src/plot.jl | 6 +++++- src/plotter.jl | 35 ----------------------------------- 8 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index f8699857..feca5ce9 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -53,6 +53,8 @@ export contour!, surface, surface!, + wireframe, + wireframe!, path3d, path3d!, scatter3d, @@ -166,6 +168,8 @@ contour(args...; kw...) = plot(args...; kw..., linetype = :contour) contour!(args...; kw...) = plot!(args...; kw..., linetype = :contour) surface(args...; kw...) = plot(args...; kw..., linetype = :surface) surface!(args...; kw...) = plot!(args...; kw..., linetype = :surface) +wireframe(args...; kw...) = plot(args...; kw..., linetype = :wireframe) +wireframe!(args...; kw...) = plot!(args...; kw..., linetype = :wireframe) path3d(args...; kw...) = plot(args...; kw..., linetype = :path3d) path3d!(args...; kw...) = plot!(args...; kw..., linetype = :path3d) scatter3d(args...; kw...) = plot(args...; kw..., linetype = :scatter3d) diff --git a/src/args.jl b/src/args.jl index eb7cf071..e604853e 100644 --- a/src/args.jl +++ b/src/args.jl @@ -7,11 +7,11 @@ const _allAxes = [:auto, :left, :right] :r => :right ) -const _3dTypes = [:path3d, :scatter3d] +const _3dTypes = [:path3d, :scatter3d, :surface, :wireframe] const _allTypes = vcat([ :none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :density, :bar, :hline, :vline, :ohlc, - :contour, :surface, :pie + :contour, :pie ], _3dTypes) @compat const _typeAliases = Dict( :n => :none, @@ -34,6 +34,7 @@ const _allTypes = vcat([ :contours => :contour, :line3d => :path3d, :surf => :surface, + :wire => :wireframe, ) ishistlike(lt::Symbol) = lt in (:hist, :density) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index d24c7ae2..0bfa1fe8 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -276,8 +276,8 @@ function get_series_json(d::Dict; plot_index = nothing) d_out[:histnorm] = "probability density" end - elseif lt in (:contour, :surface) - d_out[:type] = string(lt) + elseif lt in (:contour, :surface, :wireframe) + d_out[:type] = lt == :wireframe ? :surface : string(lt) d_out[:x], d_out[:y] = x, y d_out[:z] = d[:z].surf # d_out[:showscale] = d[:legend] @@ -285,7 +285,7 @@ function get_series_json(d::Dict; plot_index = nothing) d_out[:ncontours] = d[:nlevels] d_out[:contours] = Dict(:coloring => d[:fillrange] != nothing ? "fill" : "lines") end - d_out[:colorscale] = plotly_colorscale(d[:linecolor]) + d_out[:colorscale] = plotly_colorscale(d[lt == :contour ? :linecolor : :fillcolor]) elseif lt == :pie d_out[:type] = "pie" diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index 15cc094a..2f712051 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -121,6 +121,11 @@ getAxis(plt::Plot{PyPlotPackage}, axis::Symbol) = (axis == :right ? getRightAxis # left axis is PyPlot., right axis is "f.axes[0].twinx()." function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol) + # # need to access mplot3d functions differently + # if linetype == :surface + # return mplot3d.pymember("Axes3D")[:plot_surface] + # end + # in the 2-axis case we need to get: [:] ax = getAxis(plt, axis) # ax[:set_ylabel](plt.plotargs[:yrightlabel]) @@ -134,6 +139,9 @@ function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol) :scatter => :scatter, :contour => :contour, :scatter3d => :scatter, + :surface => :plot_surface, + :wireframe => :plot_wireframe, + # :surface => pycolors.pymember("LinearSegmentedColormap")[:from_list] ) return ax[get(fmap, linetype, :plot)] end @@ -269,6 +277,15 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...) extra_kwargs[:linestyles] = getPyPlotLineStyle(lt, d[:linestyle]) # TODO: will need to call contourf to fill in the contours + elseif lt in (:surface, :wireframe) + if lt == :surface + extra_kwargs[:cmap] = getPyPlotColorMap(d[:fillcolor], d[:fillalpha]) + end + extra_kwargs[:rstride] = 1 + extra_kwargs[:cstride] = 1 + extra_kwargs[:linewidth] = d[:linewidth] + extra_kwargs[:edgecolor] = getPyPlotColor(d[:linecolor], d[:linealpha]) + else extra_kwargs[:linestyle] = getPyPlotLineStyle(lt, d[:linestyle]) @@ -304,7 +321,7 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...) # end # set these for all types - if lt != :contour + if !(lt in (:contour,:surface,:wireframe)) if !(lt in (:scatter, :scatter3d)) extra_kwargs[:color] = color extra_kwargs[:linewidth] = d[:linewidth] @@ -326,6 +343,8 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...) handle = ax[:contourf](x, y, surf, d[:nlevels]; cmap = getPyPlotColorMap(d[:fillcolor], d[:fillalpha])) end handle + elseif lt in (:surface,:wireframe) + plotfunc(repmat(d[:x]',length(d[:y]),1), repmat(d[:y],1,length(d[:x])), d[:z].surf'; extra_kwargs...) elseif lt in _3dTypes plotfunc(d[:x], d[:y], d[:z]; extra_kwargs...) elseif lt in (:scatter, :heatmap, :hexbin) diff --git a/src/backends/supported.jl b/src/backends/supported.jl index 0fea29a2..a41c4818 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -162,7 +162,7 @@ supportedArgs(::PyPlotPackage) = [ supportedAxes(::PyPlotPackage) = _allAxes supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :density, :bar, - :hline, :vline, :contour, :path3d, :scatter3d] + :hline, :vline, :contour, :path3d, :scatter3d, :surface, :wireframe] supportedStyles(::PyPlotPackage) = [:auto, :solid, :dash, :dot, :dashdot] # supportedMarkers(::PyPlotPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :hexagon] supportedMarkers(::PyPlotPackage) = vcat(_allMarkers, Shape) diff --git a/src/components.jl b/src/components.jl index fab37d63..f1b7114b 100644 --- a/src/components.jl +++ b/src/components.jl @@ -229,6 +229,8 @@ immutable Surface{M<:AMat} surf::M end +Surface(f::Function, x, y) = Surface(Float64[f(xi,yi) for xi in x, yi in y]) + # ----------------------------------------------------------------------- type OHLC{T<:Real} diff --git a/src/plot.jl b/src/plot.jl index e344abef..ea4e67f7 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -382,7 +382,7 @@ function createKWargsList{T<:Real}(plt::PlottingObject, x::AVec, y::AVec, zmat:: # surf = Array(Any,1,1) # surf[1,1] = convert(Matrix{Float64}, zmat) d = Dict(kw) - if !(get(d, :linetype, :none) in (:contour, :surface)) + if !(get(d, :linetype, :none) in (:contour, :surface, :wireframe)) d[:linetype] = :contour end createKWargsList(plt, x, y; d..., z = surf) @@ -392,6 +392,10 @@ function createKWargsList(plt::PlottingObject, surf::Surface; kw...) createKWargsList(plt, 1:size(surf.surf,1), 1:size(surf.surf,2), convert(Matrix{Float64}, surf.surf); kw...) end +function createKWargsList(plt::PlottingObject, x::AVec, y::AVec, surf::Surface; kw...) + createKWargsList(plt, x, y, convert(Matrix{Float64}, surf.surf); kw...) +end + function createKWargsList(plt::PlottingObject, f::FuncOrFuncs; kw...) error("Can't pass a Function or Vector{Function} for y without also passing x") end diff --git a/src/plotter.jl b/src/plotter.jl index 86444be6..20176fdb 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -98,41 +98,6 @@ CurrentBackend(sym::Symbol) = CurrentBackend(sym, backendInstance(sym)) # --------------------------------------------------------- -# function pickDefaultBackend() -# try -# if Pkg.installed("PyPlot") != nothing -# return CurrentBackend(:pyplot) -# end -# end -# try -# if Pkg.installed("Immerse") != nothing -# return CurrentBackend(:immerse) -# end -# end -# try -# if Pkg.installed("Qwt") != nothing -# return CurrentBackend(:qwt) -# end -# end -# try -# if Pkg.installed("Gadfly") != nothing -# return CurrentBackend(:gadfly) -# end -# end -# try -# if Pkg.installed("UnicodePlots") != nothing -# return CurrentBackend(:unicodeplots) -# end -# end -# try -# if Pkg.installed("Bokeh") != nothing -# return CurrentBackend(:bokeh) -# end -# end -# # warn("You don't have any of the supported backends installed! Chose from ", backends()) -# return CurrentBackend(:plotly) -# end - function pickDefaultBackend() for pkgstr in ("PyPlot", "Immerse", "Qwt", "Gadfly", "UnicodePlots", "Bokeh") if Pkg.installed(pkgstr) != nothing