# Save figure with transparent background color in Plots.jl

**URL:** <https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808>\
**Category:** General Usage\
**Tags:** package, plotting, plots\
**Created:** [December 18, 2018, 8:35pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808 "2018-12-18T20:35:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shunsuke-Hori](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shunsuke-hori/32/3732_2.png) [@Shunsuke-Hori](https://discourse.julialang.org/u/Shunsuke-Hori)\
**Post date:** [December 18, 2018, 8:35pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/1 "2018-12-18T20:35:49Z")

</div>

Is there a way to save a figure with transparent background color in Plots.jl? pyplot can do that by `transparent=True`.

---

<div class="post-metadata">

**Author:** ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)\
**Post date:** [December 18, 2018, 9:17pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/2 "2018-12-18T21:17:12Z")

</div>

Yes: `background_color = :transparent`. You might also need to set your `foreground_color` to `:black`, because otherwise the axes and grid lines seem to come out white for some reason.

---

<div class="post-metadata">

**Author:** ![Shunsuke-Hori](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shunsuke-hori/32/3732_2.png) [@Shunsuke-Hori](https://discourse.julialang.org/u/Shunsuke-Hori)\
**Post date:** [December 18, 2018, 9:56pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/3 "2018-12-18T21:56:17Z")

</div>

@sswatson Thank you for the help.

It seems to work for `.png` but not for `.pdf`.

I tried the following code and got nice png and black background pdf.

```julia
using Plots
p=plot([1,2,3], background_color=:transparent, foreground_color=:black)
savefig(p, "test.png") # work nicely
savefig(p, "test.pdf") # not work

```

---

<div class="post-metadata">

**Author:** ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)\
**Post date:** [December 18, 2018, 11:00pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/4 "2018-12-18T23:00:37Z")

</div>

Looks like a GR issue. Both PDF and PNG work for me with the PyPlot backend.

---

<div class="post-metadata">

**Author:** ![Shunsuke-Hori](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shunsuke-hori/32/3732_2.png) [@Shunsuke-Hori](https://discourse.julialang.org/u/Shunsuke-Hori)\
**Post date:** [December 18, 2018, 11:13pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/5 "2018-12-18T23:13:20Z")

</div>

@sswatson Oh, that is right. Thank you!!

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [December 19, 2018, 12:36am UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/6 "2018-12-19T00:36:05Z")

</div>

Works in plain GR:

```julia
julia> using GR
julia> plot([1,2,3])
julia> savefig("test.png")
julia> savefig("test.pdf")

```

Both files are transparent.

So, it’s a problem in the Plots `gr` backend code.

---

<div class="post-metadata">

**Author:** ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)\
**Post date:** [December 19, 2018, 12:49pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/7 "2018-12-19T12:49:03Z")

</div>

Yes—I should have said an issue with the GR backend, not necessarily GR per se.

Presumably what’s happening is that the color `:transparent`—which is an `RGBA` black with an alpha value of zero—is being passed to GR in such a way that the alpha value gets lost. I suppose this means that Plots.jl isn’t adhering to the GR interface for fill color opacities, at least when it comes to the background color.

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 2:13pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/8 "2021-12-05T14:13:18Z")

</div>

Is there a way to save a plot with transparent background to a pdf file?

---

<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:** [December 5, 2021, 2:18pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/9 "2021-12-05T14:18:36Z")

</div>

Have you tried `pgfplotsx()` backend?

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 2:43pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/10 "2021-12-05T14:43:24Z")

</div>

nope. I didn’t know this backend… Is this backend new? I am on julia 1.6.1, and I don’t see it there. Should I upgrade?

---

<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:** [December 5, 2021, 2:52pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/11 "2021-12-05T14:52:19Z")

</div>

[Older than 1 year](https://discourse.julialang.org/t/getting-an-error-with-the-pgfplotsx-backend-for-plots-jl/38136/4), for sure.

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 2:59pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/12 "2021-12-05T14:59:06Z")

</div>

So why do I get this?

```julia
pgfplotsx()

ArgumentError: Package PGFPlotsX not found in current path:
- Run `import Pkg; Pkg.add("PGFPlotsX")` to install the PGFPlotsX package.

```

---

<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:** [December 5, 2021, 3:08pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/13 "2021-12-05T15:08:09Z")

</div>

`using Plots` first please, and what version?

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 3:40pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/14 "2021-12-05T15:40:17Z")

</div>

Of course that I did `using Plots`  
and the version is ` [91a5bcdd] Plots v1.25.0`.

---

<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:** [December 5, 2021, 3:57pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/15 "2021-12-05T15:57:40Z")

</div>

Of course you did. In order to use `pgfplotsx()` backend it seems that one has to install first the package `PGFPlotsX.jl`, and to have a LaTeX installation (MikTeX or other).

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 3:58pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/16 "2021-12-05T15:58:07Z")

</div>

I have latex installed.  
I tried this package, but I still get a black background.  
This is my plot:

```julia
pgfplotsx()
plot(x, sin.(x), background_color=:transparent)

```

---

<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:** [December 5, 2021, 4:32pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/17 "2021-12-05T16:32:09Z")

</div>

This creates a PDF with no black background:

```julia
using Plots; pgfplotsx()
plot(sin)

```

How do you distinguish in such PDF a transparent background from a white background?

You may try also:

```julia
plot(sin, background_color=RGBA(1,1,1,0))

```

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 4:35pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/18 "2021-12-05T16:35:14Z")

</div>

I tried both and it does not work for me.  
To really check the background color, I add the file to a keynote slide, and I see that the background is white.

---

<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:** [December 5, 2021, 4:37pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/19 "2021-12-05T16:37:20Z")

</div>

From [this issue](https://github.com/JuliaPlots/Plots.jl/issues/2725), `pyplot()` backend might be your best bet.

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [December 5, 2021, 4:40pm UTC](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808/20 "2021-12-05T16:40:24Z")

</div>

I see. Thanks!  
I see there is some issue with `pyplot` that I have to solve. But once I manage that I will test and update!  
Thanks!
