# ANN: static image export in PlotlyBase.jl

**URL:** <https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945>\
**Category:** Package Announcements\
**Tags:** package, announcement, plotting, plotlyjs\
**Created:** [August 14, 2020, 4:53pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945 "2020-08-14T16:53:25Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [August 14, 2020, 4:53pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/1 "2020-08-14T16:53:25Z")

</div>

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](https://github.com/plotly/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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [August 15, 2020, 8:44pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/2 "2020-08-15T20:44:06Z")

</div>

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](https://github.com/JuliaPackaging/Yggdrasil/issues/1483) for all our other platforms.

---

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [August 16, 2020, 3:10am UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/3 "2020-08-16T03:10:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [August 16, 2020, 1:28pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/4 "2020-08-16T13:28:36Z")

</div>

> [@sglyon](#):
>
> Or were you specifically thinking Kaledio.jl

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

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [August 28, 2020, 8:34am UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/5 "2020-08-28T08:34:36Z")

</div>

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!")`

---

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [August 28, 2020, 10:22pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/6 "2020-08-28T22:22:33Z")

</div>

Hi @ellocco

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

Thank you

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [August 29, 2020, 10:45am UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/7 "2020-08-29T10:45:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [September 12, 2020, 7:01pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/8 "2020-09-12T19:01:47Z")

</div>

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.

```julia
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.

 ![Snímek obrazovky 2020-09-13 v 12.07.28](https://global.discourse-cdn.com/julialang/original/3X/e/f/efc6d95cd78e85354f6e50d5e436518a9d586c74.png)

---

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [September 12, 2020, 8:52pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/9 "2020-09-12T20:52:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [September 13, 2020, 10:10am UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/10 "2020-09-13T10:10:21Z")

</div>

I put the output to my previous post.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [May 14, 2021, 8:23pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/11 "2021-05-14T20:23:51Z")

</div>

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)

```julia
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:

 ![aaaPlotlyJS_0](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce5bad174ded3c51a7832b93400c239d774610d8.png)

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:

 ![aaaPlotlyJS_1](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d0409d22714805385de86f6d50064ea25f86b85.png)

and the pdf output:

 ![aaaPlotlyJS_2](https://global.discourse-cdn.com/julialang/original/3X/6/d/6db516b5b41692e5273c220298e1479a1ae9b97c.png)

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.

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [May 15, 2021, 4:28pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/12 "2021-05-15T16:28:10Z")

</div>

```julia
savefig(pp, "test.pdf", width = 480, height = 480)

```

did the job:-)

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [May 17, 2021, 10:35pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/13 "2021-05-17T22:35:03Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [May 18, 2021, 6:12am UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/14 "2021-05-18T06:12:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [January 20, 2022, 6:33am UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/15 "2022-01-20T06:33:46Z")

</div>

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](https://github.com/hhaensel/PlotlySave.jl)

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [February 5, 2022, 3:38pm UTC](https://discourse.julialang.org/t/ann-static-image-export-in-plotlybase-jl/44945/16 "2022-02-05T15:38:14Z")

</div>

The package is meanwhile registered and can be installed via

```julia
]add PlotlySave

```
