Pluto plots with PlotlyBase/PlotlyJS with working static html export

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:

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

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.

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.

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.

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

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

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.

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 the new syntax to have titles in the right place in the default style.

layout=Layout(title_text="your title", title_x=0.5)
1 Like

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:

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:

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.

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:

Hi @disberd ,

When I tried your example I got this:

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:

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

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

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

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.

This notebook is working with PlotlyJS v0.18.3, which is the latest version. I checked and got this:
a1

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)

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.

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:

import PlotlyJS

and then

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.

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.

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

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.