Save figure with transparent background color in Plots.jl



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


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.


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

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


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


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


Works in plain GR:

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.


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.