# Saving plot to html

**URL:** <https://discourse.julialang.org/t/saving-plot-to-html/48029>\
**Category:** General Usage\
**Created:** [October 9, 2020, 12:00pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029 "2020-10-09T12:00:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnase](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@bnase](https://discourse.julialang.org/u/bnase)\
**Post date:** [October 9, 2020, 12:00pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/1 "2020-10-09T12:00:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [October 9, 2020, 12:34pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/2 "2020-10-09T12:34:37Z")

</div>

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:

```julia
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
<html>
<body>
<img src="foo.png" alt="Foo" style="width:50%">
</body>
</html>

```

which gives

 ![Screenshot 2020-10-09 at 14.34.01](https://global.discourse-cdn.com/julialang/original/3X/1/6/16e83a024c155f656389a43f6a66a7632f6519da.png)

---

<div class="post-metadata">

**Author:** ![Zhong\_Pan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhong_pan/32/19872_2.png) [@Zhong\_Pan](https://discourse.julialang.org/u/Zhong_Pan)\
**Post date:** [October 9, 2020, 3:55pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/3 "2020-10-09T15:55:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bnase](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@bnase](https://discourse.julialang.org/u/bnase)\
**Post date:** [October 10, 2020, 8:14am UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/4 "2020-10-10T08:14:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bnase](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@bnase](https://discourse.julialang.org/u/bnase)\
**Post date:** [October 10, 2020, 8:18am UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/5 "2020-10-10T08:18:25Z")

</div>

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…

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

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [October 10, 2020, 12:00pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/6 "2020-10-10T12:00:40Z")

</div>

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](https://franklinjl.org/extras/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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 11, 2020, 9:24am UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/7 "2020-10-11T09:24:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 11, 2020, 1:02pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/8 "2020-10-11T13:02:23Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![bnase](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@bnase](https://discourse.julialang.org/u/bnase)\
**Post date:** [October 12, 2020, 5:14pm UTC](https://discourse.julialang.org/t/saving-plot-to-html/48029/9 "2020-10-12T17:14:40Z")

</div>

Hello,

I managed to solve with the following:

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

```

All interactivity is saved!
