0

I know that I can produce a png plot with a transparent background in the following way (which works for me):

import matplotlib.pyplot as plt
plt.plot ([1,2,3],[2,1,3])
plt.savefig("test.png",transparent=True)

But how can I make the background semi-transparent, with an fractional alpha number? I read on a separate blog that one could do it like this:

fig,ax=plt.subplots()
ax.plot ([1,2,3],[2,1,3]) 
ax.patch.set_facecolor('white')
ax.patch.set_alpha(0.5)
plt.savefig('test.png', facecolor=fig.get_facecolor(), edgecolor='none')

But that didn't work for me and produces a plot without transparency and gave me this non transparent png (confirmed in ppt).

example output

In response to the comments,

plt.get_backend()

gives me 'MacOSX' and I am on

Python 3.9.4 (default, Apr 5 2021, 01:49:30) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • 1
    I think the example you posted is working. What kind of matplotlib backend did you used? You can copy the image and paste it over another image to check if it works or not. Sometimes a transparent white color is not that obvious. – Chang Ye Nov 21 '21 at 22:53
  • thanks for the feedback - I tested it in powerpoint and imovie, and the background is completely solid. How do I see the backend? – ClimateUnboxed Nov 22 '21 at 07:15
  • Hi you can try: ` import matplotlib matplotlib.pyplot.get_backend() ` – manaclan Nov 22 '21 at 09:30
  • It gives me 'MacOSX' - I'll update the question with this info – ClimateUnboxed Nov 22 '21 at 09:32

1 Answers1

1

Your code behaved correctly and nothing is wrong. I tried it on colab and here is the results (notice the ax.patch.set_alpha() value):

import matplotlib.pyplot as plt
import numpy as np

fig,ax=plt.subplots()
ax.plot ([1,2,3],[2,1,3]) 
ax.patch.set_facecolor('white')
ax.patch.set_alpha(1)
plt.savefig('test.png', facecolor=fig.get_facecolor(), edgecolor='none')

enter image description here

import matplotlib.pyplot as plt
import numpy as np

fig,ax=plt.subplots()
ax.plot ([1,2,3],[2,1,3]) 
ax.patch.set_facecolor('white')
ax.patch.set_alpha(0)
plt.savefig('test.png', facecolor=fig.get_facecolor(), edgecolor='none')

enter image description here
Updated: After saving the plot, you can load it with opencv then change its transparency like this:

"""
pip install opencv-python
"""
import cv2

im = cv2.imread('test.png',cv2.IMREAD_UNCHANGED)
im[:,:,3] = im[:,:,3]/2
cv2.imwrite('adjust.png',im)

Update 2: I think I found what you want:

import matplotlib.pyplot as plt
import numpy as np
import cv2

fig,ax=plt.subplots()
ax.plot ([1,2,3],[2,1,3]) 
ax.patch.set_facecolor('white')
plt.savefig('original.png', edgecolor='none')
plt.savefig('transparent.png', edgecolor='none',transparent=True)
#Then
im = cv2.imread('original.png',cv2.IMREAD_UNCHANGED)
im[:,:,3] = im[:,:,3]/2 + 120
cv2.imwrite('semi_transparent.png',im)

Here is the results I got (tested on MS word):
enter image description here

manaclan
  • 816
  • 9
  • 20
  • I just cut and paste your second example code into a file to be ABSOLUTELY sure I wasn't being totally stupid, and it gives me a non-transparent image, confirmed in ppt. Setting transparent=True as in my first example works though. I'm at a loss... – ClimateUnboxed Nov 22 '21 at 10:01
  • The axes is transparent but the plot isn't. I would suggest you to load the plotted image with opencv and adjust the transparency – manaclan Nov 22 '21 at 10:08
  • I updated my answer with the above method – manaclan Nov 22 '21 at 10:12
  • @AdrianTompkins Calling `savefig()` with `transparent=True` will export a transparent plot. Ref: https://stackoverflow.com/questions/15857647/how-to-export-plots-from-matplotlib-with-transparent-background – manaclan Nov 22 '21 at 10:42
  • thank you Manacian, in fact I already saw that putting transparent=True already worked (see the first part of my question), but my aim was to have a semi transparent background with an alpha value of, say, 0.5 - that's the part that is not working for me... I can get fully transparent with that option only. – ClimateUnboxed Nov 22 '21 at 11:21
  • ps: I added the hyperlink in the question, in fact it was that SO post that gave me the first solution I posted. – ClimateUnboxed Nov 22 '21 at 11:30
  • Hi, I think you can use my first update to change the image to semi transparent by changing `im[:,:,3] = im[:,:,3]/2` to `im[:,:,3] = im[:,:,3]*0.5 +120` – manaclan Nov 22 '21 at 11:39
  • sorry I was in a rush when writing the above comment. Actually my propose is: first exported the plot to transparent with `savefig(transparent=True)` then use my update 1 code to make the transparent part become semi transparent (the full updated code was updated in update 2) – manaclan Nov 22 '21 at 13:24
  • hi there, pip3 install cv2 doesn't find the package... where is it from? – ClimateUnboxed Nov 22 '21 at 21:53
  • it's `pip install opencv-python` – manaclan Nov 23 '21 at 03:32