1

With Gadfly it is possible to combine plots in a grid and save the combined plot in a file:

using Gadfly, Compose

x=1:0.1:5;
grid = Array(Compose.Context, (2, 2));
grid[1,1] = render(plot( x=x, y = x, Geom.line));
grid[1,2] = render(plot( x=x, y = x.^2, Geom.line));
grid[2,1] = render(plot( x=x, y = log(x), Geom.line));
grid[2,2] = render(plot( x=x, y = sqrt(x), Geom.line));
draw(SVG("example.svg", 100mm, 100mm),  gridstack(grid));

I wrote a function that creates such a plot and this function creates a file. It is a little bit unintuitive for someone who wants to use this function why exactly this function creates a file, while the results of all the other plotting functions with single plots are displayed directly in the browser.

So, is it possible to call a function (in place of draw?) such that the combined plot defined by the grid is displayed directly in the browser like "normal" plots?

esel
  • 901
  • 9
  • 20

1 Answers1

3
function as_temp_html(gadflyplot)
    thefilepath=tempname() * ".html"
    write(open(thefilepath,"w"),stringmime("text/html",gadflyplot))
    return thefilepath
end

You can open this file in your browser by re-using Gadfly's crossplatform "open_file" method:

function open_file(filename)
    if is_apple()
        run(`open $(filename)`)
    elseif is_linux() || is_bsd()
        run(`xdg-open $(filename)`)
    elseif is_windows()
        run(`$(ENV["COMSPEC"]) /c start $(filename)`)
    else
        warn("Showing plots is not supported on OS $(string(Compat.KERNEL))")
    end
end

EDIT: now the answer uses Gadfly-exclusive code

Felipe Lema
  • 2,700
  • 12
  • 19
  • Thanks. OK, so there seems to be no other option than to create a temporary html file and open it in the browser myself. I found that Gadfly has the almost identical mechanism for this (see https://github.com/dcjones/Gadfly.jl/blob/master/src/Gadfly.jl). I will accept your answer if you change the `open_browser_window` function to the `open_file` function of Gadfly and replace the link to the Plots package with a link the Gadfly repository. It doesn't really make sense to use one plotting package to display the plot of another plotting package, if the functionality is provided there as well. – esel Sep 12 '16 at 06:58
  • well there are tools for doing the same without writing to disk (process pipes, websockets), but as far as I know these haven't been worked to do what you're asking for (live-editing-and-update code-and-plots or fetch-updated-resunt-from-the-browser). Take a look at [Interact.jl](https://github.com/JuliaLang/Interact.jl), though... might become useful – Felipe Lema Sep 12 '16 at 12:20