diff --git a/docs/readme_template.md b/docs/readme_template.md index f56fe741..07a00902 100644 --- a/docs/readme_template.md +++ b/docs/readme_template.md @@ -29,6 +29,8 @@ Please add wishlist items, bugs, or any other comments/questions to the issues l - [Qwt.jl](docs/qwt_examples.md) - [Winston.jl](docs/winston_examples.md) +Also check out the many [IJulia notebooks](http://nbviewer.ipython.org/github/tbreloff/Plots.jl/tree/master/examples/) with many examples. + ## Installation First, add the package @@ -84,9 +86,6 @@ png("gadfly1") ![gadfly_plt](img/gadfly1.png) -See the examples pages for lots of examples of plots, and what those commands produce for each supported backend. -Also check out the [IJulia notebooks](examples) and see how it works interactively. - ## API Call `backend(backend::Symbol)` or the shorthands (`gadfly()`, `qwt()`, `unicodeplots()`, etc) to set the current plotting backend. diff --git a/src/Plots.jl b/src/Plots.jl index 30e735ef..b62062f6 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -110,6 +110,7 @@ include("plot.jl") include("subplot.jl") include("recipes.jl") include("animation.jl") +include("output.jl") # --------------------------------------------------------- @@ -173,79 +174,6 @@ xaxis!(plt::Plot, args...; kw...) = plot!(pl yaxis!(plt::Plot, args...; kw...) = plot!(plt; yaxis = args, kw...) -# --------------------------------------------------------- - - -defaultOutputFormat(plt::PlottingObject) = "png" - -function png(plt::PlottingObject, fn::@compat(AbstractString)) - fn = addExtension(fn, "png") - io = open(fn, "w") - writemime(io, MIME("image/png"), plt) - close(io) -end -png(fn::@compat(AbstractString)) = png(current(), fn) - - -@compat const _savemap = Dict( - "png" => png, - ) - -function getExtension(fn::@compat(AbstractString)) - pieces = split(fn, ".") - length(pieces) > 1 || error("Can't extract file extension: ", fn) - ext = pieces[end] - haskey(_savemap, ext) || error("Invalid file extension: ", fn) - ext -end - -function addExtension(fn::@compat(AbstractString), ext::@compat(AbstractString)) - try - oldext = getExtension(fn) - if oldext == ext - return fn - else - return "$fn.$ext" - end - catch - return "$fn.$ext" - end -end - -function savefig(plt::PlottingObject, fn::@compat(AbstractString)) - - # get the extension - local ext - try - ext = getExtension(fn) - catch - # if we couldn't extract the extension, add the default - ext = defaultOutputFormat(plt) - fn = addExtension(fn, ext) - end - - # save it - func = get(_savemap, ext) do - error("Unsupported extension $ext with filename ", fn) - end - func(plt, fn) -end -savefig(fn::@compat(AbstractString)) = savefig(current(), fn) - - -# savepng(args...; kw...) = savepng(current(), args...; kw...) -# savepng(plt::PlottingObject, fn::@compat(AbstractString); kw...) = (io = open(fn, "w"); writemime(io, MIME("image/png"), plt); close(io)) - - - - -# --------------------------------------------------------- - -gui(plt::PlottingObject = current()) = display(PlotsDisplay(), plt) - - -# override the REPL display to open a gui window -Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::PlottingObject) = gui(plt) # --------------------------------------------------------- diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index b32db77c..032651a1 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -7,70 +7,70 @@ # gadfly() = backend(:gadfly) -# supportedArgs(::GadflyPackage) = setdiff(_allArgs, [:heatmap_c, :pos, :screen, :yrightlabel]) -supportedArgs(::GadflyPackage) = [ - :annotation, - # :axis, - :background_color, - :color, - :color_palette, - :fillrange, - :fillcolor, - :fillopacity, - :foreground_color, - :group, - :label, - :layout, - :legend, - :linestyle, - :linetype, - :linewidth, - :lineopacity, - :markershape, - :markercolor, - :markersize, - :markeropacity, - :n, - :nbins, - :nc, - :nr, - # :pos, - :smooth, - :show, - :size, - :title, - :windowtitle, - :x, - :xlabel, - :xlims, - :xticks, - :y, - :ylabel, - :ylims, - # :yrightlabel, - :yticks, - :xscale, - :yscale, - :xflip, - :yflip, - :z, - :tickfont, - :guidefont, - :legendfont, - :grid, - ] -supportedAxes(::GadflyPackage) = [:auto, :left] -supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] -supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] -supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape) -supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt] +# # supportedArgs(::GadflyPackage) = setdiff(_allArgs, [:heatmap_c, :pos, :screen, :yrightlabel]) +# supportedArgs(::GadflyPackage) = [ +# :annotation, +# # :axis, +# :background_color, +# :color, +# :color_palette, +# :fillrange, +# :fillcolor, +# :fillopacity, +# :foreground_color, +# :group, +# :label, +# :layout, +# :legend, +# :linestyle, +# :linetype, +# :linewidth, +# :lineopacity, +# :markershape, +# :markercolor, +# :markersize, +# :markeropacity, +# :n, +# :nbins, +# :nc, +# :nr, +# # :pos, +# :smooth, +# :show, +# :size, +# :title, +# :windowtitle, +# :x, +# :xlabel, +# :xlims, +# :xticks, +# :y, +# :ylabel, +# :ylims, +# # :yrightlabel, +# :yticks, +# :xscale, +# :yscale, +# :xflip, +# :yflip, +# :z, +# :tickfont, +# :guidefont, +# :legendfont, +# :grid, +# ] +# supportedAxes(::GadflyPackage) = [:auto, :left] +# supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] +# supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] +# supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape) +# supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt] # --------------------------------------------------------------------------- function createGadflyPlotObject(d::Dict) - @eval import DataFrames + # @eval import DataFrames gplt = Gadfly.Plot() gplt.mapping = Dict() @@ -588,7 +588,7 @@ end # create the underlying object (each backend will do this differently) function buildSubplotObject!(subplt::Subplot{GadflyPackage}, isbefore::Bool) - isbefore && return false + isbefore && return false # wait until after plotting to create the subplots subplt.o = nothing true end @@ -637,12 +637,30 @@ function setGadflyDisplaySize(w,h) Compose.set_default_graphic_size(w * Compose.px, h * Compose.px) end -function Base.writemime(io::IO, ::MIME"image/png", plt::Plot{GadflyPackage}) +# ------------------------------------------------------------------------- + + +function dowritemime{P<:GadflyOrImmerse}(io::IO, func, plt::PlottingObject{P}) gplt = getGadflyContext(plt) setGadflyDisplaySize(plt.initargs[:size]...) - Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) + Gadfly.draw(func(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) end +getGadflyWriteFunc(::MIME"image/png") = Gadfly.PNG +getGadflyWriteFunc(::MIME"image/svg+xml") = Gadfly.SVG +getGadflyWriteFunc(::MIME"text/html") = Gadfly.SVGJS +getGadflyWriteFunc(::MIME"application/pdf") = Gadfly.PDF +getGadflyWriteFunc(::MIME"application/postscript") = Gadfly.PS +getGadflyWriteFunc(m::MIME) = error("Unsupported in Gadfly/Immerse: ", m) + +for mime in (MIME"image/png", MIME"image/svg+xml", MIME"text/html", MIME"application/pdf", MIME"application/postscript") + @eval function Base.writemime{P<:GadflyOrImmerse}(io::IO, ::$mime, plt::PlottingObject{P}) + func = getGadflyWriteFunc($mime()) + dowritemime(io, func, plt) + end +end + + function Base.display(::PlotsDisplay, plt::Plot{GadflyPackage}) setGadflyDisplaySize(plt.initargs[:size]...) @@ -651,12 +669,6 @@ end -function Base.writemime(io::IO, ::MIME"image/png", plt::Subplot{GadflyPackage}) - gplt = getGadflyContext(plt) - setGadflyDisplaySize(plt.initargs[1][:size]...) - Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) -end - function Base.display(::PlotsDisplay, subplt::Subplot{GadflyPackage}) setGadflyDisplaySize(subplt.initargs[1][:size]...) ctx = buildGadflySubplotContext(subplt) diff --git a/src/backends/immerse.jl b/src/backends/immerse.jl index 2958c3ff..e40c13e3 100644 --- a/src/backends/immerse.jl +++ b/src/backends/immerse.jl @@ -7,12 +7,12 @@ # immerse() = backend(:immerse) -supportedArgs(::ImmersePackage) = supportedArgs(GadflyPackage()) -supportedAxes(::ImmersePackage) = supportedAxes(GadflyPackage()) -supportedTypes(::ImmersePackage) = supportedTypes(GadflyPackage()) -supportedStyles(::ImmersePackage) = supportedStyles(GadflyPackage()) -supportedMarkers(::ImmersePackage) = supportedMarkers(GadflyPackage()) -supportedScales(::ImmersePackage) = supportedScales(GadflyPackage()) +# supportedArgs(::ImmersePackage) = supportedArgs(GadflyPackage()) +# supportedAxes(::ImmersePackage) = supportedAxes(GadflyPackage()) +# supportedTypes(::ImmersePackage) = supportedTypes(GadflyPackage()) +# supportedStyles(::ImmersePackage) = supportedStyles(GadflyPackage()) +# supportedMarkers(::ImmersePackage) = supportedMarkers(GadflyPackage()) +# supportedScales(::ImmersePackage) = supportedScales(GadflyPackage()) function createImmerseFigure(d::Dict) @@ -150,11 +150,17 @@ end getGadflyContext(plt::Plot{ImmersePackage}) = plt.o[2] getGadflyContext(subplt::Subplot{ImmersePackage}) = buildGadflySubplotContext(subplt) -function Base.writemime(io::IO, ::MIME"image/png", plt::Plot{ImmersePackage}) - gplt = getGadflyContext(plt) - setGadflyDisplaySize(plt.initargs[:size]...) - Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) -end +# function Base.writemime(io::IO, ::MIME"image/png", plt::Plot{ImmersePackage}) +# gplt = getGadflyContext(plt) +# setGadflyDisplaySize(plt.initargs[:size]...) +# Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) +# end + +# function Base.writemime(io::IO, ::MIME"image/svg+xml", plt::Plot{ImmersePackage}) +# gplt = getGadflyContext(plt) +# setGadflyDisplaySize(plt.initargs[:size]...) +# Gadfly.draw(Gadfly.SVG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) +# end function Base.display(::PlotsDisplay, plt::Plot{ImmersePackage}) @@ -170,11 +176,18 @@ function Base.display(::PlotsDisplay, plt::Plot{ImmersePackage}) end -function Base.writemime(io::IO, ::MIME"image/png", plt::Subplot{ImmersePackage}) - gplt = getGadflyContext(plt) - setGadflyDisplaySize(plt.initargs[1][:size]...) - Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) -end +# function Base.writemime(io::IO, ::MIME"image/png", plt::Subplot{ImmersePackage}) +# gplt = getGadflyContext(plt) +# setGadflyDisplaySize(plt.initargs[1][:size]...) +# Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) +# end + + +# function Base.writemime(io::IO, ::MIME"image/svg+xml", plt::Subplot{ImmersePackage}) +# gplt = getGadflyContext(plt) +# setGadflyDisplaySize(plt.initargs[1][:size]...) +# Gadfly.draw(Gadfly.SVG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) +# end function Base.display(::PlotsDisplay, subplt::Subplot{ImmersePackage}) diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index f3aa0166..0120b925 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -8,65 +8,65 @@ # ------------------------------- -supportedArgs(::PyPlotPackage) = [ - :annotation, - # :args, - :axis, - :background_color, - :color, - :color_palette, - :fillrange, - :fillcolor, - :foreground_color, - :group, - # :heatmap_c, - # :kwargs, - :label, - :layout, - :legend, - :linestyle, - :linetype, - :linewidth, - :markershape, - :markercolor, - :markersize, - :n, - :nbins, - :nc, - :nr, - # :pos, - # :smooth, - # :ribbon, - :show, - :size, - :title, - :windowtitle, - :x, - :xlabel, - :xlims, - :xticks, - :y, - :ylabel, - :ylims, - :yrightlabel, - :yticks, - :xscale, - :yscale, - :xflip, - :yflip, - :z, - :tickfont, - :guidefont, - :legendfont, - # :grid, - ] -supportedAxes(::PyPlotPackage) = _allAxes -supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] -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) -supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10] -subplotSupported(::PyPlotPackage) = true +# supportedArgs(::PyPlotPackage) = [ +# :annotation, +# # :args, +# :axis, +# :background_color, +# :color, +# :color_palette, +# :fillrange, +# :fillcolor, +# :foreground_color, +# :group, +# # :heatmap_c, +# # :kwargs, +# :label, +# :layout, +# :legend, +# :linestyle, +# :linetype, +# :linewidth, +# :markershape, +# :markercolor, +# :markersize, +# :n, +# :nbins, +# :nc, +# :nr, +# # :pos, +# # :smooth, +# # :ribbon, +# :show, +# :size, +# :title, +# :windowtitle, +# :x, +# :xlabel, +# :xlims, +# :xticks, +# :y, +# :ylabel, +# :ylims, +# :yrightlabel, +# :yticks, +# :xscale, +# :yscale, +# :xflip, +# :yflip, +# :z, +# :tickfont, +# :guidefont, +# :legendfont, +# # :grid, +# ] +# supportedAxes(::PyPlotPackage) = _allAxes +# supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] +# 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) +# supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10] +# subplotSupported(::PyPlotPackage) = true # convert colorant to 4-tuple RGBA getPyPlotColor(c::Colorant) = map(f->float(f(c)), (red, green, blue, alpha)) @@ -556,10 +556,18 @@ function finalizePlot(plt::Plot{PyPlotPackage}) PyPlot.draw() end -function Base.writemime(io::IO, m::MIME"image/png", plt::Plot{PyPlotPackage}) - finalizePlot(plt) - writemime(io, m, getfig(plt.o)) -end +# # allow for writing any supported mime +# for mime in keys(PyPlot.aggformats) +# @eval function Base.writemime(io::IO, m::MIME{symbol{$mime}}, plt::Plot{PyPlotPackage}) +# finalizePlot(plt) +# writemime(io, m, getfig(plt.o)) +# end +# end + +# function Base.writemime(io::IO, m::@compat(Union{MIME"image/svg+xml", MIME"image/png"}, plt::Plot{PyPlotPackage}) +# finalizePlot(plt) +# writemime(io, m, getfig(plt.o)) +# end function Base.display(::PlotsDisplay, plt::Plot{PyPlotPackage}) @@ -580,8 +588,16 @@ function Base.display(::PlotsDisplay, subplt::Subplot{PyPlotPackage}) display(getfig(subplt.o)) end - -function Base.writemime(io::IO, m::MIME"image/png", subplt::Subplot{PyPlotPackage}) - finalizePlot(subplt) - writemime(io, m, getfig(subplt.o)) +# allow for writing any supported mime +for mime in (MIME"image/png", MIME"image/svg+xml", MIME"text/html", MIME"application/pdf", MIME"application/postscript") + @eval function Base.writemime(io::IO, ::$mime, plt::PlottingObject{PyPlotPackage}) + finalizePlot(plt) + writemime(io, $mime(), getfig(plt.o)) + end end + + +# function Base.writemime(io::IO, m::MIME"image/png", subplt::Subplot{PyPlotPackage}) +# finalizePlot(subplt) +# writemime(io, m, getfig(subplt.o)) +# end diff --git a/src/backends/qwt.jl b/src/backends/qwt.jl index 6d85a5d9..c50f7fc6 100644 --- a/src/backends/qwt.jl +++ b/src/backends/qwt.jl @@ -6,58 +6,58 @@ # export qwt # qwt() = backend(:qwt) -# supportedArgs(::QwtPackage) = setdiff(_allArgs, [:xlims, :ylims, :xticks, :yticks]) -supportedArgs(::QwtPackage) = [ - :annotation, - # :args, - :axis, - :background_color, - :color, - :color_palette, - :fillrange, - :fillcolor, - :foreground_color, - :group, - # :heatmap_c, - # :kwargs, - :label, - :layout, - :legend, - :linestyle, - :linetype, - :linewidth, - :markershape, - :markercolor, - :markersize, - :n, - :nbins, - :nc, - :nr, - :pos, - :smooth, - # :ribbon, - :show, - :size, - :title, - :windowtitle, - :x, - :xlabel, - :xlims, - :xticks, - :y, - :ylabel, - :ylims, - :yrightlabel, - :yticks, - :xscale, - :yscale, - # :xflip, - # :yflip, - # :z, - ] -supportedTypes(::QwtPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] -supportedMarkers(::QwtPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :star8, :hexagon] -supportedScales(::QwtPackage) = [:identity, :log10] +# # supportedArgs(::QwtPackage) = setdiff(_allArgs, [:xlims, :ylims, :xticks, :yticks]) +# supportedArgs(::QwtPackage) = [ +# :annotation, +# # :args, +# :axis, +# :background_color, +# :color, +# :color_palette, +# :fillrange, +# :fillcolor, +# :foreground_color, +# :group, +# # :heatmap_c, +# # :kwargs, +# :label, +# :layout, +# :legend, +# :linestyle, +# :linetype, +# :linewidth, +# :markershape, +# :markercolor, +# :markersize, +# :n, +# :nbins, +# :nc, +# :nr, +# :pos, +# :smooth, +# # :ribbon, +# :show, +# :size, +# :title, +# :windowtitle, +# :x, +# :xlabel, +# :xlims, +# :xticks, +# :y, +# :ylabel, +# :ylims, +# :yrightlabel, +# :yticks, +# :xscale, +# :yscale, +# # :xflip, +# # :yflip, +# # :z, +# ] +# supportedTypes(::QwtPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] +# supportedMarkers(::QwtPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :star8, :hexagon] +# supportedScales(::QwtPackage) = [:identity, :log10] # ------------------------------- diff --git a/src/backends/supported.jl b/src/backends/supported.jl new file mode 100644 index 00000000..5303930e --- /dev/null +++ b/src/backends/supported.jl @@ -0,0 +1,309 @@ + +supportedArgs(::GadflyPackage) = [ + :annotation, + # :axis, + :background_color, + :color, + :color_palette, + :fillrange, + :fillcolor, + :fillopacity, + :foreground_color, + :group, + :label, + :layout, + :legend, + :linestyle, + :linetype, + :linewidth, + :lineopacity, + :markershape, + :markercolor, + :markersize, + :markeropacity, + :n, + :nbins, + :nc, + :nr, + # :pos, + :smooth, + :show, + :size, + :title, + :windowtitle, + :x, + :xlabel, + :xlims, + :xticks, + :y, + :ylabel, + :ylims, + # :yrightlabel, + :yticks, + :xscale, + :yscale, + :xflip, + :yflip, + :z, + :tickfont, + :guidefont, + :legendfont, + :grid, + ] +supportedAxes(::GadflyPackage) = [:auto, :left] +supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] +supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] +supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape) +supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt] + + +# -------------------------------------------------------------------------------------- + + +supportedArgs(::ImmersePackage) = supportedArgs(GadflyPackage()) +supportedAxes(::ImmersePackage) = supportedAxes(GadflyPackage()) +supportedTypes(::ImmersePackage) = supportedTypes(GadflyPackage()) +supportedStyles(::ImmersePackage) = supportedStyles(GadflyPackage()) +supportedMarkers(::ImmersePackage) = supportedMarkers(GadflyPackage()) +supportedScales(::ImmersePackage) = supportedScales(GadflyPackage()) + +# -------------------------------------------------------------------------------------- + + + +supportedArgs(::PyPlotPackage) = [ + :annotation, + # :args, + :axis, + :background_color, + :color, + :color_palette, + :fillrange, + :fillcolor, + :foreground_color, + :group, + # :heatmap_c, + # :kwargs, + :label, + :layout, + :legend, + :linestyle, + :linetype, + :linewidth, + :markershape, + :markercolor, + :markersize, + :n, + :nbins, + :nc, + :nr, + # :pos, + # :smooth, + # :ribbon, + :show, + :size, + :title, + :windowtitle, + :x, + :xlabel, + :xlims, + :xticks, + :y, + :ylabel, + :ylims, + :yrightlabel, + :yticks, + :xscale, + :yscale, + :xflip, + :yflip, + :z, + :tickfont, + :guidefont, + :legendfont, + # :grid, + ] +supportedAxes(::PyPlotPackage) = _allAxes +supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] +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) +supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10] +subplotSupported(::PyPlotPackage) = true + + +# -------------------------------------------------------------------------------------- + + + +supportedArgs(::QwtPackage) = [ + :annotation, + # :args, + :axis, + :background_color, + :color, + :color_palette, + :fillrange, + :fillcolor, + :foreground_color, + :group, + # :heatmap_c, + # :kwargs, + :label, + :layout, + :legend, + :linestyle, + :linetype, + :linewidth, + :markershape, + :markercolor, + :markersize, + :n, + :nbins, + :nc, + :nr, + :pos, + :smooth, + # :ribbon, + :show, + :size, + :title, + :windowtitle, + :x, + :xlabel, + :xlims, + :xticks, + :y, + :ylabel, + :ylims, + :yrightlabel, + :yticks, + :xscale, + :yscale, + # :xflip, + # :yflip, + # :z, + ] +supportedTypes(::QwtPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] +supportedMarkers(::QwtPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :star8, :hexagon] +supportedScales(::QwtPackage) = [:identity, :log10] + + +# -------------------------------------------------------------------------------------- + + +supportedArgs(::UnicodePlotsPackage) = [ + # :annotation, + # :args, + # :axis, + # :background_color, + # :color, + # :fill, + # :foreground_color, + :group, + # :heatmap_c, + # :kwargs, + :label, + # :layout, + :legend, + :linestyle, + :linetype, + # :linewidth, + :markershape, + # :markercolor, + # :markersize, + # :n, + :nbins, + # :nc, + # :nr, + # :pos, + # :reg, + # :ribbon, + :show, + :size, + :title, + :windowtitle, + :x, + :xlabel, + :xlims, + # :xticks, + :y, + :ylabel, + :ylims, + # :yrightlabel, + # :yticks, + # :xscale, + # :yscale, + # :xflip, + # :yflip, + # :z, + ] +supportedAxes(::UnicodePlotsPackage) = [:auto, :left] +supportedTypes(::UnicodePlotsPackage) = [:none, :line, :path, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] +supportedStyles(::UnicodePlotsPackage) = [:auto, :solid] +supportedMarkers(::UnicodePlotsPackage) = [:none, :auto, :ellipse] +supportedScales(::UnicodePlotsPackage) = [:identity] + + + + +# -------------------------------------------------------------------------------------- + + +supportedArgs(::WinstonPackage) = [ + :annotation, + # :args, + # :axis, + # :background_color, + :color, + :color_palette, + :fillrange, + :fillcolor, + # :foreground_color, + :group, + # :heatmap_c, + # :kwargs, + :label, + # :layout, + :legend, + :linestyle, + :linetype, + :linewidth, + :markershape, + :markercolor, + :markersize, + # :n, + :nbins, + # :nc, + # :nr, + # :pos, + :smooth, + # :ribbon, + :show, + :size, + :title, + :windowtitle, + :x, + :xlabel, + :xlims, + # :xticks, + :y, + :ylabel, + :ylims, + # :yrightlabel, + # :yticks, + :xscale, + :yscale, + # :xflip, + # :yflip, + # :z, + ] +supportedAxes(::WinstonPackage) = [:auto, :left] +supportedTypes(::WinstonPackage) = [:none, :line, :path, :sticks, :scatter, :hist, :bar] +supportedStyles(::WinstonPackage) = [:auto, :solid, :dash, :dot, :dashdot] +supportedMarkers(::WinstonPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] +supportedScales(::WinstonPackage) = [:identity, :log10] +subplotSupported(::WinstonPackage) = false + + +# -------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/src/backends/unicodeplots.jl b/src/backends/unicodeplots.jl index 5fbefbba..b21a5ba8 100644 --- a/src/backends/unicodeplots.jl +++ b/src/backends/unicodeplots.jl @@ -8,58 +8,58 @@ # ------------------------------- -# supportedArgs(::UnicodePlotsPackage) = setdiff(_allArgs, [:reg, :heatmap_c, :fill, :pos, :xlims, :ylims, :xticks, :yticks]) -supportedArgs(::UnicodePlotsPackage) = [ - # :annotation, - # :args, - # :axis, - # :background_color, - # :color, - # :fill, - # :foreground_color, - :group, - # :heatmap_c, - # :kwargs, - :label, - # :layout, - :legend, - :linestyle, - :linetype, - # :linewidth, - :markershape, - # :markercolor, - # :markersize, - # :n, - :nbins, - # :nc, - # :nr, - # :pos, - # :reg, - # :ribbon, - :show, - :size, - :title, - :windowtitle, - :x, - :xlabel, - :xlims, - # :xticks, - :y, - :ylabel, - :ylims, - # :yrightlabel, - # :yticks, - # :xscale, - # :yscale, - # :xflip, - # :yflip, - # :z, - ] -supportedAxes(::UnicodePlotsPackage) = [:auto, :left] -supportedTypes(::UnicodePlotsPackage) = [:none, :line, :path, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] -supportedStyles(::UnicodePlotsPackage) = [:auto, :solid] -supportedMarkers(::UnicodePlotsPackage) = [:none, :auto, :ellipse] -supportedScales(::UnicodePlotsPackage) = [:identity] +# # supportedArgs(::UnicodePlotsPackage) = setdiff(_allArgs, [:reg, :heatmap_c, :fill, :pos, :xlims, :ylims, :xticks, :yticks]) +# supportedArgs(::UnicodePlotsPackage) = [ +# # :annotation, +# # :args, +# # :axis, +# # :background_color, +# # :color, +# # :fill, +# # :foreground_color, +# :group, +# # :heatmap_c, +# # :kwargs, +# :label, +# # :layout, +# :legend, +# :linestyle, +# :linetype, +# # :linewidth, +# :markershape, +# # :markercolor, +# # :markersize, +# # :n, +# :nbins, +# # :nc, +# # :nr, +# # :pos, +# # :reg, +# # :ribbon, +# :show, +# :size, +# :title, +# :windowtitle, +# :x, +# :xlabel, +# :xlims, +# # :xticks, +# :y, +# :ylabel, +# :ylims, +# # :yrightlabel, +# # :yticks, +# # :xscale, +# # :yscale, +# # :xflip, +# # :yflip, +# # :z, +# ] +# supportedAxes(::UnicodePlotsPackage) = [:auto, :left] +# supportedTypes(::UnicodePlotsPackage) = [:none, :line, :path, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] +# supportedStyles(::UnicodePlotsPackage) = [:auto, :solid] +# supportedMarkers(::UnicodePlotsPackage) = [:none, :auto, :ellipse] +# supportedScales(::UnicodePlotsPackage) = [:identity] diff --git a/src/backends/winston.jl b/src/backends/winston.jl index 28eaa220..2624d4f4 100644 --- a/src/backends/winston.jl +++ b/src/backends/winston.jl @@ -30,60 +30,60 @@ ) -supportedArgs(::WinstonPackage) = [ - :annotation, - # :args, - # :axis, - # :background_color, - :color, - :color_palette, - :fillrange, - :fillcolor, - # :foreground_color, - :group, - # :heatmap_c, - # :kwargs, - :label, - # :layout, - :legend, - :linestyle, - :linetype, - :linewidth, - :markershape, - :markercolor, - :markersize, - # :n, - :nbins, - # :nc, - # :nr, - # :pos, - :smooth, - # :ribbon, - :show, - :size, - :title, - :windowtitle, - :x, - :xlabel, - :xlims, - # :xticks, - :y, - :ylabel, - :ylims, - # :yrightlabel, - # :yticks, - :xscale, - :yscale, - # :xflip, - # :yflip, - # :z, - ] -supportedAxes(::WinstonPackage) = [:auto, :left] -supportedTypes(::WinstonPackage) = [:none, :line, :path, :sticks, :scatter, :hist, :bar] -supportedStyles(::WinstonPackage) = [:auto, :solid, :dash, :dot, :dashdot] -supportedMarkers(::WinstonPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] -supportedScales(::WinstonPackage) = [:identity, :log10] -subplotSupported(::WinstonPackage) = false +# supportedArgs(::WinstonPackage) = [ +# :annotation, +# # :args, +# # :axis, +# # :background_color, +# :color, +# :color_palette, +# :fillrange, +# :fillcolor, +# # :foreground_color, +# :group, +# # :heatmap_c, +# # :kwargs, +# :label, +# # :layout, +# :legend, +# :linestyle, +# :linetype, +# :linewidth, +# :markershape, +# :markercolor, +# :markersize, +# # :n, +# :nbins, +# # :nc, +# # :nr, +# # :pos, +# :smooth, +# # :ribbon, +# :show, +# :size, +# :title, +# :windowtitle, +# :x, +# :xlabel, +# :xlims, +# # :xticks, +# :y, +# :ylabel, +# :ylims, +# # :yrightlabel, +# # :yticks, +# :xscale, +# :yscale, +# # :xflip, +# # :yflip, +# # :z, +# ] +# supportedAxes(::WinstonPackage) = [:auto, :left] +# supportedTypes(::WinstonPackage) = [:none, :line, :path, :sticks, :scatter, :hist, :bar] +# supportedStyles(::WinstonPackage) = [:auto, :solid, :dash, :dot, :dashdot] +# supportedMarkers(::WinstonPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] +# supportedScales(::WinstonPackage) = [:identity, :log10] +# subplotSupported(::WinstonPackage) = false function preparePlotUpdate(plt::Plot{WinstonPackage}) diff --git a/src/output.jl b/src/output.jl new file mode 100644 index 00000000..afc98c25 --- /dev/null +++ b/src/output.jl @@ -0,0 +1,104 @@ + + +defaultOutputFormat(plt::PlottingObject) = "png" + +function png(plt::PlottingObject, fn::@compat(AbstractString)) + fn = addExtension(fn, "png") + io = open(fn, "w") + writemime(io, MIME("image/png"), plt) + close(io) +end +png(fn::@compat(AbstractString)) = png(current(), fn) + +function svg(plt::PlottingObject, fn::@compat(AbstractString)) + fn = addExtension(fn, "svg") + io = open(fn, "w") + writemime(io, MIME("image/svg+xml"), plt) + close(io) +end +svg(fn::@compat(AbstractString)) = svg(current(), fn) + + +function pdf(plt::PlottingObject, fn::@compat(AbstractString)) + fn = addExtension(fn, "pdf") + io = open(fn, "w") + writemime(io, MIME("application/pdf"), plt) + close(io) +end +pdf(fn::@compat(AbstractString)) = pdf(current(), fn) + + +function ps(plt::PlottingObject, fn::@compat(AbstractString)) + fn = addExtension(fn, "ps") + io = open(fn, "w") + writemime(io, MIME("application/postscript"), plt) + close(io) +end +ps(fn::@compat(AbstractString)) = ps(current(), fn) + + +# ---------------------------------------------------------------- + + +@compat const _savemap = Dict( + "png" => png, + "svg" => svg, + "pdf" => pdf, + "ps" => ps, + ) + +function getExtension(fn::@compat(AbstractString)) + pieces = split(fn, ".") + length(pieces) > 1 || error("Can't extract file extension: ", fn) + ext = pieces[end] + haskey(_savemap, ext) || error("Invalid file extension: ", fn) + ext +end + +function addExtension(fn::@compat(AbstractString), ext::@compat(AbstractString)) + try + oldext = getExtension(fn) + if oldext == ext + return fn + else + return "$fn.$ext" + end + catch + return "$fn.$ext" + end +end + +function savefig(plt::PlottingObject, fn::@compat(AbstractString)) + + # get the extension + local ext + try + ext = getExtension(fn) + catch + # if we couldn't extract the extension, add the default + ext = defaultOutputFormat(plt) + fn = addExtension(fn, ext) + end + + # save it + func = get(_savemap, ext) do + error("Unsupported extension $ext with filename ", fn) + end + func(plt, fn) +end +savefig(fn::@compat(AbstractString)) = savefig(current(), fn) + + +# savepng(args...; kw...) = savepng(current(), args...; kw...) +# savepng(plt::PlottingObject, fn::@compat(AbstractString); kw...) = (io = open(fn, "w"); writemime(io, MIME("image/png"), plt); close(io)) + + + + +# --------------------------------------------------------- + +gui(plt::PlottingObject = current()) = display(PlotsDisplay(), plt) + + +# override the REPL display to open a gui window +Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::PlottingObject) = gui(plt) diff --git a/src/plot.jl b/src/plot.jl index ebc6508b..e49ca451 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -57,6 +57,8 @@ function plot(args...; kw...) plt = plot(pkg; getPlotArgs(pkg, d, 1)...) # create a new, blank plot delete!(d, :background_color) + # print(d) + # return plt plot!(plt, args...; d...) # add to it end @@ -80,6 +82,9 @@ end # this adds to a specific plot... most plot commands will flow through here function plot!(plt::Plot, args...; kw...) + # println(plt) + # println(args) + # println(kw) d = Dict(kw) preprocessArgs!(d) diff --git a/src/plotter.jl b/src/plotter.jl index b1fbdb8d..6f8cb554 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -7,6 +7,8 @@ immutable QwtPackage <: PlottingPackage end immutable UnicodePlotsPackage <: PlottingPackage end immutable WinstonPackage <: PlottingPackage end +typealias GadflyOrImmerse @compat(Union{GadflyPackage, ImmersePackage}) + export gadfly, immerse, @@ -22,6 +24,7 @@ qwt() = backend(:qwt) unicodeplots() = backend(:unicodeplots) winston() = backend(:winston) +include("backends/supported.jl") include("backends/qwt.jl") include("backends/gadfly.jl") @@ -114,6 +117,7 @@ end Returns the current plotting package name. Initializes package on first call. """ function backend() + # error() currentBackendSymbol = CURRENT_BACKEND.sym if !(currentBackendSymbol in INITIALIZED_BACKENDS) @@ -125,16 +129,18 @@ function backend() @eval import Qwt @eval export Qwt catch err - error("Couldn't import Qwt. Install it with: Pkg.clone(\"https://github.com/tbreloff/Qwt.jl.git\")\n (Note: also requires pyqt and pyqwt).\n Error: ", err) + warn("Couldn't import Qwt. Install it with: Pkg.clone(\"https://github.com/tbreloff/Qwt.jl.git\")\n (Note: also requires pyqt and pyqwt).") + rethrow(err) end elseif currentBackendSymbol == :gadfly try - @eval import Gadfly, Compose - @eval export Gadfly, Compose + @eval import Gadfly, Compose, DataFrames + @eval export Gadfly, Compose, DataFrames @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "gadfly_shapes.jl")) catch err - error("Couldn't import Gadfly. Install it with: Pkg.add(\"Gadfly\").\n Error: ", err) + warn("Couldn't import Gadfly. Install it with: Pkg.add(\"Gadfly\").") + rethrow(err) end elseif currentBackendSymbol == :unicodeplots @@ -142,7 +148,8 @@ function backend() @eval import UnicodePlots @eval export UnicodePlots catch err - error("Couldn't import UnicodePlots. Install it with: Pkg.add(\"UnicodePlots\").\n Error: ", err) + warn("Couldn't import UnicodePlots. Install it with: Pkg.add(\"UnicodePlots\").") + rethrow(err) end elseif currentBackendSymbol == :pyplot @@ -154,7 +161,8 @@ function backend() PyPlot.ioff() end catch err - error("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\").\n Error: ", err) + warn("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\").") + rethrow(err) end elseif currentBackendSymbol == :immerse @@ -163,7 +171,9 @@ function backend() @eval export Immerse, Gadfly, Compose, Gtk @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "gadfly_shapes.jl")) catch err - error("Couldn't import Immerse. Install it with: Pkg.add(\"Immerse\").\n Error: ", err) + # error("Couldn't import Immerse. Install it with: Pkg.add(\"Immerse\").\n Error: ", err) + warn("Couldn't import Immerse. Install it with: Pkg.add(\"Immerse\").") + rethrow(err) end elseif currentBackendSymbol == :winston @@ -171,16 +181,15 @@ function backend() @eval ENV["WINSTON_OUTPUT"] = "gtk" @eval import Winston, Gtk @eval export Winston, Gtk - #@eval Winston._winston_config.defaults["output_surface"] = "gtk" catch err - error("Couldn't import Winston. Install it with: Pkg.add(\"Winston\").\n Error: ", err) + warn("Couldn't import Winston. Install it with: Pkg.add(\"Winston\").") + rethrow(err) end else error("Unknown backend $currentBackendSymbol. Choose from: $BACKENDS") end push!(INITIALIZED_BACKENDS, currentBackendSymbol) - # println("[Plots.jl] done.") end CURRENT_BACKEND.pkg diff --git a/test/imgcomp.jl b/test/imgcomp.jl index fbd138db..f9afbbc0 100644 --- a/test/imgcomp.jl +++ b/test/imgcomp.jl @@ -51,7 +51,7 @@ function isTempImageCorrect(tmpfn, reffn) end -function image_comparison_tests(pkg::Symbol, idx::Int; debug = true) +function image_comparison_tests(pkg::Symbol, idx::Int; debug = true, sigma = [0,0], eps = 1e-3) # first Plots._debugMode.on = debug @@ -72,7 +72,25 @@ function image_comparison_tests(pkg::Symbol, idx::Int; debug = true) refimg = imread(reffn) # run the test - @test_approx_eq_sigma_eps(tmpimg, refimg, sigma, eps) + # NOTE: sigma is a 2-length vector with x/y values for the number of pixels + # to blur together when comparing images + try + + # run the comparison test... a difference will throw an error + @test_approx_eq_sigma_eps(tmpimg, refimg, sigma, eps) + + catch ex + if isinteractive() + + # if we're in interactive mode, open a popup and give us a chance to examine the images + if isTempImageCorrect(tmpfn, reffn) + return + end + end + + # if we rejected the image, or if we're in automated tests, throw the error + throw(ex) + end end function image_comparison_tests(pkg::Symbol; debug = false)