Fix plotting imports and tests

pull/86/head
Matti Pastell 2017-03-14 09:27:16 +02:00
parent 1b5c0f819c
commit bbad66d0b8
8 changed files with 40 additions and 21 deletions

View File

@ -1,10 +1,10 @@
using Gadfly
import Gadfly
Gadfly.set_default_plot_format(:svg)
#Gadfly doesn't call the default display methods, this catches
#all Gadfly plots
function Base.display(report::Report, m::MIME"image/svg+xml", p::Plot)
function Base.display(report::Report, m::MIME"image/svg+xml", p::Gadfly.Plot)
chunk = report.cur_chunk
full_name, rel_name = get_figname(report, chunk)
@ -14,28 +14,28 @@ function Base.display(report::Report, m::MIME"image/svg+xml", p::Plot)
report.fignum += 1
w = chunk.options[:fig_width]inch
h = chunk.options[:fig_height]inch
w = chunk.options[:fig_width]Gadfly.inch
h = chunk.options[:fig_height]Gadfly.inch
format = chunk.options[:fig_ext]
dpi = chunk.options[:dpi]
#This is probably not the correct way to handle different formats, but it works.
if format == ".png"
try
draw(PNG(full_name, w, h, dpi=dpi), p)
Gadfly.draw(Gadfly.PNG(full_name, w, h, dpi=dpi), p)
catch
draw(PNG(full_name, w, h), p) #Compose < 0.3.1, Gadfly < 0.3.1
Gadfly.draw(Gadfly.PNG(full_name, w, h), p) #Compose < 0.3.1, Gadfly < 0.3.1
end
elseif format == ".pdf"
draw(PDF(full_name, w, h), p)
Gadfly.draw(Gadfly.PDF(full_name, w, h), p)
elseif format == ".ps"
draw(PS(full_name, w, h), p)
Gadfly.draw(Gadfly.PS(full_name, w, h), p)
elseif format == ".svg"
draw(SVG(full_name, w, h), p)
Gadfly.draw(Gadfly.SVG(full_name, w, h), p)
elseif format == ".js.svg"
draw(SVGJS(full_name, w, h), p)
Gadfly.draw(Gadfly.SVGJS(full_name, w, h), p)
elseif format == ".tex"
draw(PGF(full_name, w, h, true ), p)
Gadfly.draw(Gadfly.PGF(full_name, w, h, true ), p)
else
warn("Can't save figure. Unsupported format")
end

View File

@ -1,4 +1,4 @@
using PyPlot
import PyPlot
function savefigs_pyplot(report::Report)
chunk = report.cur_chunk
@ -9,12 +9,12 @@ function savefigs_pyplot(report::Report)
chunkid = (chunk.options[:name] == nothing) ? chunk.number : chunk.options[:name]
#Iterate over all open figures, save them and store names
for fig = plt[:get_fignums]()
for fig = PyPlot.plt[:get_fignums]()
full_name, rel_name = get_figname(report, chunk, fignum=fig)
savefig(full_name, dpi=chunk.options[:dpi])
PyPlot.savefig(full_name, dpi=chunk.options[:dpi])
push!(report.figures, rel_name)
report.fignum += 1
plt[:draw]()
plt[:close]()
PyPlot.plt[:draw]()
PyPlot.plt[:close]()
end
end

View File

@ -487,12 +487,14 @@ function collect_results(chunk::CodeChunk, fmt::CollectResult)
end
function detect_plotlib(chunk::CodeChunk)
if isdefined(:Plots)
init_plotting("Plots")
#Need to set size before plots are created
plots_set_size(chunk)
return
end
isdefined(:PyPlot) && init_plotting("PyPlot") && return
isdefined(:Gadfly) && init_plotting("Gadfly") && return
end

View File

@ -25,6 +25,8 @@ julia> plot(x, sin(x))
~~~~~~~~~~~~~
![](figures/plotsjl_test_gr_2_1.png)\
~~~~{.julia}
plot(rand(100) / 3,reg=true,fill=(0,:green))
@ -32,15 +34,27 @@ scatter!(rand(100),markersize=6,c=:orange)
~~~~~~~~~~~~~
![](figures/plotsjl_test_gr_3_1.png)\
~~~~{.julia}
julia> plot(rand(100) / 3,reg=true,fill=(0,:green))
~~~~~~~~~~~~~
![](figures/plotsjl_test_gr_4_1.png)\
~~~~{.julia}
julia> scatter!(rand(100),markersize=6,c=:orange)
~~~~~~~~~~~~~
![](figures/plotsjl_test_gr_4_2.png)\
![A random walk.](figures/plotsjl_test_gr_random_1.png)

View File

@ -3,7 +3,7 @@ using Weave
using Base.Test
weave("documents/gadfly_formats_test.txt", "tex")
weave("documents/gadfly_formats_test.txt", doctype = "tex", plotlib="gadfly")
result = readstring("documents/gadfly_formats_test.tex")
ref = readstring("documents/gadfly_formats_test_ref.tex")
@test result == ref

View File

@ -1,5 +1,6 @@
using Weave
using Base.Test
import Plots
function pljtest(source, resfile, doctype)
weave("documents/$source", out_path = "documents/plotsjl/$resfile", doctype=doctype)

View File

@ -1,5 +1,6 @@
using Weave
using Base.Test
import Plots
function publish_test(outfile, format)
outfile = joinpath("documents/publish", outfile)

View File

@ -1,3 +1,4 @@
import PyPlot
using Weave
using Base.Test
@ -13,7 +14,7 @@ include("formatter_test.jl")
if VERSION < v"0.6-"
info("Testing rich output")
include("rich_output.jl")
info("Test: Caching")
include("cache_test.jl")
@ -26,7 +27,7 @@ if VERSION < v"0.6-"
info("Test: Weaving with PyPlot")
include("pyplot_formats.jl")
#info("Test: Weaving with Plots.jl")
#include("plotsjl_test.jl")
#include("publish_test.jl")
info("Test: Weaving with Plots.jl")
include("plotsjl_test.jl")
include("publish_test.jl")
end