mirror of https://github.com/mpastell/Weave.jl
Initial support for Winston plots
parent
b6aa8289de
commit
00f45832b3
|
@ -0,0 +1,42 @@
|
|||
|
||||
# Introducion to JuliaReport
|
||||
|
||||
This a sample [Julia](http://julialang.org/) noweb document that can
|
||||
be executed using JuliaReport. Output from code chunks and Winston
|
||||
plots will be included in the weaved document. You also need to install Pweave from Github in order to use JuliaReport.
|
||||
|
||||
This documented can be turned into Pandoc markdown with captured
|
||||
result from Julia prompt.
|
||||
|
||||
~~~~{.julia}
|
||||
using JuliaReport
|
||||
weave(Pkg.dir("JuliaReport","examples","winston_sample.mdw"), plotlib="Winston")
|
||||
~~~~
|
||||
|
||||
## Terminal chunk
|
||||
|
||||
<<"term"=>true>>=
|
||||
x = 1:10
|
||||
d = {"juliareport" => "testing"}
|
||||
y = [2, 4 ,8]
|
||||
@
|
||||
|
||||
## Capturing figures
|
||||
|
||||
The figures and code can be included in the output.
|
||||
|
||||
<<>>=
|
||||
using Winston
|
||||
figure()
|
||||
t = linspace(0, 2*pi, 100)
|
||||
plot(t, sinc(t))
|
||||
xlabel("x")
|
||||
ylabel("sinc(x)")
|
||||
@
|
||||
|
||||
You can also include a plot with caption and hide the code:
|
||||
|
||||
<<"echo"=>false, "caption"=>"Random walk.", "label"=>"random">>=
|
||||
figure()
|
||||
plot(cumsum(randn(1000, 1)))
|
||||
@
|
|
@ -1,6 +1,6 @@
|
|||
module JuliaReport
|
||||
using PyCall
|
||||
using PyPlot
|
||||
#using PyPlot
|
||||
@pyimport pweave #Output formatting uses Pweave
|
||||
|
||||
|
||||
|
@ -23,7 +23,7 @@ function listformats()
|
|||
pweave.listformats()
|
||||
end
|
||||
|
||||
function weave(source ; doctype = "pandoc", informat="noweb", figdir = "figures", figformat = nothing)
|
||||
function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb", figdir = "figures", figformat = nothing)
|
||||
pweave.rcParams["chunk"]["defaultoptions"]["engine"] = "julia"
|
||||
|
||||
doc = pweave.Pweb(source, doctype, shell="julia")
|
||||
|
@ -43,6 +43,17 @@ function weave(source ; doctype = "pandoc", informat="noweb", figdir = "figures"
|
|||
report.figdir = figdir
|
||||
report.formatdict = formatdict
|
||||
|
||||
l_plotlib = lowercase(plotlib)
|
||||
if l_plotlib == "winston"
|
||||
eval(Expr(:using, :Winston))
|
||||
rcParams["plotlib"] = "Winston"
|
||||
elseif l_plotlib == "pyplot"
|
||||
#eval(parse("import PyPlot.plt"))
|
||||
eval(Expr(:using, :PyPlot))
|
||||
rcParams["plotlib"] = "PyPlot"
|
||||
end
|
||||
|
||||
|
||||
parsed = read_noweb(source)
|
||||
doc[:executed] = run(parsed)
|
||||
|
||||
|
@ -130,6 +141,15 @@ function run(parsed)
|
|||
end
|
||||
|
||||
function savefigs(chunk)
|
||||
l_plotlib = lowercase(rcParams["plotlib"])
|
||||
if l_plotlib == "pyplot"
|
||||
return savefigs_pyplot(chunk)
|
||||
elseif l_plotlib == "winston"
|
||||
return savefigs_winston(chunk)
|
||||
end
|
||||
end
|
||||
|
||||
function savefigs_pyplot(chunk)
|
||||
fignames = String[]
|
||||
ext = report.formatdict["figfmt"]
|
||||
figpath = joinpath(report.cwd, report.figdir)
|
||||
|
@ -148,6 +168,36 @@ function savefigs(chunk)
|
|||
return fignames
|
||||
end
|
||||
|
||||
function savefigs_winston(chunk)
|
||||
fignames = String[]
|
||||
ext = report.formatdict["figfmt"]
|
||||
figpath = joinpath(report.cwd, report.figdir)
|
||||
isdir(figpath) || mkdir(figpath)
|
||||
|
||||
chunkid = ((chunk["name"] == nothing) ? chunk["number"] : chunk["name"])
|
||||
#Iterate over all open figures, save them and store names
|
||||
#println(Winston._display.figs)
|
||||
#println(Winston._display.fig_order)
|
||||
|
||||
for fig = copy(Winston._display.fig_order)
|
||||
full_name = joinpath(report.cwd, report.figdir, "$(report.basename)_$(chunkid)_$fig$ext")
|
||||
rel_name = "$(report.figdir)/$(report.basename)_$(chunkid)_$fig$ext" #Relative path is used in output
|
||||
println(rel_name)
|
||||
#figure(fig) #Calling figure clears the canvas!
|
||||
#savefig(Winston._display.figs[gcf()].plot, full_name)
|
||||
savefig(full_name)
|
||||
closefig()
|
||||
#savefig(full_name)
|
||||
push!(fignames, rel_name)
|
||||
end
|
||||
return fignames
|
||||
end
|
||||
|
||||
#Saving Winston figures
|
||||
#savefig(Winston._display.figs[1].plot, "test.png")
|
||||
|
||||
|
||||
|
||||
export weave
|
||||
|
||||
include("config.jl")
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const rcParams = {"figdir"=> "figures",
|
||||
"usematplotlib"=> true,
|
||||
"plotlib" => "Winston",
|
||||
"storeresults"=> false,
|
||||
"cachedir"=> "cache",
|
||||
"chunk"=>
|
||||
|
|
Loading…
Reference in New Issue