Table of Contents
In order to export a matplotlib plot with a transparent background, you can use the “savefig” function with the “transparent” parameter set to “True”. This will save the plot as a PNG file with a transparent background. Alternatively, you can specify the background color as “none” using the “facecolor” parameter to achieve the same result. This feature is useful for creating graphics that can be easily integrated into different backgrounds or presentations.
Export Matplotlib Plot with Transparent Background
You can use the following basic syntax to export a Matplotlib plot with a transparent background:
savefig('my_plot.png', transparent=True)
Note that the default argument for savefig() is transparent=False.
By specifying transparent=True we can save a Matplotlib figure with a transparent background.
The following example shows how to use this syntax in practice.
Example: Export Matplotlib Plot with Transparent Background
The following code shows how to create a line plot in Matplotlib and save the plot with a transparent background:
import matplotlib.pyplotas plt
#define x and y
x = [1, 4, 10, 15]
y = [5, 7, 21, 22]
#create line plot
plt.plot(x, y)
#add title and axis labels
plt.title('Title of Plot')
plt.xlabel('X Label')
plt.ylabel('Y Label')
#save plot with transparent background
plt.savefig('my_plot.png', transparent=True)
If I navigate to the location on my computer where the image is saved, I can view it:

However, this doesn’t illustrate the transparent background well.
To do so, I can place the image on a colored background in Excel:

Notice that the background is completely transparent.
You can contrast this with the exact same image saved without using the transparent argument:
#save plot without specifying transparent background
plt.savefig('my_plot2.png')

Note: You can find the complete online documentation for the savefig() function .
Cite this article
stats writer (2024). How can I export a matplotlib plot with a transparent background?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-export-a-matplotlib-plot-with-a-transparent-background/
stats writer. "How can I export a matplotlib plot with a transparent background?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-export-a-matplotlib-plot-with-a-transparent-background/.
stats writer. "How can I export a matplotlib plot with a transparent background?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-export-a-matplotlib-plot-with-a-transparent-background/.
stats writer (2024) 'How can I export a matplotlib plot with a transparent background?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-export-a-matplotlib-plot-with-a-transparent-background/.
[1] stats writer, "How can I export a matplotlib plot with a transparent background?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I export a matplotlib plot with a transparent background?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
