pyplot annotations and ioff fix

This commit is contained in:
Thomas Breloff 2015-09-28 12:09:52 -04:00
parent eafc1a784c
commit 228ce768e0
8 changed files with 63 additions and 34 deletions

View File

@ -68,6 +68,7 @@ scatter(iris, :SepalLength, :SepalWidth, group=:Species, ms=12, m=[:+,:d,:s])
![gadfly_plt](img/gadfly1.png)
See the examples pages for lots of examples of plots, and what those commands produce for each supported backend.
Also check out the [IJulia notebooks](examples) and see how it works interactively.
## API
@ -168,12 +169,14 @@ xlims!{T<:Real,S<:Real}(lims::Tuple{T,S}) = plot!(xlims = lims)
ylims!{T<:Real,S<:Real}(lims::Tuple{T,S}) = plot!(ylims = lims)
xticks!{T<:Real}(v::AVec{T}) = plot!(xticks = v)
yticks!{T<:Real}(v::AVec{T}) = plot!(yticks = v)
annotate!(annotations) = plot!(annotation = annotations)
```
Some keyword arguments you can set:
Keyword | Default | Type | Aliases
---- | ---- | ---- | ----
`:annotation` | `nothing` | Series | `:ann`, `:annotate`, `:annotations`, `:anns`
`:args` | `Any[]` | Series | `:argss`
`:axis` | `left` | Series | `:axiss`
`:color` | `auto` | Series | `:c`, `:colors`

View File

@ -86,7 +86,13 @@ const examples = PlotExample[
PlotExample("Open/High/Low/Close",
"Create an OHLC chart. Pass in a vector of 4-tuples as your `y` argument. Adjust the tick width with arg `markersize`.",
[:(n=20), :(hgt=rand(n)+1), :(bot=randn(n)), :(openpct=rand(n)), :(closepct=rand(n)), :(y = [OHLC(openpct[i]*hgt[i]+bot[i], bot[i]+hgt[i], bot[i], closepct[i]*hgt[i]+bot[i]) for i in 1:n]), :(ohlc(y; markersize=8))]),
PlotExample("Annotations",
"Currently only text annotations are supported. Pass in a tuple or vector-of-tuples: (x,y,text). `annotate!(ann)` is shorthand for `plot!(; annotation=ann)`",
[
:(y = rand(10)),
:(plot(y, ann=(3,y[3],"this is #3"))),
:(annotate!([(5,y[5],"this is #5"),(8.4,y[10],"this is #10")]))
]),
]
@ -164,7 +170,7 @@ function test_example(pkgname::Symbol, idx::Int)
plotter()
map(eval, examples[idx].exprs)
plt = currentPlot()
display(plt)
gui(plt)
plt
end
@ -172,9 +178,9 @@ end
function test_all_examples(pkgname::Symbol)
plts = Dict()
for i in 1:length(examples)
if examples[i].header == "Subplots" && !subplotSupported()
break
end
# if examples[i].header == "Subplots" && !subplotSupported()
# break
# end
try
plt = test_example(pkgname, i)

View File

@ -68,6 +68,7 @@ scatter(iris, :SepalLength, :SepalWidth, group=:Species, ms=12, m=[:+,:d,:s])
![gadfly_plt](img/gadfly1.png)
See the examples pages for lots of examples of plots, and what those commands produce for each supported backend.
Also check out the [IJulia notebooks](examples) and see how it works interactively.
## API

File diff suppressed because one or more lines are too long

View File

@ -301,18 +301,16 @@ end
# ----------------------------------------------------------------
function createAnnotationObject(x, y, val::AbstractString)
function createGadflyAnnotationObject(x, y, val::AbstractString)
Gadfly.Guide.annotation(Compose.compose(
Compose.context(),
Compose.text(x, y, val)
# Compose.fill(colorant"black"),
# Compose.stroke(colorant"black")
))
end
function addAnnotations{X,Y,V}(plt::Plot{GadflyPackage}, anns::AVec{Tuple{X,Y,V}})
for ann in anns
push!(plt.o.guides, createAnnotationObject(ann...))
push!(plt.o.guides, createGadflyAnnotationObject(ann...))
end
end

View File

@ -50,6 +50,14 @@ end
# ----------------------------------------------------------------
function addAnnotations{X,Y,V}(plt::Plot{ImmersePackage}, anns::AVec{Tuple{X,Y,V}})
for ann in anns
push!(plt.o[2].guides, createGadflyAnnotationObject(ann...))
end
end
# ----------------------------------------------------------------
# accessors for x/y data

View File

@ -243,14 +243,16 @@ end
# -------------------------------
# function savepng(::PyPlotPackage, plt::PlottingObject, fn::AbstractString, args...)
# fig, num = plt.o
# addPyPlotLegend(plt)
# f = open(fn, "w")
# writemime(f, "image/png", fig)
# close(f)
# end
function createPyPlotAnnotationObject(plt::Plot{PyPlotPackage}, x, y, val::AbstractString)
ax = getLeftAxis(plt.o[1])
ax[:annotate](val, xy = (x,y))
end
function addAnnotations{X,Y,V}(plt::Plot{PyPlotPackage}, anns::AVec{Tuple{X,Y,V}})
for ann in anns
createPyPlotAnnotationObject(plt, ann...)
end
end
# -----------------------------------------------------------------
@ -274,7 +276,6 @@ end
function Base.writemime(io::IO, m::MIME"image/png", plt::PlottingObject{PyPlotPackage})
fig, num = plt.o
# makePyPlotCurrent(plt)
addPyPlotLegend(plt)
ax = fig.o[:axes][1]
updateAxisColors(ax, getPyPlotColor(plt.initargs[:foreground_color]))
@ -284,8 +285,6 @@ end
function Base.display(::PlotsDisplay, plt::Plot{PyPlotPackage})
fig, num = plt.o
# PyPlot.figure(num) # makes this current
# makePyPlotCurrent(plt)
addPyPlotLegend(plt)
ax = fig.o[:axes][1]
updateAxisColors(ax, getPyPlotColor(plt.initargs[:foreground_color]))

View File

@ -125,7 +125,9 @@ function plotter()
try
@eval import PyPlot
@eval export PyPlot
PyPlot.ioff()
if !isa(Base.Multimedia.displays[end], Base.REPL.REPLDisplay)
PyPlot.ioff()
end
catch
error("Couldn't import PyPlot. Install it with: Pkg.add(\"PyPlot\")")
end