# Pluto plots with PlotlyBase/PlotlyJS with working static html export

**URL:** <https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730>\
**Category:** Pluto\
**Created:** [May 24, 2021, 4:24pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730 "2021-05-24T16:24:58Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 5, 2021, 10:03am UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/21 "2021-07-05T10:03:10Z")

</div>

In any case I will leave here (and select as solution) the github reference of the notebook I will use to test/update the show function so the latest version is always up-to date there:

> <https://github.com/disberd/PlutoUtilities.jl/blob/master/src/plotly_show.jl>

---

<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:** [July 5, 2021, 10:28am UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/22 "2021-07-05T10:28:36Z")

</div>

@disberd. Brilliant. It keeps your latest approach’s speed, responsiveness, and reactivity, and the problems with layouts are gone. Just perfect!

---

<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:** [August 2, 2021, 10:34am UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/23 "2021-08-02T10:34:40Z")

</div>

Hi @disberd,  
I am using your latest solution, and I have two minor issues. The first is that the titles in individual plots are aligned on the left-hand side of the plot, but aligned at the center when we have subplots (please see figure below). The second, which is quite nice, is that plots are now produced with ggplot style. If I need the default style what do I need to do? Thanks a lot.

 ![aaa](https://global.discourse-cdn.com/julialang/original/3X/f/9/f9b97a5f1d86981770f7e4990ee3e088d4bd10ad.png)

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [August 2, 2021, 12:22pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/24 "2021-08-02T12:22:42Z")

</div>

Hi tried to have a look and I think both problems are caused by some change in behavior in PlotlyBase.  
I noticed that there have been a lot of updates in the past weeks on the codebase of PlotlyBase to add functionalities and I didn’t go through the code in detail to see what changed.

If I try to do plot fixing the version of PlotlyBase at 0.6.3 (latest version I had installed on my main environment) everything works as before.  
With the newest version there are these templates which could most likely be tweaked to get back what you wanted but I don’t know yet how you could do it.

---

<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:** [August 2, 2021, 12:33pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/25 "2021-08-02T12:33:27Z")

</div>

Yep, I noticed that there have been some recent upgrades in both PlotlyJS and PlotlyBase. In the past, in the docs of PlotlyJS, there was an entry to show how to change the default style. I can not find it anymore. Thanks.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [August 2, 2021, 12:52pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/26 "2021-08-02T12:52:05Z")

</div>

I tried playing a bit, and it seems you can provide templates as an additional keyword arguments to the Layout constructor as here:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b884b4c4c6503835e3f0ec1450daa7ddf81b894.png)

Default template is `:plotly` which has the _bug_ of non-centered title.

It seems that the template `PlotlyBase.templates[:gridon]` is the default one before the update to PlotlyBase, so you could use that to have plots looking like before templates

---

<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:** [August 2, 2021, 6:43pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/27 "2021-08-02T18:43:06Z")

</div>

Thanks a lot for your help. You are right: the problem is in default style in the new version of PlotlyBase. As usual, you make it relatively easy to choose from many alternative ways to solve a problem.

---

<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:** [August 3, 2021, 2:53pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/28 "2021-08-03T14:53:15Z")

</div>

Hi @disberd, I had raised an issue next to PlotlyJS about the title coming out in the left-hand corner of individual plots. @empet explained [here](https://github.com/JuliaPlots/PlotlyJS.jl/issues/405) the new syntax to have titles in the right place in the default style.

```julia
layout=Layout(title_text="your title", title_x=0.5)

```

---

<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:** [August 7, 2021, 3:47pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/29 "2021-08-07T15:47:36Z")

</div>

Hi @disberd, sorry for bothering you again with Pluto and PlotlyJS issues. I am using the latest versions of PlotlyJS (v0.18.3), PlotlyBase (v0.8.5), and Pluto (v0.15.1). I can save figures using PlotlyJS in VSCode in the usual way:

```julia
savefig(plot1, "coco.pdf")

```

But I can not do this anymore using either your first solution to integrate PlotlyJS into Pluto, or the latest one. I get the following error:

```julia
UndefVarError: savefig not defined
	1. top-level scope @ Local: 12

```

When you came up with your first solution, I remember pretty well that I had no problems saving figures in pdf, svg, png, and html formats (I still have examples of my experiments with them). Now I can not, and this may be related to the recent upgrades of PlotlyJS and PlotlyBase or the latest Pluto version. Some help will be deeply appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [August 8, 2021, 8:32am UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/30 "2021-08-08T08:32:07Z")

</div>

Hi @VivMendes.

savefig was moved from PlotlyBase to PlotlyJS recently (from v0.8.0 of PlotlyBase and v0.18.1 of PlotlyJS).

I don’w know what is your exact forkflow for this in Pluto, but my code above always assume to only have PlotlyBase loaded in the notebook.  
If you want to correctly use `savefig`, you now have to load PlotlyJS as well.

This works for me at the moment:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/6/56ce29ba1518c92d6bf69acf206d75e3ea5d1577.png)

---

<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:** [August 8, 2021, 11:16am UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/31 "2021-08-08T11:16:52Z")

</div>

Hi @disberd ,

When I tried your example I got this:

 ![a1](https://global.discourse-cdn.com/julialang/original/3X/2/1/216841c265924557c690d4caeba1fc77af7833a9.png)

There is something wrong with my installation. In VSCode I can save figures using PlotlyJS, so the `savefig` function is there and working OK. It is in Pluto that I can not make it now, not even when using your first solution to integrate PlotlyJS into Pluto, which worked pretty well. Is it related to your package PlutoUtils?

I made a stupid mistake when trying to install your package. Because it is called PlutoUtils I googled it and went to the wrong address:

> **[GitHub - fonsp/PlutoUtils.jl: Nifty scripts to go with Pluto.jl](https://github.com/fonsp/PlutoUtils.jl)**
>
> Nifty scripts to go with Pluto.jl. Contribute to fonsp/PlutoUtils.jl development by creating an account on GitHub.

I did not check it carefully, installed and then removed it afterward when I realized that it was not your package. But now the REPL is throwing this error when trying to install your PlutoUtils from here:

[GitHub - disberd/PlutoExtras.jl: Random Pluto Utilities](https://github.com/disberd/PlutoUtils.jl)

```julia
(@v1.6) pkg> add https://github.com/disberd/PlutoUtils.jl
    Cloning git-repo `https://github.com/disberd/PlutoUtils.jl`
   Updating git-repo `https://github.com/disberd/PlutoUtils.jl`
  Resolving package versions...
ERROR: Unsatisfiable requirements detected for package PlutoDevMacros [a0499f29]:
PlutoDevMacros [a0499f29] log:
├─PlutoDevMacros [a0499f29] has no known versions!
└─restricted to versions * by PlutoUtils [ed5d0301] — no versions left
  └─PlutoUtils [ed5d0301] log:
    ├─possible versions are: 0.3.3 or uninstalled
    └─PlutoUtils [ed5d0301] is fixed to version 0.3.3 

```

Gosh, I made a mess out of it.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [August 8, 2021, 11:53am UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/32 "2021-08-08T11:53:05Z")

</div>

No it shouldn’t be related to my package,  
What version do you have of PlotlyJS installed in the notebook above where you had the error? (You can check the version by clicking on the tick next to the name).  
It might be (if this was not a new notebook but one you have been using for a while) that the version of PlotlyJS was fixed to the one that was available when you first created the notebook.  
This is the default behavior with the new Pluto pkgmanager (only update package after manual trigger by the user)  
You can update packages by clicking on the tick and pressing the button with the up arrow button next to the document looking icon:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/1/8114335dc78f4f5a314a2c17eae0c90b313a016d.png)

I guess the error you are getting in including PlotlyJS is because of this reason, my own PlutoUtils package should not have anything to do with it.  
The error you got when trying to install my package is my fault, I removed the dependency to a package not in General now, but again you shouldn’t need my package to make the plot work, the only thing my package does with respect to plotting is directly creating the Base.show methods for PlotlyBase.Plot objects.

---

<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:** [August 8, 2021, 12:37pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/33 "2021-08-08T12:37:22Z")

</div>

This notebook is working with PlotlyJS v0.18.3, which is the latest version. I checked and got this:  
 ![a1](https://global.discourse-cdn.com/julialang/original/3X/b/e/bec57504f00b08d755c1e99b423313bf5207089d.png)

I have also tried in the first notebooks where I used your first solution and did many experiments with savefigs (those notebooks were developed with the previous versions of PlotlyJS and PlotlyBase), get the same PlotlyJS v0.18.3 working there and the same error: `savefig not defined`.

I have just tried with an entirely new notebook from scratch and got exactly the same error. I stopped Julia and restarted and got the same error. Can it be related to some conflict with the packages I am using in these notebooks (I have finished the 10th, out of 12 notebooks for teaching)

```julia
begin
	using PlotlyBase
	using HypertextLiteral
	using PlutoUI
	using CSV
	using DataFrames
	using LinearAlgebra
	using Dates
	using MonthlyDates
	using SparseArrays
	using StatsBase
	using LaTeXStrings
	using FileIO
	using ImageIO
	using ColorVectorSpace
	using Images
	using ImageTransformations
end

```

Thanks a lot for your help.

---

<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:** [August 8, 2021, 3:10pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/34 "2021-08-08T15:10:24Z")

</div>

Hi,

After many trials and errors, I have figured out how to do it in the new version of PlotlyJS and Pluto. Instead of `import PlotlyJS : savefig` , I did:

```julia
import PlotlyJS

```

and then

```julia
PlotlyJS.savefig(p10_2, "inflation.pdf")

```

I do not know whether this problem is related to the fact that the new version of Pluto already produces plots using PlotlyBase, without directly requiring the explicit use of your solutions. The only problem is that the plots look pretty ugly and useless to display in the notebook. But when they are saved using `PlotlyJS.savefig(., ".")`, the pdf figures display the correct plots.

Gosh, I have no idea of what is going on in Pluto. That savefig function looks like the old style of plotting. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [August 8, 2021, 3:41pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/35 "2021-08-08T15:41:22Z")

</div>

It is very strange that you couldn’t import directly savefig from PlotlyJS. I doub’t it’s a Pluto problem but more something with your configuration.

Regarding plots with Plotlybase without requiring my solution, it’s probably because I think the default `show` method is to simply show the static png/jpg image when you show a plot in Pluto, which makes the output a static image that is not interactive.

---

<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:** [August 8, 2021, 4:51pm UTC](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730/36 "2021-08-08T16:51:49Z")

</div>

I tried on another computer, with a fresh Julia installation, and **your code does indeed solve the problem** :

```julia
import PlotlyJS:savefig

```

with the traditional `savefig()` function to save the plot.

To solve the problem I tried many variations on this issue on my computer, that there must be something mad running at the back. I will sort it out. Thanks.

[Previous page](https://discourse.julialang.org/t/pluto-plots-with-plotlybase-plotlyjs-with-working-static-html-export/61730.md?page=1)
