animations

This commit is contained in:
Thomas Breloff 2015-10-16 16:36:40 -04:00
parent 2c884ab5ab
commit 1db36c05a8
6 changed files with 331 additions and 32 deletions

119
examples/animations.ipynb Normal file
View File

@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Recompiling stale cache file /home/tom/.julia/lib/v0.4/Plots.ji for module Plots.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Plots.jl] Initializing backend: gadfly\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Saved animation to /home/tom/.julia/v0.4/Plots/examples/tmp.gif\n"
]
},
{
"data": {
"text/html": [
"<img src=\"tmp.gif?0.4950005887019313>\" />"
],
"text/plain": [
"Plots.AnimatedGif(\"/home/tom/.julia/v0.4/Plots/examples/tmp.gif\")"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Plots\n",
"gadfly()\n",
"\n",
"# create a plot\n",
"n = 10\n",
"p = scatter(randn(n), randn(n), size=(500,300))\n",
"\n",
"# make an animation by adding data and saving the frames\n",
"anim = Animation()\n",
"for i in 1:100\n",
" append!(p, 1, randn(n), randn(n))\n",
" frame(anim)\n",
"end\n",
"g = gif(anim, fps=50)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Saved animation to /home/tom/.julia/v0.4/Plots/examples/tmp.gif\n"
]
},
{
"data": {
"text/html": [
"<img src=\"tmp.gif?0.30448413983944445>\" />"
],
"text/plain": [
"Plots.AnimatedGif(\"/home/tom/.julia/v0.4/Plots/examples/tmp.gif\")"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = gif(anim, fps=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.4.0-rc4",
"language": "julia",
"name": "julia-0.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

File diff suppressed because one or more lines are too long

View File

@ -17,7 +17,38 @@ function frame(anim::Animation)
push!(anim.frames, filename)
end
function gif(anim::Animation, fn::@compat(AbstractString) = tempname() * ".gif"; fps::Integer = 20)
run(`ffmpeg -framerate $fps -i $(anim.dir)/%06d.png -y $fn`)
# -----------------------------------------------
"Wraps the location of an animated gif so that it can be displayed"
immutable AnimatedGif
filename::ASCIIString
end
function gif(anim::Animation, fn::@compat(AbstractString) = "tmp.gif"; fps::Integer = 20)
fn = abspath(fn)
try
# high quality
speed = round(Int, 100 / fps)
run(`convert -delay $speed -loop 0 $(anim.dir)/*.png $fn`)
catch err
warn("Tried to create gif using convert (ImageMagick), but got error: $err\nWill try ffmpeg, but it's lower quality...)")
# low quality
run(`ffmpeg -v 0 -framerate $fps -i $(anim.dir)/%06d.png -y $fn`)
# run(`ffmpeg -v warning -i "fps=$fps,scale=320:-1:flags=lanczos"`)
end
info("Saved animation to ", fn)
end
AnimatedGif(fn)
end
# write out html to view the gif... note the rand call which is a hack so the image doesn't get cached
function Base.writemime(io::IO, ::MIME"text/html", agif::AnimatedGif)
write(io, "<img src=\"$(relpath(agif.filename))?$(rand())>\" />")
end

View File

@ -184,7 +184,8 @@ function addGadflyLine!(plt::Plot, d::Dict, geoms...)
# add the layer
x = d[d[:linetype] == :hist ? :y : :x]
prepend!(gplt.layers, Gadfly.layer(gfargs...; x = x, y = d[:y], kwargs...))
Gadfly.layer(gfargs...; x = x, y = d[:y], kwargs...)
# prepend!(gplt.layers, )
end
@ -207,7 +208,6 @@ function getGadflyMarkerTheme(d::Dict)
end
function addGadflyMarker!(plt::Plot, d::Dict, geoms...)
gplt = getGadflyContext(plt)
gfargs = vcat(geoms...,
getGadflyMarkerTheme(d),
getMarkerGeom(d))
@ -220,10 +220,11 @@ function addGadflyMarker!(plt::Plot, d::Dict, geoms...)
if !isa(d[:markercolor], ColorGradient)
d[:markercolor] = colorscheme(:bluesreds)
end
push!(gplt.scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p))))
push!(getGadflyContext(plt).scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p))))
end
prepend!(gplt.layers, Gadfly.layer(gfargs...; x = d[:x], y = d[:y], kwargs...))
Gadfly.layer(gfargs...; x = d[:x], y = d[:y], kwargs...)
# prepend!(gplt.layers, )
end
@ -276,6 +277,8 @@ getGadflySmoothing(smooth::Real) = [Gadfly.Geom.smooth(method=:loess, smoothing=
function addGadflySeries!(plt::Plot, d::Dict)
layers = Gadfly.Layer[]
# add a regression line?
# TODO: make more flexible
# smooth = d[:smooth] ? [Gadfly.Geom.smooth(method=:lm)] : Any[]
@ -284,7 +287,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
# lines
geom = getLineGeom(d)
if geom != nothing
addGadflyLine!(plt, d, geom, smooth...)
prepend!(layers, addGadflyLine!(plt, d, geom, smooth...))
# don't add a regression for markers too
smooth = Any[]
@ -300,10 +303,14 @@ function addGadflySeries!(plt::Plot, d::Dict)
# markers
if d[:markershape] != :none
addGadflyMarker!(plt, d, smooth...)
prepend!(layers, addGadflyMarker!(plt, d, smooth...))
end
lt in (:hist, :heatmap, :hexbin) || addToGadflyLegend(plt, d)
# now save the layers that apply to this series
d[:gadflylayers] = layers
prepend!(getGadflyContext(plt).layers, layers)
end
@ -516,6 +523,32 @@ function updatePlotItems(plt::Plot{GadflyPackage}, d::Dict)
end
# ----------------------------------------------------------------
# accessors for x/y data
# TODO: need to save all the layer indices which apply to this series
function getGadflyMappings(plt::Plot, i::Integer)
@assert i > 0 && i <= plt.n
mappings = [l.mapping for l in plt.seriesargs[i][:gadflylayers]]
end
function Base.getindex(plt::Plot{GadflyPackage}, i::Integer)
# data = getGadflyContext(plt).layers[end-i+1].mapping
# data[:x], data[:y]
mapping = getGadflyMappings(plt, i)[1]
mapping[:x], mapping[:y]
end
function Base.setindex!(plt::Plot{GadflyPackage}, xy::Tuple, i::Integer)
# data = getGadflyContext(plt).layers[end-i+1].mapping
# data[:x], data[:y] = xy
for mapping in getGadflyMappings(plt, i)
mapping[:x], mapping[:y] = xy
end
plt
end
# ----------------------------------------------------------------

View File

@ -279,8 +279,8 @@ end
# used in updating an existing series
extendUnitRange(v::UnitRange{Int}, n::Int = 1) = minimum(v):maximum(v)+n
extendSeriesData(v::AVec, z) = (push!(v, z); v)
extendSeriesData(v::AVec, z::AVec) = (append!(v, z); v)
extendSeriesData{T}(v::AVec{T}, z::Real) = (push!(v, convert(T, z)); v)
extendSeriesData{T}(v::AVec{T}, z::AVec) = (append!(v, convert(Vector{T}, z)); v)
# ---------------------------------------------------------------