How to save figure in matplotlib pyplot
In this article, you will learn to save matplotlib pyplot figure in different format.
Matplotlib is probably the single most used Python package for 2D-graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats.
For this task, first we need to import the matplotlib module and data to plot the graph -
import matplotlib.pyplot as plt # Data to plot x = [2, 3, 4, 5, 6, 8, 9, 10] y1 = [17, 15, 6, 12, 10, 25, 12, 14] y2 = [13, 12, 8, 7, 14, 18, 20, 11] y3 = [15, 10, 11, 8, 5, 12, 20, 23] plt.plot(x,y1, marker = 'o', color='#170337') plt.plot(x,y2, marker = 'x', color='#FAA50A') plt.plot(x,y3, marker = '+', color='#0496ff')
Next, we control over the plotting window GUI, using the figure() method. It has several parameters that determine how the figure look like. Here, figsize is the figure size (width, height) in inches. We set the size of our figure to be 8 by 5 inches. The fig.add_subplot() adds a subplot to the current figure. It returns a figure object. Using this, we set a title and labels for the x and y axis and grid pattern.
figsize = (8, 5) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) ax.set_title('Plot') ax.set_xlabel('measured') ax.set_ylabel('calculated') ax.grid(True)
The matplotlib.pyplot.savefig is used to save the current figure in different file format. It has the following syntax with basic parameters -
savefig(fname, dpi=None, facecolor='w', edgecolor='w', format=None, transparent=False, quality=None, optimize=Flase)
Here, fname is the file path, dpi is the resolution in dots per inch, facecolor is the face color, edgecolor is the edgecolor of the figure, format is the file format, e.g. 'png', 'pdf', 'svg' and the transparent is a boolean value. If True, the axes patches will all be transparent, the figure patch will also be transparent unless facecolor and/or edgecolor are specified via kwargs. The quality is the image quality, on a scale from 1 (worst) to 95 (best). The optimize is optional and only applicable in JPG file format.
Save matplotlib figure as JPG
Here is the complete code to save the generated matplotlib figure as jpg -
import matplotlib.pyplot as plt figsize = (8, 5) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) ax.set_title('Plot') ax.set_xlabel('measured') ax.set_ylabel('calculated') ax.grid(True) # Data to plot x = [2, 3, 4, 5, 6, 8, 9, 10] y1 = [17, 15, 6, 12, 10, 25, 12, 14] y2 = [13, 12, 8, 7, 14, 18, 20, 11] y3 = [15, 10, 11, 8, 5, 12, 20, 23] plt.plot(x,y1, marker = 'o', color='#170337') plt.plot(x,y2, marker = 'x', color='#FAA50A') plt.plot(x,y3, marker = '+', color='#0496ff') plt.savefig('multiplot.jpg', dpi=150, quality=90, optimize=True) plt.show()
The above code saves the following jpg file in your working directory -
Save matplotlib figure as PDF
Similarly, we can save the above plot in PDF file format.
Save matplotlib figure as PNG
We generally use the png format to create the transparent background. By default, matplotlib creates plot on white background. But you can also make the background transparent by passing transparent=true to the savefig() method, like -
plt.savefig('multiplot.png', dpi=200, transparent=True)
Related ArticlesHow to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Simple Face Detection Program in Python OpenCV
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples