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.
Is there a way to save a plot with transparent background to a pdf file?
Have you tried pgfplotsx()
backend?
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?
Older than 1 year, for sure.
So why do I get this?
pgfplotsx()
ArgumentError: Package PGFPlotsX not found in current path:
- Run `import Pkg; Pkg.add("PGFPlotsX")` to install the PGFPlotsX package.
using Plots
first please, and what version?
Of course that I did using Plots
and the version is [91a5bcdd] Plots v1.25.0
.
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).
I have latex installed.
I tried this package, but I still get a black background.
This is my plot:
pgfplotsx()
plot(x, sin.(x), background_color=:transparent)
This creates a PDF with no black background:
using Plots; pgfplotsx()
plot(sin)
How do you distinguish in such PDF a transparent background from a white background?
You may try also:
plot(sin, background_color=RGBA(1,1,1,0))
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.
From this issue, pyplot()
backend might be your best bet.
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!