mirror of https://github.com/mpastell/Weave.jl
Generic svg and png handlers work
parent
37f0c7f796
commit
45f8871740
|
@ -1,82 +0,0 @@
|
|||
% 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>
|
|
@ -1,43 +0,0 @@
|
|||
|
||||
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
|
10
src/run.jl
10
src/run.jl
|
@ -148,8 +148,9 @@ function capture_output(expr::Expr, SandBox::Module, term, plotlib, lastline)
|
|||
obj != nothing && display(obj)
|
||||
elseif plotlib == "Gadfly" && typeof(obj) == Gadfly.Plot
|
||||
obj != nothing && display(obj)
|
||||
#Display Plots.jl plots if they are the last expr in script mode
|
||||
#elseif plotlib == "Plots" && lastline && issubtype(typeof(obj), Plots.Plot)
|
||||
#This displays images from last line, result can
|
||||
#still be e.g. SVG depending on the avaible methods
|
||||
#for the type
|
||||
elseif lastline && mimewritable("image/png", obj)
|
||||
obj != nothing && display(obj)
|
||||
end
|
||||
|
@ -241,7 +242,7 @@ end
|
|||
|
||||
function init_plotting(plotlib)
|
||||
if plotlib == nothing
|
||||
rcParams[:chunk_defaults][:fig] = false
|
||||
#rcParams[:chunk_defaults][:fig] = false
|
||||
rcParams[:plotlib] = nothing
|
||||
else
|
||||
l_plotlib = lowercase(plotlib)
|
||||
|
@ -256,9 +257,6 @@ function init_plotting(plotlib)
|
|||
elseif l_plotlib == "gadfly"
|
||||
eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))"""))
|
||||
rcParams[:plotlib] = "Gadfly"
|
||||
elseif l_plotlib == "plots"
|
||||
eval(parse("""include(Pkg.dir("Weave","src","plotsjl.jl"))"""))
|
||||
rcParams[:plotlib] = "Plots"
|
||||
end
|
||||
end
|
||||
return nothing
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import Winston
|
||||
|
||||
function Base.display(report::Report, m::MIME"image/png", data)
|
||||
function Base.display(report::Report, m::MIME"image/svg+xml", Winston.FramedPlot)
|
||||
|
||||
chunk = report.cur_chunk
|
||||
full_name, rel_name = get_figname(report, chunk)
|
||||
|
@ -9,7 +9,6 @@ function Base.display(report::Report, m::MIME"image/png", data)
|
|||
push!(report.figures, rel_name)
|
||||
report.fignum += 1
|
||||
vector_fmts = [".pdf"; ".svg"]
|
||||
|
||||
#Don't use dpi for vector formats
|
||||
if chunk.options[:fig_ext] in vector_fmts
|
||||
Winston.savefig(data, full_name, width=chunk.options[:fig_width]*100,
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
|
||||
|
||||
```julia
|
||||
using Plots
|
||||
pyplot()
|
||||
x = linspace(0, 2*pi)
|
||||
println(x)
|
||||
plot(x = x, y = sin(x), size =(900,300))
|
||||
```
|
||||
|
||||
|
||||
```julia; term=true
|
||||
plot(x = x, y = sin(x))
|
||||
```
|
||||
|
||||
```julia
|
||||
plot(rand(100) / 3,reg=true,fill=(0,:green))
|
||||
scatter!(rand(100),markersize=6,c=:orange)
|
||||
```
|
||||
|
||||
```julia; term=true
|
||||
plot(rand(100) / 3,reg=true,fill=(0,:green))
|
||||
scatter!(rand(100),markersize=6,c=:orange)
|
||||
```
|
||||
|
||||
|
||||
```{julia;echo=false; fig_cap="A random walk."; label="random"}
|
||||
plot(y = cumsum(randn(1000, 1)))
|
||||
```
|
||||
|
||||
```julia
|
||||
using TestImages
|
||||
testimage("mandrill")
|
||||
```
|
Loading…
Reference in New Issue