Plots.jl icon indicating copy to clipboard operation
Plots.jl copied to clipboard

[BUG] savefig function and legend argument don't work with Plotly

Open nathanrboyer opened this issue 2 years ago • 2 comments

Details

julia> using Plots

julia> plotly()
Plots.PlotlyBackend()

julia> plot(1:3, 4:6, label="y", legend=:left)

julia> savefig("test.png")
ERROR: UndefVarError: savefig not defined
Stacktrace:
 [1] _show(io::IOStream, #unused#::MIME{Symbol("image/png")}, plt::Plots.Plot{Plots.PlotlyBackend})
   @ Plots C:\Users\nboyer.AIP\.julia\packages\Plots\hxZ0l\src\backends\plotlybase.jl:21
 [2] show
   @ C:\Users\nboyer.AIP\.julia\packages\Plots\hxZ0l\src\output.jl:214 [inlined]
 [3] png(plt::Plots.Plot{Plots.PlotlyBackend}, fn::String)
   @ Plots C:\Users\nboyer.AIP\.julia\packages\Plots\hxZ0l\src\output.jl:7
 [4] savefig(plt::Plots.Plot{Plots.PlotlyBackend}, fn::String)
   @ Plots C:\Users\nboyer.AIP\.julia\packages\Plots\hxZ0l\src\output.jl:123
 [5] savefig(fn::String)
   @ Plots C:\Users\nboyer.AIP\.julia\packages\Plots\hxZ0l\src\output.jl:128
 [6] top-level scope
   @ REPL[4]:1

image

The legend keyword argument is ignored, and the savefig function cannot be found. savefig works on PlotlyJS backend, but legend still doesn't.

Backends

This bug occurs on ( insert x below )

Backend yes no untested
gr (default) x
pyplot x
plotlyjs x
pgfplotsx x
unicodeplots x
inspectdr x
gaston x

Versions

Plots.jl version: v1.25.10 Backend version (]st -m <backend(s)>): v0.4.1 Output of versioninfo():

julia> versioninfo()
Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = code.cmd
  JULIA_NUM_THREADS =

nathanrboyer avatar Feb 17 '22 20:02 nathanrboyer

I'm getting this error too. I don't know how this made it past unit tests, because it's simple that using PlotlyBase doesn't allow you to use savefig anymore (i.e., it fails even without using legend):

(tmp) pkg> add Plots, PlotlyBase
# ...

julia> using Plots

julia> plotly()
Plots.PlotlyBackend()

julia> plt = plot(1:3, 4:6)

julia> savefig(plt, "test.pdf")
ERROR: UndefVarError: savefig not defined
Stacktrace:
 [1] _show(io::IOStream, #unused#::MIME{Symbol("application/pdf")}, plt::Plots.Plot{Plots.PlotlyBackend})
   @ Plots ~/.julia/packages/Plots/LI4FE/src/backends/plotlybase.jl:21
 [2] show
   @ ~/.julia/packages/Plots/LI4FE/src/output.jl:205 [inlined]
 [3] #277
   @ ~/.julia/packages/Plots/LI4FE/src/output.jl:20 [inlined]
 [4] open(::Plots.var"#277#278"{Plots.Plot{Plots.PlotlyBackend}}, ::String, ::Vararg{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Base ./io.jl:330
 [5] open
   @ ./io.jl:328 [inlined]
 [6] pdf(plt::Plots.Plot{Plots.PlotlyBackend}, fn::String)
   @ Plots ~/.julia/packages/Plots/LI4FE/src/output.jl:19
 [7] savefig(plt::Plots.Plot{Plots.PlotlyBackend}, fn::String)
   @ Plots ~/.julia/packages/Plots/LI4FE/src/output.jl:114
 [8] top-level scope
   @ REPL[4]:1

Presumably PlotlyJS works (but not PlotlyBase). It looks like PlotlyBase has removed their savefig function which Plots calls here, rather, suggesting to use PlotlyJS to export figures. Someone has pointed out that it's probably a bit bloated to have to load PlotlyJS to export figures, so they've made a PlotlySave package, which looks neat, and which exports savefig without loading PlotlyJS.

jakewilliami avatar Feb 21 '22 00:02 jakewilliami

... and which exports savefig without loading PlotlyJS.

Just to avoid misunderstandings or frustration; PlotlySave does not export savefig() in order not to collide with PlotlyJS's definition. It does export FileIO's save(filename, fig) instead, and it does provide the possibility to actively import savefig(). So in order to use old scripts, I'd recommend doing

using PlotlySave
import PlotlySave.savefig
savefig(myplot, "myplot.svg")

otherwise do

using PlotlySave
save("myplot.svg", myplot)

hhaensel avatar Feb 21 '22 21:02 hhaensel