Fix Plots.jl PyPlot backend saving and update tests

pull/66/head
Matti Pastell 2016-11-01 15:23:14 +02:00
parent 10e089379b
commit 7bccdd2ab1
10 changed files with 88 additions and 47 deletions

View File

@ -32,7 +32,7 @@ function Base.display(report::Report, data)
try
display(report, m, data)
catch
info(m)
warn("Failed to save image in \"$m\" format")
continue
end
#Always show plain text as well for term mode
@ -45,21 +45,15 @@ function Base.display(report::Report, data)
end
function Base.display(report::Report, m::MIME"image/png", data)
figname = add_figure(report, ".png")
figname = add_figure(report, data, m, ".png")
end
function Base.display(report::Report, m::MIME"image/svg+xml", data)
figname = add_figure(report, ".svg")
open(figname, "w") do io
show(io, m, data)
end
figname = add_figure(report, data, m, ".svg")
end
function Base.display(report::Report, m::MIME"application/pdf", data)
figname = add_figure(report, ".pdf")
open(figname, "w") do io
show(io, m, data)
end
figname = add_figure(report, m, data, ".pdf")
end
#Text is written to stdout, called from "term" mode chunks
@ -87,7 +81,7 @@ end
"""Add saved figure name to results and return the name"""
function add_figure(report::Report, ext)
function add_figure(report::Report, data, m, ext)
chunk = report.cur_chunk
full_name, rel_name = get_figname(report, chunk, ext = ext)

View File

@ -53,3 +53,27 @@ function Base.display(report::Report, m::MIME"image/png", plot::Plots.Plot{Plots
report.rich_output *= "\n" * body
end
"""Add saved figure name to results and return the name"""
function add_pyplot_figure(report::Report, plot::Plots.Plot{Plots.PyPlotBackend}, ext)
chunk = report.cur_chunk
full_name, rel_name = get_figname(report, chunk, ext = ext)
Plots.savefig(plot, full_name)
push!(report.figures, rel_name)
report.fignum += 1
return full_name
end
function Base.display(report::Report, m::MIME"application/pdf", plot::Plots.Plot{Plots.PyPlotBackend})
add_pyplot_figure(report, plot, ".pdf")
end
function Base.display(report::Report, m::MIME"image/png", plot::Plots.Plot{Plots.PyPlotBackend})
add_pyplot_figure(report, plot, ".png")
end
function Base.display(report::Report, m::MIME"image/svg+xml", plot::Plots.Plot{Plots.PyPlotBackend})
add_pyplot_figure(report, plot, ".svg")
end

View File

@ -0,0 +1,16 @@
using Plots
pyplot()
x = linspace(0, 2*pi)
println(x)
p = plot(x = x, y = sin(x), size =(900,300))
plot(x = x, y = sin(x))
plot(rand(100) / 3,reg=true,fill=(0,:green))
scatter!(rand(100),markersize=6,c=:orange)
plot(rand(100) / 3,reg=true,fill=(0,:green))
scatter!(rand(100),markersize=6,c=:orange)
plot(y = cumsum(randn(1000, 1)))

View File

@ -20,10 +20,7 @@ p = plot(x = x, y = sin(x), size =(900,300))
~~~~~~~~~~~~~
~~~~
[Plots.jl] Initializing backend: pyplot
~~~~
![](figures/plotsjl_test_1_1.png)\
@ -31,12 +28,14 @@ p = plot(x = x, y = sin(x), size =(900,300))
~~~~{.julia}
julia> plot(x = x, y = sin(x))
Plot{Plots.PyPlotBackend() n=1}
~~~~~~~~~~~~~
![](figures/plotsjl_test_2_1.png)\
![](figures/plotsjl_test_2_1.png)\
@ -48,13 +47,15 @@ scatter!(rand(100),markersize=6,c=:orange)
![](figures/plotsjl_test_3_1.png)\
![](figures/plotsjl_test_3_1.png)\
~~~~{.julia}
julia> plot(rand(100) / 3,reg=true,fill=(0,:green))
Plot{Plots.PyPlotBackend() n=1}
~~~~~~~~~~~~~
@ -63,14 +64,17 @@ Plot{Plots.PyPlotBackend() n=1}
~~~~{.julia}
julia> scatter!(rand(100),markersize=6,c=:orange)
Plot{Plots.PyPlotBackend() n=2}
~~~~~~~~~~~~~
![](figures/plotsjl_test_4_2.png)\
![](figures/plotsjl_test_4_1.png)\
![A random walk.](figures/plotsjl_test_random_1.png)
![](figures/plotsjl_test_random_1.png)

View File

@ -20,7 +20,7 @@ p = plot(x = x, y = sin(x), size =(900,300))
\begin{juliaterm}
julia> plot(x = x, y = sin(x))
Plot{Plots.PyPlotBackend() n=1}
\end{juliaterm}
\includegraphics[width=\linewidth]{figures/plotsjl_test_2_1.pdf}
@ -37,13 +37,13 @@ scatter!(rand(100),markersize=6,c=:orange)
\begin{juliaterm}
julia> plot(rand(100) / 3,reg=true,fill=(0,:green))
Plot{Plots.PyPlotBackend() n=1}
\end{juliaterm}
\includegraphics[width=\linewidth]{figures/plotsjl_test_4_1.pdf}
\begin{juliaterm}
julia> scatter!(rand(100),markersize=6,c=:orange)
Plot{Plots.PyPlotBackend() n=2}
\end{juliaterm}
\includegraphics[width=\linewidth]{figures/plotsjl_test_4_2.pdf}

View File

@ -11,7 +11,7 @@ plot(x, sin(x))
````
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f5326a4a290>
PyObject <matplotlib.lines.Line2D object at 0x7f6a20a84780>
````
@ -21,7 +21,7 @@ plot(x, sin(x))
````
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f5326a56c10>
PyObject <matplotlib.lines.Line2D object at 0x7f6a208be780>
````
@ -31,7 +31,7 @@ plot(x, sin(x))
````
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f5326d76350>
PyObject <matplotlib.lines.Line2D object at 0x7f6a207f5c50>
````
@ -42,17 +42,18 @@ plot(x, sin(x))
````julia
julia> x = linspace(0, 2π, 200)
linspace(0.0,6.283185307179586,200)
200-element LinSpace{Float64}:
0.0,0.0315738,0.0631476,0.0947214,0.126295,…,6.18846,6.22004,6.25161,6.28319
julia> plot(x, sin(x))
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f5326de8c10>
PyObject <matplotlib.lines.Line2D object at 0x7f6a207da518>
julia> y = 20
20
julia> plot(x, cos(x))
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f5326de8e50>
PyObject <matplotlib.lines.Line2D object at 0x7f6a207da780>
````
@ -67,7 +68,7 @@ contourf(x)
````
PyObject <matplotlib.contour.QuadContourSet object at 0x7f53346d2910>
PyObject <matplotlib.contour.QuadContourSet object at 0x7f6a1980e0f0>
````

View File

@ -13,7 +13,7 @@
::
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f9745111450>
PyObject <matplotlib.lines.Line2D object at 0x7f6a209bb518>
@ -28,7 +28,7 @@
::
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f9777e49990>
PyObject <matplotlib.lines.Line2D object at 0x7f6a2099c3c8>
@ -43,7 +43,7 @@
::
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f9777d7b590>
PyObject <matplotlib.lines.Line2D object at 0x7f6a20e59780>
@ -57,17 +57,18 @@
julia> x = linspace(0, 2π, 200)
linspace(0.0,6.283185307179586,200)
200-element LinSpace{Float64}:
0.0,0.0315738,0.0631476,0.0947214,0.126295,…,6.18846,6.22004,6.25161,6.28319
julia> plot(x, sin(x))
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f9777c71190>
PyObject <matplotlib.lines.Line2D object at 0x7f6a19819208>
julia> y = 20
20
julia> plot(x, cos(x))
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7f9777c71390>
PyObject <matplotlib.lines.Line2D object at 0x7f6a209aed68>
@ -86,7 +87,7 @@ julia> plot(x, cos(x))
::
PyObject <matplotlib.contour.QuadContourSet object at 0x7f9777ba3510>
PyObject <matplotlib.contour.QuadContourSet object at 0x7f6a193c6668>

View File

@ -9,7 +9,7 @@ plot(x, sin(x))
\end{juliacode}
\begin{juliaout}
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7fe847179cd0>
PyObject <matplotlib.lines.Line2D object at 0x7f6a2109f6a0>
\end{juliaout}
\begin{figure}[ht]
\center
@ -22,7 +22,7 @@ plot(x, sin(x))
\begin{juliaout}
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7fe846fb2950>
PyObject <matplotlib.lines.Line2D object at 0x7f6a20f34e48>
\end{juliaout}
\begin{figure}[htpb]
\center
@ -34,7 +34,7 @@ plot(x, sin(x))
\begin{juliaout}
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7fe839790510>
PyObject <matplotlib.lines.Line2D object at 0x7f6a20e48b70>
\end{juliaout}
\includegraphics[width=\linewidth]{figures/pyplot_formats_cos2_fun_1.pdf}
@ -43,17 +43,18 @@ plot(x, sin(x))
\begin{juliaterm}
julia> x = linspace(0, 2π, 200)
linspace(0.0,6.283185307179586,200)
200-element LinSpace{Float64}:
0.0,0.0315738,0.0631476,0.0947214,0.126295,…,6.18846,6.22004,6.25161,6.28319
julia> plot(x, sin(x))
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7fe8396c8110>
PyObject <matplotlib.lines.Line2D object at 0x7f6a20dad160>
julia> y = 20
20
julia> plot(x, cos(x))
1-element Array{Any,1}:
PyObject <matplotlib.lines.Line2D object at 0x7fe8396c8310>
PyObject <matplotlib.lines.Line2D object at 0x7f6a20e9ce80>
\end{juliaterm}
\includegraphics[width=\linewidth]{figures/pyplot_formats_4_1.pdf}
@ -64,6 +65,6 @@ x = randn(100, 100)
contourf(x)
\end{juliacode}
\begin{juliaout}
PyObject <matplotlib.contour.QuadContourSet object at 0x7fe8395fc390>
PyObject <matplotlib.contour.QuadContourSet object at 0x7f6a20adb1d0>
\end{juliaout}
\includegraphics[width=15cm]{figures/pyplot_formats_5_1.pdf}

View File

@ -2,7 +2,7 @@ using Weave, Compat
using Base.Test
function pljtest(source, resfile, doctype)
weave("documents/$source", out_path = "documents/plotsjl/$resfile", doctype=doctype, plotlib=nothing)
weave("documents/$source", out_path = "documents/plotsjl/$resfile", doctype=doctype)
result = @compat readstring(open("documents/plotsjl/$resfile"))
ref = @compat readstring(open("documents/plotsjl/$resfile.ref"))
@test result == ref

View File

@ -26,8 +26,8 @@ include("gadfly_formats.jl")
#include("winston_formats.jl")
# Output changed, needs fixing for 0.5
#info("Test: Weaving with PyPlot")
#include("pyplot_formats.jl")
info("Test: Weaving with PyPlot")
include("pyplot_formats.jl")
#info("Test: Weaving with Plots.jl")
#include("plotsjl_test.jl")
info("Test: Weaving with Plots.jl")
include("plotsjl_test.jl")