pdf/ps/svg outputs

This commit is contained in:
Thomas Breloff 2015-10-19 14:11:22 -04:00
parent dcd50a7c62
commit 8208f6947f
13 changed files with 809 additions and 396 deletions

View File

@ -29,6 +29,8 @@ Please add wishlist items, bugs, or any other comments/questions to the issues l
- [Qwt.jl](docs/qwt_examples.md) - [Qwt.jl](docs/qwt_examples.md)
- [Winston.jl](docs/winston_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 ## Installation
First, add the package First, add the package
@ -84,9 +86,6 @@ png("gadfly1")
![gadfly_plt](img/gadfly1.png) ![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 ## API
Call `backend(backend::Symbol)` or the shorthands (`gadfly()`, `qwt()`, `unicodeplots()`, etc) to set the current plotting backend. Call `backend(backend::Symbol)` or the shorthands (`gadfly()`, `qwt()`, `unicodeplots()`, etc) to set the current plotting backend.

View File

@ -110,6 +110,7 @@ include("plot.jl")
include("subplot.jl") include("subplot.jl")
include("recipes.jl") include("recipes.jl")
include("animation.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...) 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)
# --------------------------------------------------------- # ---------------------------------------------------------

View File

@ -7,70 +7,70 @@
# gadfly() = backend(:gadfly) # gadfly() = backend(:gadfly)
# supportedArgs(::GadflyPackage) = setdiff(_allArgs, [:heatmap_c, :pos, :screen, :yrightlabel]) # # supportedArgs(::GadflyPackage) = setdiff(_allArgs, [:heatmap_c, :pos, :screen, :yrightlabel])
supportedArgs(::GadflyPackage) = [ # supportedArgs(::GadflyPackage) = [
:annotation, # :annotation,
# :axis, # # :axis,
:background_color, # :background_color,
:color, # :color,
:color_palette, # :color_palette,
:fillrange, # :fillrange,
:fillcolor, # :fillcolor,
:fillopacity, # :fillopacity,
:foreground_color, # :foreground_color,
:group, # :group,
:label, # :label,
:layout, # :layout,
:legend, # :legend,
:linestyle, # :linestyle,
:linetype, # :linetype,
:linewidth, # :linewidth,
:lineopacity, # :lineopacity,
:markershape, # :markershape,
:markercolor, # :markercolor,
:markersize, # :markersize,
:markeropacity, # :markeropacity,
:n, # :n,
:nbins, # :nbins,
:nc, # :nc,
:nr, # :nr,
# :pos, # # :pos,
:smooth, # :smooth,
:show, # :show,
:size, # :size,
:title, # :title,
:windowtitle, # :windowtitle,
:x, # :x,
:xlabel, # :xlabel,
:xlims, # :xlims,
:xticks, # :xticks,
:y, # :y,
:ylabel, # :ylabel,
:ylims, # :ylims,
# :yrightlabel, # # :yrightlabel,
:yticks, # :yticks,
:xscale, # :xscale,
:yscale, # :yscale,
:xflip, # :xflip,
:yflip, # :yflip,
:z, # :z,
:tickfont, # :tickfont,
:guidefont, # :guidefont,
:legendfont, # :legendfont,
:grid, # :grid,
] # ]
supportedAxes(::GadflyPackage) = [:auto, :left] # supportedAxes(::GadflyPackage) = [:auto, :left]
supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] # supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc]
supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] # supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape) # supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape)
supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt] # supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt]
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
function createGadflyPlotObject(d::Dict) function createGadflyPlotObject(d::Dict)
@eval import DataFrames # @eval import DataFrames
gplt = Gadfly.Plot() gplt = Gadfly.Plot()
gplt.mapping = Dict() gplt.mapping = Dict()
@ -588,7 +588,7 @@ end
# create the underlying object (each backend will do this differently) # create the underlying object (each backend will do this differently)
function buildSubplotObject!(subplt::Subplot{GadflyPackage}, isbefore::Bool) function buildSubplotObject!(subplt::Subplot{GadflyPackage}, isbefore::Bool)
isbefore && return false isbefore && return false # wait until after plotting to create the subplots
subplt.o = nothing subplt.o = nothing
true true
end end
@ -637,12 +637,30 @@ function setGadflyDisplaySize(w,h)
Compose.set_default_graphic_size(w * Compose.px, h * Compose.px) Compose.set_default_graphic_size(w * Compose.px, h * Compose.px)
end 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) gplt = getGadflyContext(plt)
setGadflyDisplaySize(plt.initargs[:size]...) 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 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}) function Base.display(::PlotsDisplay, plt::Plot{GadflyPackage})
setGadflyDisplaySize(plt.initargs[:size]...) 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}) function Base.display(::PlotsDisplay, subplt::Subplot{GadflyPackage})
setGadflyDisplaySize(subplt.initargs[1][:size]...) setGadflyDisplaySize(subplt.initargs[1][:size]...)
ctx = buildGadflySubplotContext(subplt) ctx = buildGadflySubplotContext(subplt)

View File

@ -7,12 +7,12 @@
# immerse() = backend(:immerse) # immerse() = backend(:immerse)
supportedArgs(::ImmersePackage) = supportedArgs(GadflyPackage()) # supportedArgs(::ImmersePackage) = supportedArgs(GadflyPackage())
supportedAxes(::ImmersePackage) = supportedAxes(GadflyPackage()) # supportedAxes(::ImmersePackage) = supportedAxes(GadflyPackage())
supportedTypes(::ImmersePackage) = supportedTypes(GadflyPackage()) # supportedTypes(::ImmersePackage) = supportedTypes(GadflyPackage())
supportedStyles(::ImmersePackage) = supportedStyles(GadflyPackage()) # supportedStyles(::ImmersePackage) = supportedStyles(GadflyPackage())
supportedMarkers(::ImmersePackage) = supportedMarkers(GadflyPackage()) # supportedMarkers(::ImmersePackage) = supportedMarkers(GadflyPackage())
supportedScales(::ImmersePackage) = supportedScales(GadflyPackage()) # supportedScales(::ImmersePackage) = supportedScales(GadflyPackage())
function createImmerseFigure(d::Dict) function createImmerseFigure(d::Dict)
@ -150,11 +150,17 @@ end
getGadflyContext(plt::Plot{ImmersePackage}) = plt.o[2] getGadflyContext(plt::Plot{ImmersePackage}) = plt.o[2]
getGadflyContext(subplt::Subplot{ImmersePackage}) = buildGadflySubplotContext(subplt) getGadflyContext(subplt::Subplot{ImmersePackage}) = buildGadflySubplotContext(subplt)
function Base.writemime(io::IO, ::MIME"image/png", plt::Plot{ImmersePackage}) # function Base.writemime(io::IO, ::MIME"image/png", plt::Plot{ImmersePackage})
gplt = getGadflyContext(plt) # gplt = getGadflyContext(plt)
setGadflyDisplaySize(plt.initargs[:size]...) # setGadflyDisplaySize(plt.initargs[:size]...)
Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) # Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt)
end # 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}) function Base.display(::PlotsDisplay, plt::Plot{ImmersePackage})
@ -170,11 +176,18 @@ function Base.display(::PlotsDisplay, plt::Plot{ImmersePackage})
end end
function Base.writemime(io::IO, ::MIME"image/png", plt::Subplot{ImmersePackage}) # function Base.writemime(io::IO, ::MIME"image/png", plt::Subplot{ImmersePackage})
gplt = getGadflyContext(plt) # gplt = getGadflyContext(plt)
setGadflyDisplaySize(plt.initargs[1][:size]...) # setGadflyDisplaySize(plt.initargs[1][:size]...)
Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt) # Gadfly.draw(Gadfly.PNG(io, Compose.default_graphic_width, Compose.default_graphic_height), gplt)
end # 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}) function Base.display(::PlotsDisplay, subplt::Subplot{ImmersePackage})

View File

@ -8,65 +8,65 @@
# ------------------------------- # -------------------------------
supportedArgs(::PyPlotPackage) = [ # supportedArgs(::PyPlotPackage) = [
:annotation, # :annotation,
# :args, # # :args,
:axis, # :axis,
:background_color, # :background_color,
:color, # :color,
:color_palette, # :color_palette,
:fillrange, # :fillrange,
:fillcolor, # :fillcolor,
:foreground_color, # :foreground_color,
:group, # :group,
# :heatmap_c, # # :heatmap_c,
# :kwargs, # # :kwargs,
:label, # :label,
:layout, # :layout,
:legend, # :legend,
:linestyle, # :linestyle,
:linetype, # :linetype,
:linewidth, # :linewidth,
:markershape, # :markershape,
:markercolor, # :markercolor,
:markersize, # :markersize,
:n, # :n,
:nbins, # :nbins,
:nc, # :nc,
:nr, # :nr,
# :pos, # # :pos,
# :smooth, # # :smooth,
# :ribbon, # # :ribbon,
:show, # :show,
:size, # :size,
:title, # :title,
:windowtitle, # :windowtitle,
:x, # :x,
:xlabel, # :xlabel,
:xlims, # :xlims,
:xticks, # :xticks,
:y, # :y,
:ylabel, # :ylabel,
:ylims, # :ylims,
:yrightlabel, # :yrightlabel,
:yticks, # :yticks,
:xscale, # :xscale,
:yscale, # :yscale,
:xflip, # :xflip,
:yflip, # :yflip,
:z, # :z,
:tickfont, # :tickfont,
:guidefont, # :guidefont,
:legendfont, # :legendfont,
# :grid, # # :grid,
] # ]
supportedAxes(::PyPlotPackage) = _allAxes # supportedAxes(::PyPlotPackage) = _allAxes
supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] # supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline]
supportedStyles(::PyPlotPackage) = [:auto, :solid, :dash, :dot, :dashdot] # supportedStyles(::PyPlotPackage) = [:auto, :solid, :dash, :dot, :dashdot]
# supportedMarkers(::PyPlotPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :hexagon] # # supportedMarkers(::PyPlotPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :hexagon]
supportedMarkers(::PyPlotPackage) = vcat(_allMarkers, Shape) # supportedMarkers(::PyPlotPackage) = vcat(_allMarkers, Shape)
supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10] # supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10]
subplotSupported(::PyPlotPackage) = true # subplotSupported(::PyPlotPackage) = true
# convert colorant to 4-tuple RGBA # convert colorant to 4-tuple RGBA
getPyPlotColor(c::Colorant) = map(f->float(f(c)), (red, green, blue, alpha)) getPyPlotColor(c::Colorant) = map(f->float(f(c)), (red, green, blue, alpha))
@ -556,10 +556,18 @@ function finalizePlot(plt::Plot{PyPlotPackage})
PyPlot.draw() PyPlot.draw()
end end
function Base.writemime(io::IO, m::MIME"image/png", plt::Plot{PyPlotPackage}) # # allow for writing any supported mime
finalizePlot(plt) # for mime in keys(PyPlot.aggformats)
writemime(io, m, getfig(plt.o)) # @eval function Base.writemime(io::IO, m::MIME{symbol{$mime}}, plt::Plot{PyPlotPackage})
end # 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}) function Base.display(::PlotsDisplay, plt::Plot{PyPlotPackage})
@ -580,8 +588,16 @@ function Base.display(::PlotsDisplay, subplt::Subplot{PyPlotPackage})
display(getfig(subplt.o)) display(getfig(subplt.o))
end end
# allow for writing any supported mime
function Base.writemime(io::IO, m::MIME"image/png", subplt::Subplot{PyPlotPackage}) for mime in (MIME"image/png", MIME"image/svg+xml", MIME"text/html", MIME"application/pdf", MIME"application/postscript")
finalizePlot(subplt) @eval function Base.writemime(io::IO, ::$mime, plt::PlottingObject{PyPlotPackage})
writemime(io, m, getfig(subplt.o)) finalizePlot(plt)
writemime(io, $mime(), getfig(plt.o))
end
end end
# function Base.writemime(io::IO, m::MIME"image/png", subplt::Subplot{PyPlotPackage})
# finalizePlot(subplt)
# writemime(io, m, getfig(subplt.o))
# end

View File

@ -6,58 +6,58 @@
# export qwt # export qwt
# qwt() = backend(:qwt) # qwt() = backend(:qwt)
# supportedArgs(::QwtPackage) = setdiff(_allArgs, [:xlims, :ylims, :xticks, :yticks]) # # supportedArgs(::QwtPackage) = setdiff(_allArgs, [:xlims, :ylims, :xticks, :yticks])
supportedArgs(::QwtPackage) = [ # supportedArgs(::QwtPackage) = [
:annotation, # :annotation,
# :args, # # :args,
:axis, # :axis,
:background_color, # :background_color,
:color, # :color,
:color_palette, # :color_palette,
:fillrange, # :fillrange,
:fillcolor, # :fillcolor,
:foreground_color, # :foreground_color,
:group, # :group,
# :heatmap_c, # # :heatmap_c,
# :kwargs, # # :kwargs,
:label, # :label,
:layout, # :layout,
:legend, # :legend,
:linestyle, # :linestyle,
:linetype, # :linetype,
:linewidth, # :linewidth,
:markershape, # :markershape,
:markercolor, # :markercolor,
:markersize, # :markersize,
:n, # :n,
:nbins, # :nbins,
:nc, # :nc,
:nr, # :nr,
:pos, # :pos,
:smooth, # :smooth,
# :ribbon, # # :ribbon,
:show, # :show,
:size, # :size,
:title, # :title,
:windowtitle, # :windowtitle,
:x, # :x,
:xlabel, # :xlabel,
:xlims, # :xlims,
:xticks, # :xticks,
:y, # :y,
:ylabel, # :ylabel,
:ylims, # :ylims,
:yrightlabel, # :yrightlabel,
:yticks, # :yticks,
:xscale, # :xscale,
:yscale, # :yscale,
# :xflip, # # :xflip,
# :yflip, # # :yflip,
# :z, # # :z,
] # ]
supportedTypes(::QwtPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] # 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] # supportedMarkers(::QwtPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :star8, :hexagon]
supportedScales(::QwtPackage) = [:identity, :log10] # supportedScales(::QwtPackage) = [:identity, :log10]
# ------------------------------- # -------------------------------

309
src/backends/supported.jl Normal file
View File

@ -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
# --------------------------------------------------------------------------------------

View File

@ -8,58 +8,58 @@
# ------------------------------- # -------------------------------
# supportedArgs(::UnicodePlotsPackage) = setdiff(_allArgs, [:reg, :heatmap_c, :fill, :pos, :xlims, :ylims, :xticks, :yticks]) # # supportedArgs(::UnicodePlotsPackage) = setdiff(_allArgs, [:reg, :heatmap_c, :fill, :pos, :xlims, :ylims, :xticks, :yticks])
supportedArgs(::UnicodePlotsPackage) = [ # supportedArgs(::UnicodePlotsPackage) = [
# :annotation, # # :annotation,
# :args, # # :args,
# :axis, # # :axis,
# :background_color, # # :background_color,
# :color, # # :color,
# :fill, # # :fill,
# :foreground_color, # # :foreground_color,
:group, # :group,
# :heatmap_c, # # :heatmap_c,
# :kwargs, # # :kwargs,
:label, # :label,
# :layout, # # :layout,
:legend, # :legend,
:linestyle, # :linestyle,
:linetype, # :linetype,
# :linewidth, # # :linewidth,
:markershape, # :markershape,
# :markercolor, # # :markercolor,
# :markersize, # # :markersize,
# :n, # # :n,
:nbins, # :nbins,
# :nc, # # :nc,
# :nr, # # :nr,
# :pos, # # :pos,
# :reg, # # :reg,
# :ribbon, # # :ribbon,
:show, # :show,
:size, # :size,
:title, # :title,
:windowtitle, # :windowtitle,
:x, # :x,
:xlabel, # :xlabel,
:xlims, # :xlims,
# :xticks, # # :xticks,
:y, # :y,
:ylabel, # :ylabel,
:ylims, # :ylims,
# :yrightlabel, # # :yrightlabel,
# :yticks, # # :yticks,
# :xscale, # # :xscale,
# :yscale, # # :yscale,
# :xflip, # # :xflip,
# :yflip, # # :yflip,
# :z, # # :z,
] # ]
supportedAxes(::UnicodePlotsPackage) = [:auto, :left] # supportedAxes(::UnicodePlotsPackage) = [:auto, :left]
supportedTypes(::UnicodePlotsPackage) = [:none, :line, :path, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] # supportedTypes(::UnicodePlotsPackage) = [:none, :line, :path, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline]
supportedStyles(::UnicodePlotsPackage) = [:auto, :solid] # supportedStyles(::UnicodePlotsPackage) = [:auto, :solid]
supportedMarkers(::UnicodePlotsPackage) = [:none, :auto, :ellipse] # supportedMarkers(::UnicodePlotsPackage) = [:none, :auto, :ellipse]
supportedScales(::UnicodePlotsPackage) = [:identity] # supportedScales(::UnicodePlotsPackage) = [:identity]

View File

@ -30,60 +30,60 @@
) )
supportedArgs(::WinstonPackage) = [ # supportedArgs(::WinstonPackage) = [
:annotation, # :annotation,
# :args, # # :args,
# :axis, # # :axis,
# :background_color, # # :background_color,
:color, # :color,
:color_palette, # :color_palette,
:fillrange, # :fillrange,
:fillcolor, # :fillcolor,
# :foreground_color, # # :foreground_color,
:group, # :group,
# :heatmap_c, # # :heatmap_c,
# :kwargs, # # :kwargs,
:label, # :label,
# :layout, # # :layout,
:legend, # :legend,
:linestyle, # :linestyle,
:linetype, # :linetype,
:linewidth, # :linewidth,
:markershape, # :markershape,
:markercolor, # :markercolor,
:markersize, # :markersize,
# :n, # # :n,
:nbins, # :nbins,
# :nc, # # :nc,
# :nr, # # :nr,
# :pos, # # :pos,
:smooth, # :smooth,
# :ribbon, # # :ribbon,
:show, # :show,
:size, # :size,
:title, # :title,
:windowtitle, # :windowtitle,
:x, # :x,
:xlabel, # :xlabel,
:xlims, # :xlims,
# :xticks, # # :xticks,
:y, # :y,
:ylabel, # :ylabel,
:ylims, # :ylims,
# :yrightlabel, # # :yrightlabel,
# :yticks, # # :yticks,
:xscale, # :xscale,
:yscale, # :yscale,
# :xflip, # # :xflip,
# :yflip, # # :yflip,
# :z, # # :z,
] # ]
supportedAxes(::WinstonPackage) = [:auto, :left] # supportedAxes(::WinstonPackage) = [:auto, :left]
supportedTypes(::WinstonPackage) = [:none, :line, :path, :sticks, :scatter, :hist, :bar] # supportedTypes(::WinstonPackage) = [:none, :line, :path, :sticks, :scatter, :hist, :bar]
supportedStyles(::WinstonPackage) = [:auto, :solid, :dash, :dot, :dashdot] # supportedStyles(::WinstonPackage) = [:auto, :solid, :dash, :dot, :dashdot]
supportedMarkers(::WinstonPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] # supportedMarkers(::WinstonPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5]
supportedScales(::WinstonPackage) = [:identity, :log10] # supportedScales(::WinstonPackage) = [:identity, :log10]
subplotSupported(::WinstonPackage) = false # subplotSupported(::WinstonPackage) = false
function preparePlotUpdate(plt::Plot{WinstonPackage}) function preparePlotUpdate(plt::Plot{WinstonPackage})

104
src/output.jl Normal file
View File

@ -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)

View File

@ -57,6 +57,8 @@ function plot(args...; kw...)
plt = plot(pkg; getPlotArgs(pkg, d, 1)...) # create a new, blank plot plt = plot(pkg; getPlotArgs(pkg, d, 1)...) # create a new, blank plot
delete!(d, :background_color) delete!(d, :background_color)
# print(d)
# return plt
plot!(plt, args...; d...) # add to it plot!(plt, args...; d...) # add to it
end end
@ -80,6 +82,9 @@ end
# this adds to a specific plot... most plot commands will flow through here # this adds to a specific plot... most plot commands will flow through here
function plot!(plt::Plot, args...; kw...) function plot!(plt::Plot, args...; kw...)
# println(plt)
# println(args)
# println(kw)
d = Dict(kw) d = Dict(kw)
preprocessArgs!(d) preprocessArgs!(d)

View File

@ -7,6 +7,8 @@ immutable QwtPackage <: PlottingPackage end
immutable UnicodePlotsPackage <: PlottingPackage end immutable UnicodePlotsPackage <: PlottingPackage end
immutable WinstonPackage <: PlottingPackage end immutable WinstonPackage <: PlottingPackage end
typealias GadflyOrImmerse @compat(Union{GadflyPackage, ImmersePackage})
export export
gadfly, gadfly,
immerse, immerse,
@ -22,6 +24,7 @@ qwt() = backend(:qwt)
unicodeplots() = backend(:unicodeplots) unicodeplots() = backend(:unicodeplots)
winston() = backend(:winston) winston() = backend(:winston)
include("backends/supported.jl")
include("backends/qwt.jl") include("backends/qwt.jl")
include("backends/gadfly.jl") include("backends/gadfly.jl")
@ -114,6 +117,7 @@ end
Returns the current plotting package name. Initializes package on first call. Returns the current plotting package name. Initializes package on first call.
""" """
function backend() function backend()
# error()
currentBackendSymbol = CURRENT_BACKEND.sym currentBackendSymbol = CURRENT_BACKEND.sym
if !(currentBackendSymbol in INITIALIZED_BACKENDS) if !(currentBackendSymbol in INITIALIZED_BACKENDS)
@ -125,16 +129,18 @@ function backend()
@eval import Qwt @eval import Qwt
@eval export Qwt @eval export Qwt
catch err 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 end
elseif currentBackendSymbol == :gadfly elseif currentBackendSymbol == :gadfly
try try
@eval import Gadfly, Compose @eval import Gadfly, Compose, DataFrames
@eval export Gadfly, Compose @eval export Gadfly, Compose, DataFrames
@eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "gadfly_shapes.jl")) @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "gadfly_shapes.jl"))
catch err 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 end
elseif currentBackendSymbol == :unicodeplots elseif currentBackendSymbol == :unicodeplots
@ -142,7 +148,8 @@ function backend()
@eval import UnicodePlots @eval import UnicodePlots
@eval export UnicodePlots @eval export UnicodePlots
catch err 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 end
elseif currentBackendSymbol == :pyplot elseif currentBackendSymbol == :pyplot
@ -154,7 +161,8 @@ function backend()
PyPlot.ioff() PyPlot.ioff()
end end
catch err 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 end
elseif currentBackendSymbol == :immerse elseif currentBackendSymbol == :immerse
@ -163,7 +171,9 @@ function backend()
@eval export Immerse, Gadfly, Compose, Gtk @eval export Immerse, Gadfly, Compose, Gtk
@eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "gadfly_shapes.jl")) @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "gadfly_shapes.jl"))
catch err 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 end
elseif currentBackendSymbol == :winston elseif currentBackendSymbol == :winston
@ -171,16 +181,15 @@ function backend()
@eval ENV["WINSTON_OUTPUT"] = "gtk" @eval ENV["WINSTON_OUTPUT"] = "gtk"
@eval import Winston, Gtk @eval import Winston, Gtk
@eval export Winston, Gtk @eval export Winston, Gtk
#@eval Winston._winston_config.defaults["output_surface"] = "gtk"
catch err 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 end
else else
error("Unknown backend $currentBackendSymbol. Choose from: $BACKENDS") error("Unknown backend $currentBackendSymbol. Choose from: $BACKENDS")
end end
push!(INITIALIZED_BACKENDS, currentBackendSymbol) push!(INITIALIZED_BACKENDS, currentBackendSymbol)
# println("[Plots.jl] done.")
end end
CURRENT_BACKEND.pkg CURRENT_BACKEND.pkg

View File

@ -51,7 +51,7 @@ function isTempImageCorrect(tmpfn, reffn)
end 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 # first
Plots._debugMode.on = debug Plots._debugMode.on = debug
@ -72,7 +72,25 @@ function image_comparison_tests(pkg::Symbol, idx::Int; debug = true)
refimg = imread(reffn) refimg = imread(reffn)
# run the test # 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 end
function image_comparison_tests(pkg::Symbol; debug = false) function image_comparison_tests(pkg::Symbol; debug = false)