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"
|
imgname = "$(pkgname)_example_$i.png"
|
||||||
|
|
||||||
# NOTE: uncomment this to overwrite the images as well
|
# 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 out the header, description, code block, and image link
|
||||||
write(md, "### $(example.header)\n\n")
|
write(md, "### $(example.header)\n\n")
|
||||||
|
|||||||
@ -340,7 +340,12 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"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!,
|
yticks!,
|
||||||
annotate!,
|
annotate!,
|
||||||
|
|
||||||
savepng,
|
savefig,
|
||||||
|
png,
|
||||||
gui,
|
gui,
|
||||||
|
|
||||||
backend,
|
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...)
|
function addExtension(fn::AbstractString, ext::AbstractString)
|
||||||
savepng(plt::PlottingObject, fn::AbstractString; kw...) = (io = open(fn, "w"); writemime(io, MIME("image/png"), plt); close(io))
|
try
|
||||||
# savepng(plt::PlottingObject, args...; kw...) = savepng(plt.backend, plt, args...; kw...)
|
oldext = getExtension(fn)
|
||||||
# savepng(::PlottingPackage, plt::PlottingObject, fn::AbstractString, args...) = error("unsupported") # fallback so multiple dispatch doesn't get confused if it's missing
|
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)
|
gui(plt::PlottingObject = current()) = display(PlotsDisplay(), plt)
|
||||||
|
|
||||||
|
|
||||||
# override the REPL display to open a gui window
|
# override the REPL display to open a gui window
|
||||||
Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::PlottingObject) = gui(plt)
|
Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::PlottingObject) = gui(plt)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function __init__()
|
function __init__()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user