# Savefig() in Plots.jl is slow when I scatter 13056 points

**URL:** <https://discourse.julialang.org/t/savefig-in-plots-jl-is-slow-when-i-scatter-13056-points/54443>\
**Category:** General Usage\
**Tags:** package\
**Created:** [February 2, 2021, 8:44am UTC](https://discourse.julialang.org/t/savefig-in-plots-jl-is-slow-when-i-scatter-13056-points/54443 "2021-02-02T08:44:40Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![SeSodesa](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@SeSodesa](https://discourse.julialang.org/u/SeSodesa)\
**Post date:** [August 6, 2021, 8:37am UTC](https://discourse.julialang.org/t/savefig-in-plots-jl-is-slow-when-i-scatter-13056-points/54443/21 "2021-08-06T08:37:05Z")

</div>

I wish this was just an issue only caused by large datasets. On Plots.jl version 1.20.0, just 10 scatter points and a straight line drawn based on the parameters returned by LsqFit.jl seems to be extremely slow, regardless of the output format:

```julia
 8.972615 seconds (20.30 M allocations: 1.104 GiB, 4.26% gc time, 2.74% compilation time)

```

The code that generates these numbers goes as follows:

```julia
function draw_scatter_and_line(
    xdata,
    ydata,
    gradient,
    constant_term,
    scatter_title,
    line_title,
    xlabel,
    ylabel,
    filename
)
    img_path = joinpath(@ __DIR__ , filename)
    
    # Generate plot object
    img = Plots.plot(
        xlabel=xlabel,
        ylabel=ylabel,
        legend=:bottomright)

    # Measurements
    Plots.scatter!(
        img,
        xdata,
        ydata,
        label=scatter_title,
        color=:steelblue)

    # Generate fit
    fit_x = [xdata[begin] - abs(xdata[end] / 10), xdata[end] + xdata[end] / 10)
    fit_y = gradient .* fit_x .+ constant_term

    # Draw fitted line:
    Plots.plot!(
        img,
        fit_x,
        fit_y,
        linestyle=:dash,
        label=line_title,
        color=:steelblue)

    # The bottleneck    
    @time Plots.pdf(img, img_path)
end

```

The compilation times of `Plots.{pdf, savefig}` seem irrelevant when looking at how many allocations they are doing. Again, `xdata` and `ydata` are vectors of length 10…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 6, 2021, 10:06am UTC](https://discourse.julialang.org/t/savefig-in-plots-jl-is-slow-when-i-scatter-13056-points/54443/22 "2021-08-06T10:06:06Z")

</div>

> [@SeSodesa](#):
>
> The compilation times of `Plots.{pdf, savefig}` seem irrelevant when looking at how many allocations they are doing. Again, `xdata` and `ydata` are vectors of length 10…

The allocations may also happen during compilation, they’re also counted towards the total allocation amount during the timed section.

---

<div class="post-metadata">

**Author:** ![SeSodesa](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@SeSodesa](https://discourse.julialang.org/u/SeSodesa)\
**Post date:** [August 6, 2021, 11:02am UTC](https://discourse.julialang.org/t/savefig-in-plots-jl-is-slow-when-i-scatter-13056-points/54443/23 "2021-08-06T11:02:38Z")

</div>

I guess this is why it is generally recommended to use more robust benchmarking tools. Regardless, even during a second run there are roughly 250k allocations made by `Plots.savefig` alone, in the case of the mentioned 10 data points.

[Previous page](https://discourse.julialang.org/t/savefig-in-plots-jl-is-slow-when-i-scatter-13056-points/54443.md?page=1)
