ANN: static image export in PlotlyBase.jl

Hi all!

I’m excited to announce version 0.4 of PlotlyBase.jl which adds static image export that does not require Blink.jl, or any external web process! This can easily run on remote machines without a display.

We have integrated with plotly’s new C++ based image exporter kaleido to provide a fast and seamless experience for Plotly users in Julia.

This new functionality makes the ORCA.jl package obsolete and no longer required for exporting figures to files.

To use it make sure you have the latest PlotlyBase (or PlotlyJS, which uses PlotlyBase under the hood) installed on your system and then do:

using PlotlyBase  # or using PlotlyJS

# construct a plot -- this is just random lines
p = Plot(rand(10, 4))   # or `plot(...)` if you used PlotlyJS

savefig(p, "myfile.png")
savefig(p, "myfile.pdf")
savefig(p, "myfile.jpg")

# other file formats... 

Please take it for a spin let us know how it works!

18 Likes

Hi Spencer,

This is exciting. I remember speaking with you about the problems with Plotly depending on Electron many years ago. So thanks for doing this (and thanks to the Plotly folks as well)

It may be worth making a separate package out of this, since I imagine other packages might want to use this.

Also, I see that the plotly folks provide pre-compiled binaries on linux64/mac/win64, which satisfies most of our users. However, I do hope we can get BinaryBuilder to build for all our other platforms.

1 Like

That would be great!

I know the plotly team has been working towards more operating system coverage (just released win32 today). I bet they’d love any help the BinaryBuilder julia community can provide.

Also, PlotlyBase has very light dependencies. Only JSON, LatexStrings, Requires, and DocStringExtensions – is this light enough to be that a separate package you mentioned? Or were you specifically thinking Kaledio.jl?

PlotlyJS.jl is the one that imports this and layers in the Electron stuff.

Yeah, I was thinking of a potential Kaledio.jl. But I’m being very speculative, so take it with a grain of salt.

For me it is in both cases (“PlotlyBase” & “PlotlyJS”, v0.14.0) a capital “P” for the Plot-command.
“savefig” does not work for me.
I need to add the module name to the method: PlotlyBase.savefig() or PlotlyJS.savefig()
Do I miss something?
Here is my example:

println("Hello, World!")
using PlotlyBase
# using PlotlyJS
p_ = Plot(sin, -2pi, pi, label="sine function")
PlotlyJS.savefig(p_, "/media/data/data/julia/scripts/new_plot.pdf")
PlotlyBase.savefig(p_, "/media/data/data/julia/scripts/PlotlyBase_plot.pdf")
println("End!")

Hi @ellocco

can you show the output of running that script and let me know which operating system you are using ?

Thank you

Hi @sglyon,

it is some kind of voodoo. It seems a reboot of the machine cured the issue
with “savefig”.
I am working under Ubuntu 20.04.
Also the issue with a small “p” for “PlotlyJS” disappeared.
Both seem to be case-insensitive.

Regards,

Stefan

Thank you for nice update of the package.
I tried to control the size of saved figure but it worked for me only when I saved it as html.

using PlotlyBase
data = scatter(;y=rand(10))    
layout = Layout(;width  = 480, height = 480)

pp = Plot(data, layout)

savefig(pp, "test.html")
savefig(pp, "test.pdf")

Here is screenshot, html on the left-hand side, pdf on the right-hand side.

Hmm, I’m surpised by this. can you post the output?

I put the output to my previous post.

Hi. I am using PlotlyJS v0.14.1, PlotlyBase v0.4.3, WebIO v0.8.15, and Julia v 1.6.1. Moreover, I am on a Windows 10 machine.

I have encountered a problem when I save a particular figure in pdf or html formats. Not always, but in some cases, what we see when the figure is displayed on the Electron window is quite different from what we get if we save the figure in any of those two formats (at least the piece of code is exactly the same, apart from the savefig()).

As an MWE, I will use a fine piece of code I found in the Julia discourse exchanges (I run this in VSCode)

using PlotlyJS, Dates

dates = [Date(2021, 05, 08), Date(2021, 05, 09), Date(2021, 05, 10)]
trace1 = bar(; x = dates, y = [15, 1, 6], name = "Buy - euro", barmode="stack", legendgroup = "euro", marker_color = "Orange")
trace2 = bar(; x = dates, y = [2, 5, 4], name = "Buy - usd", barmode="stack", legendgroup = "usd", marker_color = "Green")
tracesBuy = [trace1, trace2]

p1 = plot(tracesBuy, Layout(;barmode="stack", title="Buy"))

trace3 = bar(; x = dates, y = [-10, -5, -8], name = "Sell - euro", barmode="stack", legendgroup = "euro", showlegend = false, marker_color = "Orange")
trace4 = bar(; x = dates, y = [-15, -6, -3], name = "Sell - usd", barmode="stack", legendgroup = "usd", showlegend = false, marker_color = "Green")
tracesSell = [trace3, trace4]
p2 = plot(tracesSell, Layout(;barmode="stack", title="Sell"))
p = [p1  p2]
p.plot.layout["barmode"] = "stack"
p
#savefig(p, "C:/Users/Utilizador/Desktop/aaaFig1_PlotlyJS.html")

If I run the code without saving anything, this is the output I get:

If instead, I save the figure as a pdf or an html file, these are the outputs (the html remains interactive what is nice):

The html output:

and the pdf output:

Is there anything I should do to avoid this distortion of the original figure? @sglyon Thanks, and congrats on the wonderful package you have developed.

1 Like
savefig(pp, "test.pdf", width = 480, height = 480)

did the job:-)

@Oto_Brzobohaty Thanks. Unfortunately, your solution does not work in my case. At least in VSCode. In both pdf and html formats, the outputs keep the same looking as above.

Sorry that was answer to @sglyon for my older issue not for you.

Meanwhile savefig functionality has moved to the PlotlyJS package. For those who tend to use PlotlyBase without PlotlyJS, I’ve writeen the rather light-weight package PlotlySave

4 Likes

The package is meanwhile registered and can be installed via

]add PlotlySave