1

When saving a plot in pdf format matplotlib doens't retain transparency. I have tried in svg instead and here it does, so it is able to save the figure with transparency.

I have seen solutions such as in Matplotlib plots lose transparency when saving as .pdf but setting the axes to rasterized doesn't work for me. Is this a bug in matplotlib or am I doing something wrong?

Note: I am using python 3.9.2

CODE

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi)
y = np.cos(x)

fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x, y, '--')

fig.savefig('example.pdf', transparent=True, bbox_inches='tight')
fig.savefig('example.svg', transparent=True, bbox_inches='tight')
Morten Nissov
  • 392
  • 3
  • 13
  • Some forms of pdf ([pdf/a1](https://en.wikipedia.org/wiki/PDF/A#PDF/A-1)) do not support transparency - maybe check wich pdf versions you produce? How do you know that your image in your pdf is not transparent? You might have a transparent figure on the (default-white) background of the pdf. – Patrick Artner Mar 09 '21 at 12:13
  • I inserted the figure in a presentation and the background covered up some slide elements. – Morten Nissov Mar 09 '21 at 13:25
  • That being said it could be interesting to check which pdf versions I am producing, is this entirely on my local machine or is this a function of some pieces of matplotlib's library? Like is it a setting somewhere. – Morten Nissov Mar 09 '21 at 13:26

0 Answers0