Save figure with transparent background color in Plots.jl

package
plotting

#1

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


#2

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.


#3

@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

#4

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


#5

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


#6

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.


#7

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.