force background of matplotlib figure to be transparent

  • Last Update :
  • Techknowledgy :

e.g.:

import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(10))
fig.savefig('temp.png', transparent = True)

Another way is to set the appropriate global rcParams and simply specify the colors. Here is an MWE (I used the RGBA color format to specify the alpha/opacity):

import matplotlib.pyplot as plt

plt.rcParams.update({
   "figure.facecolor": (1.0, 0.0, 0.0, 0.3),
   # red with alpha = 30 %
   "axes.facecolor": (0.0, 1.0, 0.0, 0.5),
   # green with alpha = 50 %
   "savefig.facecolor": (0.0, 0.0, 1.0, 0.2),
   # blue with alpha = 20 %
})

plt.plot(range(10))
plt.savefig("temp.png")
plt.show()

Suggestion : 2

Edited ( November 20, 2019 ) Edit

Using subplot and the option axisbg

from pylab
import *

subplot(111, axisbg = '#ababab')
t = arange(0.0, 2.0, 0.01)
s = sin(2 * pi * t)
plot(t, s)
xlabel('x')
ylabel('y')
title('How to change background color in matplotlib ?')
savefig('exemple_01.png')
show()

It is also possible to change the transparency using patch.set_alpha()

import matplotlib.pyplot as plt
from pylab
import *

fig = plt.figure()

fig.patch.set_facecolor('#E0E0E0')
fig.patch.set_alpha(0.7)

ax = fig.add_subplot(111)

t = arange(0.0, 2.0, 0.01)
s = sin(2 * pi * t)
plot(t, s)

ax.plot(t, s) #plot(range(10))

ax.patch.set_facecolor('#ababab')
ax.patch.set_alpha(0.5)

# If we don 't specify the edgecolor and facecolor for the figure when
# saving with savefig, it will override the value we set earlier!
   fig.savefig('exemple_02.png', facecolor = fig.get_facecolor(), edgecolor = 'none')

plt.show()

Suggestion : 3

The matplotlib pyplot’s savefig() function is used to save a plot as a file. You can use the transparent argument to specify whether or not you want a transparent background for your saved image. The following is the syntax:,The default background for matplotlib plots is white, you can change the background colors of the plot, and you can also save your plots with a transparent background. In this tutorial, we’ll look at how to save a matplotlib figure as a PNG image with a transparent background.,You can notice that this image easily blends in with the colored background since it’s transparent. ,The above syntax assumes that “matplotlib.pyplot” is imported as “plt”. Note that the default value of the transparent argument is False.

The matplotlib pyplot’s savefig() function is used to save a plot as a file. You can use the transparent argument to specify whether or not you want a transparent background for your saved image. The following is the syntax:

plt.savefig("filename.png", transparent = True)

Let’s create a sample line chart in matplotlib and save it as an image file without explicitly passing a value to the transparent keyword. The below code creates a line chart of year-on-year employee growth at a company.

import matplotlib.pyplot as plt

# employee growth year over year
emp_count = [3, 20, 50, 200, 350, 400]
year = [2014, 2015, 2016, 2017, 2018, 2019]
# plot the employee growth
plt.plot(year, emp_count)

# add axes labels and plot title
plt.xlabel('Year')
plt.ylabel('Employees')
plt.title("Employee Growth YoY")

# save the plot as a PNG image
plt.savefig("employee_growth.png")

Now let’s go ahead and plot the same line plot but this time we’ll save it with a transparent background.

# plot the employee growth
plt.plot(year, emp_count)
# add axes labels and plot title
plt.xlabel('Year')
plt.ylabel('Employees')
plt.title("Employee Growth YoY")

# save the plot as a PNG image with Transparent background
plt.savefig("employee_growth_tp.png", transparent = True)