Why is Julia's graphics system so slow?

I’m having this issue when plotting monte carlo simulations with more than 10^5 random points. I will really apreciate if someone knows how to configure the Plots output to png. SVG is really good for quality, but for some plots with huge random points there are huge performance loss. Vscode dont support large svg files.

You should be able to run:

using Plots
default(fmt=:png)

And this should create png output.

4 Likes

Thank you. That save my work here.

Last I checked, Jupyter shows SVG by default.

In PyPlot I worked around this by defining showable(::MIME"image/svg+xml", ::PyPlot.Figure) = false by default (which prevents that MIME type from being sent to Jupyter, even though the SVG show method exists), with a setting to enable SVG output if desired

even with png, vscode hangs :frowning: Too much memory for that

Pluto can plot this withou any issues.

By any chance are there still old svg plots in the VSCode plot pane? Like if you click the arrows, can you see the plots you made before changing the output to png? If so, try closing them.

No chance. I restarted vscode and test it again. I takes a lot of memory. The plot will work but it will consume your machine memory until it crashes. It hapens with the code below:

using Plots, Roots, Random, LinearAlgebra
default(fmt=:png)
Random.seed!()

N = 10^5
data = [[rand(), rand()] for _ in 1:N];
indata = filter((x) -> (norm(x) <= 1), data);
outdata = filter((x) -> (norm(x) > 1), data);

pi_appox = 4 * length(indata) / N;
println("Pi estimate: ", pi_appox);

scatter(first.(indata), last.(indata), c=:blue, ms=1, msw=0);
scatter!(first.(outdata), last.(outdata), c=:red, ms=1, msw=0, xlims=(0, 1), ylims=(0, 1), legend=:none, ratio=:equal)

@jcbritobr, tried your code and confirmed the VS Code plot pane memory hog (Win10). However, it works very smoothly when setting VS code to plot to external window.

NB: your question should probably be split into a separate topic concerning VS Code plots.

1 Like

May you please explain how to plot in external window using vscode? I’ll open a new topic about this and maybe fill another bug in julia vscode.

Please check this post for a visual explanation.

2 Likes

Well, its another bug. Qt backend is leaking pictures, and I can’t delete them :frowning:
image
I’ll stay a while with Pluto, and use vscode only to create packages.

this code worked as workaraund:

p2 = scatter!(first.(outdata), last.(outdata),
    xlabel="x", ylabel="y",
    c=:red, ms=1, msw=0, xlims=(0, 1), ylims=(0, 1), legend=:none, ratio=:equal);

display("image/png", p2)
1 Like

What do you mean? Running your code above in VS Code using Plots; gr() shows a single GKS QtTerm window, to which all new plots are sent to.

I’m running in windows 10 64, julia 1.6.3. The Gks window is storing all plots I run. And each time plot is generated, the memory of old plot is not released.

How do I get Gadfly to use PNG rather than SVG?

using Plots: default
default(fmt=:png)

makes no difference, it is still an SVG.

display("image/png", h)

is not supported for Gadfly objects.

Also, it’s not true that SVG is inherently slow. If you save the SVG and drag it onto your brower it displays in an instant.

maybe this should be split in a separate conversation, have a look at Backends · Gadfly.jl

1 Like

The Gadfly backends are for writing files. I’m trying to render in VSCode.

Your message didn’t mention VSCode (also, VSCode renders SVG so I’m not sure why you’d want to do this in general apart if the SVG is extremely complex and that’s causing issues). Anyway, here’s one way you could do it:

using Fontconfig, Cairo, Gadfly
import Base.show
struct PNGPlot
    p::Gadfly.Plot
end
Base.show(io::IO, ::MIME"image/png", pp::PNGPlot) = draw(PNG(io), pp.p)

png(p) = PNGPlot(p)

plot(y=[1,2,3]) |> png
1 Like

Sorry, I thought this thread was about plotting performance in interactive environments which is why I didn’t repeat that information. I have plots that take a minute to display, even after the Gadfly object has been created. Scrolling through plots, saving and deleting them in the plot windows is also very painful. From the discussion above I thought it might be the VSCode handling of SVGs (as I mentioned browsers have no such problem with SVGs). Your elegant and clever code doesn’t improve the initial rendering but does make working with the plots in the plot windows much nicer (and taught me something about Julia).

1 Like