Saving plot to html

Hello,
First time poster, long time lurker!

We are using docker and we wait to avoid local X windows installation. Display() tries to use xdb-show, which we cannot have. We want to save the contents of the figure in html (it’s exactly the same .html that gets created in /tmp/ when calling display()), so how can this be achieved? We tried savefig etc., but we can’t see to find a way to save the plot in html. Thanks a lot!

2 Likes

It might be useful specify what library you’re using and what you mean by “save to HTML”, let’s say you’re using PyPlot, this will work:

using PyPlot
ioff() # turn off display
x = range(0, 1, length=100)
y = x .^ 2
plot(x, y)
savefig("foo.png")

Then in HTML

<html>
<body>
<img src="foo.png" alt="Foo" style="width:50%">
</body>
</html>

which gives

1 Like

If you are talking about saving data and plotting instructions with HTML, PlotlyJS.jl or Plotly.jl, or Plots.jl with Plotly backend may be useful.

@Zhong_Pan Hello and thanks for the reply, can you be more specific please?

Hey, thanks for the reply. The problem is that saving to png will remove the “interactivity” that display() offers. My code is the one below…

using DelimitedFiles
using Plots
using Statistics

# parse output of execution
M = readdlm("./run.log", ',', Float64, skipstart=1);

# select plotly as backend and open figure
plotly(ticks=:native)
plt = plot();
# Binet
F(n) = convert( Int64, round(((1+sqrt(big(5)))^n-(1-sqrt(big(5)))^n)/(sqrt(big(5))*big(2)^n),digits=0))

# draw different curve for differet number of processors
npList = unique(M[:,1]);
for i in 1:length(npList)
    ind = npList[i] .== M[:,1];
    x = M[ind,2]
    @assert all( (F.(x) - M[ind,3]) .== 0 )
    y = vec(mean(M[ind,5:end], dims=2))
    plot!(plt, x, y, label="np = $(npList[i])",
          markershape=:xcross)
end


# fix plot attributes & display
plot!(title = "Time to compute fib(n)", xlabel = "n", ylabel = "time (sec)",
      xscale = :log, yscale = :log)
display(plt);

So diplay() offers interactivity with the plot, opposed to the png. So when running this, without having X windows installed, you get an error that says that xdb was not able to load /tmp/randomfile.html. That html file is what I want to save in a timely manner!

I’m not sure what you’re suggesting is even possible but In the meantime if you want interactivity in the browser you can use PlotlyJS which will work quite well for things like panning, zooming etc

As an example, here: Work with Plotly you can see how a plotly generated plot can be inserted in a webpage, you don’t have to use Franklin, you could just imitate the procedure

1 Like

For static/non-interactive plots, you can also try to save to SVG and then just render it in a browser directly, or embed it in a trivial HTML (depending on your application).

What I frequently do is to, first, suppress the need of a X server, using:

ENV["GKSwstype"]="nul" 

Then I save the figures in pdf format (sometimes automatically copy it from the server to the machine where I am working). Most pdf readers update the files automatically when they are modified. I usually do this even with a X server available, because the rendering of the figure assumes exactly its final appearance.

1 Like

Hello,

I managed to solve with the following:

plotly()
savefig("plot.html");

All interactivity is saved!

3 Likes