# Why is Julia's graphics system so slow?

**URL:** <https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750>\
**Category:** Performance\
**Tags:** plotting, ttfp\
**Created:** [September 25, 2021, 1:09pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750 "2021-09-25T13:09:29Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 14, 2021, 10:17pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/21 "2021-10-14T22:17:18Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 14, 2021, 11:07pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/22 "2021-10-14T23:07:54Z")

</div>

@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._

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 14, 2021, 11:20pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/24 "2021-10-14T23:20:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 14, 2021, 11:29pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/25 "2021-10-14T23:29:19Z")

</div>

Please check [this post for a visual explanation](https://discourse.julialang.org/t/plots-figure-size/37969/5).

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 15, 2021, 12:58am UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/26 "2021-10-15T00:58:36Z")

</div>

Well, its another bug. Qt backend is leaking pictures, and I can’t delete them ☹  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/4/04fc0ff3cc57f3c91174fa7ca4afbcab8aecbc94.png)  
I’ll stay a while with Pluto, and use vscode only to create packages.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 15, 2021, 3:47am UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/27 "2021-10-15T03:47:57Z")

</div>

this code worked as workaraund:

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

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 15, 2021, 10:32am UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/28 "2021-10-15T10:32:17Z")

</div>

> [@jcbritobr](#):
>
> Qt backend is leaking pictures

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.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 15, 2021, 7:30pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/29 "2021-10-15T19:30:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [March 16, 2022, 9:22am UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/30 "2022-03-16T09:22:37Z")

</div>

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

```julia
using Plots: default
default(fmt=:png)

```

makes no difference, it is still an SVG.

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

---

<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:** [March 16, 2022, 1:24pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/31 "2022-03-16T13:24:40Z")

</div>

maybe this should be split in a separate conversation, have a look at [Backends · Gadfly.jl](http://gadflyjl.org/stable/man/backends/#Backends)

---

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [March 16, 2022, 9:02pm UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/32 "2022-03-16T21:02:57Z")

</div>

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

---

<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:** [March 17, 2022, 7:53am UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/33 "2022-03-17T07:53:59Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)\
**Post date:** [March 17, 2022, 8:59am UTC](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750/34 "2022-03-17T08:59:14Z")

</div>

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

[Previous page](https://discourse.julialang.org/t/why-is-julias-graphics-system-so-slow/68750.md?page=1)
