将 PlotlyJS 绘图保存为 PNG (Julia)

发布于 2025-01-16 08:08:49 字数 3000 浏览 3 评论 0原文

我想使用 Julia 将 PlotlyJS 绘图保存为 PNG,但我不知道如何操作。 Julia 的 PlotlyJS 网页 指出“要保存图表以供外部查看,您可以这样做使用 savefig(p, filename::String) 方法”。但是,以下代码:

using PlotlyJS

p = plot(rand(10, 4))
PlotlyJS.savefig(p, filename="myimage.png")

由于“得到不支持的关键字参数“文件名”而导致错误,但使用

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,但这没有成功。

另外,基于 this 接受了 SO。答案,我可以将绘图保存为 HTML 文件,但我仍然需要将 HTML 文件转换或保存为 PNG。有人提出了解决此问题的方法 这里,但这仍然感觉很混乱。

最终,我想将 PlotlyJS 绘图保存为 PNG 文件。

I would like to save a PlotlyJS plot as a PNG using Julia, but I cannot figure out how. The PlotlyJS webpage for Julia states that "to save the chart for external viewing you can do so using the savefig(p, filename::String) method". However, the following code:

using PlotlyJS

p = plot(rand(10, 4))
PlotlyJS.savefig(p, filename="myimage.png")

results in an error due to "got unsupported keyword argument "filename", but using

PlotlyJS.savefig(p, "myimage.png")

results in an "access to undefined reference" error:

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

I have also tried defining a PyPlot figure, and opening/saving the image via PyPlot, but this did not work out.

Alternatively, based on this accepted SO answer, I can save the plot as an HTML file, but I still need to convert or save the HTML file to a PNG. A solution to this problem was proposed here, but this still feels messy.

Ultimately, I would like to save a PlotlyJS plot as a PNG file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2025-01-23 08:08:49

正如 Oskar 在他的评论中所说,您在 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") 是该函数针对您的目的的正确用法。作为让 Plotly 从文件扩展名猜测所需格式的替代方法,您可以传递 format = "png" 来创建 png 输出。

As Oskar says in his comment, there should be no issues with your second attempt on the current stable release of PlotlyJS, 0.18.8.

Doing

julia> using PlotlyJS

julia> savefig(plot(rand(10)), "test.png")

results in a png file being written to the current working directory for me.

Your first attempt passing a filename keyword didn't work because, as the error says, this keyword doesn't exist. It's useful to look at a function's method signatures using the REPL help mode (accessible by pressing ?):

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

(Note that here I've ommitted the first method defined for savefig for brevity).

This tells you that your second attempt, calling savefig(p, "myimage.png") is the correct usage of the function for your purpose. As an alternative to having Plotly guess the desired format from the file extension, you can pass format = "png" to create png outputs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文