mirror of https://github.com/mpastell/Weave.jl
First version with Plots.jl support, need to decide how to display figures
parent
87e7965627
commit
f24b2157a6
|
@ -0,0 +1,82 @@
|
||||||
|
% Intro to Weave.jl with Plot.jl
|
||||||
|
% Matti Pastell
|
||||||
|
% 20th April 2016
|
||||||
|
|
||||||
|
# Introduction
|
||||||
|
|
||||||
|
This a sample [Julia](http://julialang.org/) noweb document that can
|
||||||
|
be executed using [Weave.jl](https://github.com/mpastell/Weave.jl).
|
||||||
|
|
||||||
|
The code is delimited from docs using markdown fenced code blocks
|
||||||
|
markup which can be seen looking at the source document [gadfly_md_sample.jmd](gadfly_md_sample.jmd)
|
||||||
|
in the examples directory of the package. The source document can be executed
|
||||||
|
and the results with Plot.jl plots are captured in the resulting file.
|
||||||
|
|
||||||
|
You can create markdown output or pdf and HTML directly (with Pandoc) using
|
||||||
|
the weave command as follows:
|
||||||
|
|
||||||
|
```{julia; eval=false}
|
||||||
|
using Weave
|
||||||
|
#Markdown
|
||||||
|
weave(Pkg.dir("Weave","examples","gadfly_md_sample.jmd"), informat="markdown",
|
||||||
|
out_path = :pwd, doctype = "pandoc")
|
||||||
|
#HTML
|
||||||
|
weave(Pkg.dir("Weave","examples","gadfly_md_sample.jmd"), informat="markdown",
|
||||||
|
out_path = :pwd, doctype = "md2html")
|
||||||
|
#pdf
|
||||||
|
weave(Pkg.dir("Weave","examples","gadfly_md_sample.jmd"), informat="markdown",
|
||||||
|
out_path = :pwd, doctype = "md2pdf")
|
||||||
|
```
|
||||||
|
|
||||||
|
*The documents will be written to the Julia working directory when you
|
||||||
|
use the `out_path = :pwd`.*
|
||||||
|
|
||||||
|
# Capturing code
|
||||||
|
|
||||||
|
The basic code chunk will be run with default options and the code and
|
||||||
|
output will be captured.
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using Plots
|
||||||
|
pyplot()
|
||||||
|
x = linspace(0, 2*pi)
|
||||||
|
println(x)
|
||||||
|
plot(x = x, y = sin(x))
|
||||||
|
```
|
||||||
|
|
||||||
|
```julia; term=true
|
||||||
|
plot(x = x, y = sin(x))
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
You can also control the way the results are captured, plot size etc.
|
||||||
|
using chunk options. Here is an example of a chunk that behaves like a repl.
|
||||||
|
|
||||||
|
```{julia;term=true}
|
||||||
|
x = 1:10
|
||||||
|
d = Dict("Weave" => "testing")
|
||||||
|
y = [2, 4 ,8]
|
||||||
|
```
|
||||||
|
|
||||||
|
```julia
|
||||||
|
plot(rand(100) / 3,reg=true,fill=(0,:green))
|
||||||
|
scatter!(rand(100),markersize=6,c=:orange)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
You can also for instance hide the code and show only the figure, add a
|
||||||
|
caption to the figure and make it wider as follows (you can only see the
|
||||||
|
syntax from the source document):
|
||||||
|
|
||||||
|
```{julia;echo=false; fig_cap="A random walk."; label="random"; fig_width=8; fig_height=4}
|
||||||
|
plot(y = cumsum(randn(1000, 1)))
|
||||||
|
```
|
||||||
|
|
||||||
|
# Whats next
|
||||||
|
|
||||||
|
Read the documentation:
|
||||||
|
|
||||||
|
- stable: <http://mpastell.github.io/Weave.jl/stable/>
|
||||||
|
- latest: <http://mpastell.github.io/Weave.jl/latest/>
|
||||||
|
|
||||||
|
See other examples in: <https://github.com/mpastell/Weave.jl/tree/master/examples>
|
|
@ -0,0 +1,43 @@
|
||||||
|
|
||||||
|
using Plots
|
||||||
|
|
||||||
|
#Captures figures
|
||||||
|
function Base.display(report::Report, m::MIME"image/png", data)
|
||||||
|
|
||||||
|
chunk = report.cur_chunk
|
||||||
|
full_name, rel_name = get_figname(report, chunk)
|
||||||
|
|
||||||
|
docformat = formats[report.formatdict[:doctype]]
|
||||||
|
push!(report.figures, rel_name)
|
||||||
|
|
||||||
|
report.fignum += 1
|
||||||
|
|
||||||
|
w = chunk.options[:fig_width]
|
||||||
|
h = chunk.options[:fig_height]
|
||||||
|
format = chunk.options[:fig_ext]
|
||||||
|
dpi = chunk.options[:dpi]
|
||||||
|
info("Caught a plot")
|
||||||
|
info(typeof(data))
|
||||||
|
savefig(data, full_name)
|
||||||
|
|
||||||
|
#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)
|
||||||
|
# catch
|
||||||
|
# draw(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)
|
||||||
|
# elseif format == ".ps"
|
||||||
|
# draw(PS(full_name, w, h), p)
|
||||||
|
# elseif format == ".svg"
|
||||||
|
# draw(SVG(full_name, w, h), p)
|
||||||
|
# elseif format == ".js.svg"
|
||||||
|
# draw(SVGJS(full_name, w, h), p)
|
||||||
|
# elseif format == ".tex"
|
||||||
|
# draw(PGF(full_name, w, h, true ), p)
|
||||||
|
# else:
|
||||||
|
# warn("Can't save figure. Unsupported format")
|
||||||
|
# end
|
||||||
|
end
|
|
@ -144,6 +144,8 @@ function capture_output(expr::Expr, SandBox::Module, term, plotlib)
|
||||||
obj != nothing && display(obj)
|
obj != nothing && display(obj)
|
||||||
elseif plotlib == "Gadfly" && typeof(obj) == Gadfly.Plot
|
elseif plotlib == "Gadfly" && typeof(obj) == Gadfly.Plot
|
||||||
obj != nothing && display(obj)
|
obj != nothing && display(obj)
|
||||||
|
elseif plotlib == "Plots" && issubtype(typeof(obj), Plots.Plot)
|
||||||
|
obj != nothing && display(obj)
|
||||||
end
|
end
|
||||||
|
|
||||||
finally
|
finally
|
||||||
|
@ -248,7 +250,10 @@ function init_plotting(plotlib)
|
||||||
elseif l_plotlib == "gadfly"
|
elseif l_plotlib == "gadfly"
|
||||||
eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))"""))
|
eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))"""))
|
||||||
rcParams[:plotlib] = "Gadfly"
|
rcParams[:plotlib] = "Gadfly"
|
||||||
end
|
elseif l_plotlib == "plots"
|
||||||
|
eval(parse("""include(Pkg.dir("Weave","src","plotsjl.jl"))"""))
|
||||||
|
rcParams[:plotlib] = "Plots"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return nothing
|
return nothing
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue