savefig and png
This commit is contained in:
parent
e386636d66
commit
a68ec9b191
@ -140,7 +140,7 @@ function generate_markdown(pkgname::Symbol)
|
||||
imgname = "$(pkgname)_example_$i.png"
|
||||
|
||||
# NOTE: uncomment this to overwrite the images as well
|
||||
savepng("$IMGDIR/$pkgname/$imgname")
|
||||
png("$IMGDIR/$pkgname/$imgname")
|
||||
|
||||
# write out the header, description, code block, and image link
|
||||
write(md, "### $(example.header)\n\n")
|
||||
|
||||
@ -340,7 +340,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"?sort"
|
||||
"using Plots\n",
|
||||
"plot(rand(10))\n",
|
||||
"png(\"tmp\") # saves as \"tmp.png\"\n",
|
||||
"png(\"tmp.png\") # saves as \"tmp.png\"\n",
|
||||
"savefig(\"tmp\") # error... can't extract extension\n",
|
||||
"savefig(\"tmp.png\") # saves as \"tmp.png\""
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
58
src/Plots.jl
58
src/Plots.jl
@ -42,7 +42,8 @@ export
|
||||
yticks!,
|
||||
annotate!,
|
||||
|
||||
savepng,
|
||||
savefig,
|
||||
png,
|
||||
gui,
|
||||
|
||||
backend,
|
||||
@ -116,19 +117,66 @@ annotate!(plt::Plot, anns) = plot!(plt; annotation =
|
||||
|
||||
# ---------------------------------------------------------
|
||||
|
||||
function getExtension(fn::AbstractString)
|
||||
pieces = split(fn, ".")
|
||||
if length(pieces) < 2
|
||||
error("Can't extract file extension: ", fn)
|
||||
end
|
||||
pieces[end]
|
||||
end
|
||||
|
||||
savepng(args...; kw...) = savepng(current(), args...; kw...)
|
||||
savepng(plt::PlottingObject, fn::AbstractString; kw...) = (io = open(fn, "w"); writemime(io, MIME("image/png"), plt); close(io))
|
||||
# savepng(plt::PlottingObject, args...; kw...) = savepng(plt.backend, plt, args...; kw...)
|
||||
# savepng(::PlottingPackage, plt::PlottingObject, fn::AbstractString, args...) = error("unsupported") # fallback so multiple dispatch doesn't get confused if it's missing
|
||||
function addExtension(fn::AbstractString, ext::AbstractString)
|
||||
try
|
||||
oldext = getExtension(fn)
|
||||
if oldext == ext
|
||||
return fn
|
||||
else
|
||||
return "$fn.$ext"
|
||||
end
|
||||
catch
|
||||
return "$fn.$ext"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function png(plt::PlottingObject, fn::AbstractString)
|
||||
fn = addExtension(fn, "png")
|
||||
io = open(fn, "w")
|
||||
writemime(io, MIME("image/png"), plt)
|
||||
close(io)
|
||||
end
|
||||
png(fn::AbstractString) = png(current(), fn)
|
||||
|
||||
|
||||
const _savemap = Dict(
|
||||
"png" => png,
|
||||
)
|
||||
|
||||
function savefig(plt::PlottingObject, fn::AbstractString)
|
||||
ext = getExtension(fn)
|
||||
func = get(_savemap, ext) do
|
||||
error("Unsupported extension $ext with filename ", fn)
|
||||
end
|
||||
func(plt, fn)
|
||||
end
|
||||
savefig(fn::AbstractString) = savefig(current(), fn)
|
||||
|
||||
|
||||
# savepng(args...; kw...) = savepng(current(), args...; kw...)
|
||||
# savepng(plt::PlottingObject, fn::AbstractString; kw...) = (io = open(fn, "w"); writemime(io, MIME("image/png"), plt); close(io))
|
||||
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
|
||||
gui(plt::PlottingObject = current()) = display(PlotsDisplay(), plt)
|
||||
|
||||
|
||||
# override the REPL display to open a gui window
|
||||
Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::PlottingObject) = gui(plt)
|
||||
|
||||
# ---------------------------------------------------------
|
||||
|
||||
|
||||
function __init__()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user