Change Plots.jl backend dynamically

I want to plot a figure using all backends. I do this:

function plots_all_formats()
    plots("png")
    plots("html")
    plots("svg")
end
function plots(format)
    # load data
    # ...

    if format == "html"
        plotlyjs() # plotlyjs can export interactive plots in html
    elseif format == "svg"
        pyplot() # pyplot has better svg export
    else # gr is the quickest renderer and is perfect for pngs
        gr()
    end
    plot_function(format, data) # makes the plot and pass it to print_output
end
function print_output(path, p, format)
    savefig(p, joinpath(PLOTS_PATH, path*"."*format))
end

But it seems that the backend takes some time to change and I do not get reproducible results. Sometimes I get errors like broken pipe or ERROR: only png or svg allowed. got: :html but it does work when I do it manually in the REPL. Did someone encountered the same problem?

elseif format == "svg"
        pyplot() # pyplot has better svg export

Just for my understanding: What’s wrong with GR’s SVG export?

It’s quite good but the letters appear in individual groups with their root at the position (0,0) and this is very difficult to modify after with inkscape.

That’s because the default font is a vector font. If you use plot(..., font="Times_Roman") it will work as expected.