5
$\begingroup$

I'm wondering if it's possible to somehow change the background of the result of a plot function so that whenever I paste it in a movie editor, it will be transparent (not white). An example code is:

Animate[Plot[Sin[x t], {x, 0, 2 Pi }], {t, 0, 1}]

Here, the background is white. I would like it to be transparent.

EDIT

I tried (simplified example code):

test = Plot[Sin[2 x], {x, 0, 2 Pi}, Background-> None]
Export["test.jpg", test]

this resulted in:

planets

Is there any way of saving the file with a transparent background? Maybe the file extension is wrong?

$\endgroup$
3
  • 5
    $\begingroup$ Animate[Plot[Sin[x t], {x, 0, 2 Pi}, Background -> None], {t, 0, 1}] ? $\endgroup$
    – Mr.Wizard
    Jun 21, 2020 at 4:22
  • 4
    $\begingroup$ does this give what you need: testframe = RemoveBackground[Plot[Sin[2 x], {x, 0, 2 Pi}]] Export["testframe.png", testframe]? $\endgroup$
    – kglr
    Jun 21, 2020 at 23:35
  • $\begingroup$ @kglr Yes, this is perfect, thank you! $\endgroup$ Jun 21, 2020 at 23:41

3 Answers 3

4
$\begingroup$

You should specify Background -> None for Export and use an image format which supports transparency:

test = Plot[Sin[2 x], {x, 0, 2 Pi}];
file = Export["test.png", test, Background -> None];

The exported image has an alpha channel:

img = Import[file];
ImageMeasurements[img, "Transparency"]
img // AlphaChannel
True

output

$\endgroup$
0
$\begingroup$

I'm wondering if it's possible to somehow change the background of the result of a plot function

one option is to use Background option

Plot[Sin[x ], {x, 0, 2 Pi}, Background -> LightGray]

Mathematica graphics

Adjust the color as needed.

You can add Opacity as follows

 Plot[Sin[x ], {x, 0, 2 Pi}, Background -> Directive[Opacity[.2], Red]]

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ This doesn't solve the problem at all. The question was whether there is an option that exports the graphic/animation such that it has a transparent background. Background -> None doesn't do this for some reason $\endgroup$
    – Gert
    Oct 8, 2021 at 13:39
0
$\begingroup$

Both jpeg(jpg) and png seems to be non-transparent, at least by default and without some extra tricks. If you save as PDF, which is transparent, you can render to pixels in, e.g., Photoshop, and maintain transparency and save as png.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.