我想使用朱莉娅将一个PlotlyJS图保存为PNG,但我不知道如何保存。朱莉娅的PlotlyJS网页声明“要保存图表以便外部查看,可以使用savefig(p,filename::String)方法”。然而,以下代码:
using PlotlyJS
p = plot(rand(10, 4))
PlotlyJS.savefig(p, filename="myimage.png")由于"got不受支持的关键字参数“"filename”而导致错误,但使用
PlotlyJS.savefig(p, "myimage.png")结果出现“访问未定义引用”错误:
ERROR: UndefRefError: access to undefined reference
Stacktrace:
[1] getproperty
@ .\Base.jl:42 [inlined]
[2] _kaleido_running
@ C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:176 [inlined]
[3] _ensure_kaleido_running()
@ PlotlyBase C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:177
[4] savefig(p::Plot{Vector{GenericTrace}, Layout{Dict{Symbol, Any}}, Vector{PlotlyFrame}}; width::Nothing, height::Nothing, scale::Nothing, format::String)
@ PlotlyBase C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:84
[5] savefig(io::IOStream, p::Plot{Vector{GenericTrace}, Layout{Dict{Symbol, Any}}, Vector{PlotlyFrame}}; width::Nothing, height::Nothing, scale::Nothing, format::String)
@ PlotlyBase C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:139
[6] (::PlotlyBase.var"#181#182"{Nothing, Nothing, Nothing, Plot{Vector{GenericTrace}, Layout{Dict{Symbol, Any}}, Vector{PlotlyFrame}}})(f::IOStream)
@ PlotlyBase C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:171
[7] open(::PlotlyBase.var"#181#182"{Nothing, Nothing, Nothing, Plot{Vector{GenericTrace}, Layout{Dict{Symbol, Any}}, Vector{PlotlyFrame}}}, ::String, ::Vararg{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Base .\io.jl:330
[8] open
@ .\io.jl:328 [inlined]
[9] savefig(p::Plot{Vector{GenericTrace}, Layout{Dict{Symbol, Any}}, Vector{PlotlyFrame}}, fn::String; format::Nothing, width::Nothing, height::Nothing, scale::Nothing)
@ PlotlyBase C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:170
[10] savefig(p::Plot{Vector{GenericTrace}, Layout{Dict{Symbol, Any}}, Vector{PlotlyFrame}}, fn::String)
@ PlotlyBase C:\Users\apung\.julia\packages\PlotlyBase\NxSlF\src\kaleido.jl:165
[11] savefig(p::PlotlyJS.SyncPlot, a::String; k::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ PlotlyJS C:\Users\apung\.julia\packages\PlotlyJS\WF333\src\PlotlyJS.jl:49
[12] savefig(p::PlotlyJS.SyncPlot, a::String)
@ PlotlyJS C:\Users\apung\.julia\packages\PlotlyJS\WF333\src\PlotlyJS.jl:49
[13] top-level scope
@ none:1我也尝试过定义一个PyPlot图形,并通过PyPlot打开/保存图像,但这没有成功。
或者,基于这接受所以答案,我可以将情节保存为一个HTML,但我仍然需要将该HTML转换或保存为一个PNG。这里提出了解决这个问题的方法,但这仍然让人觉得很混乱。
最后,我想将一个PlotlyJS图保存为一个PNG文件。
发布于 2022-03-23 09:39:01
正如奥斯卡在他的评论中所说的,你第二次尝试当前稳定发布的PlotlyJS ( 0.18.8 )应该不会有问题。
正在做什么
julia> using PlotlyJS
julia> savefig(plot(rand(10)), "test.png")结果为我将一个png文件写入当前工作目录。
第一次传递filename关键字的尝试没有成功,因为正如错误所述,该关键字不存在。使用REPL帮助模式(通过按?访问)查看函数的方法签名非常有用:
help?> savefig
search: savefig StackOverflowError
(...)
savefig(
p::Plot, fn::AbstractString;
format::Union{Nothing,String}=nothing,
width::Union{Nothing,Int}=nothing,
height::Union{Nothing,Int}=nothing,
scale::Union{Nothing,Real}=nothing,
)
Save a plot p to a file named fn. If format is given and is one of png, jpeg, webp, svg, pdf, eps, json, or html; it will be the format of the file. By default the format is guessed from the extension of fn.
scale sets the image scale. width and height set the dimensions, in pixels. Defaults are taken from p.layout, or supplied by plotly(请注意,这里我省略了为savefig定义的第一个简洁方法)。
这告诉您,您的第二次尝试--调用savefig(p, "myimage.png") --是正确使用函数的目的。作为一种替代方法,您可以传递format = "png"来创建png输出,而不是让Plotly猜测文件扩展名中所需的格式。
https://stackoverflow.com/questions/71578549
复制相似问题